Class: HaveAPI::Client::Resource
- Inherits:
-
Object
- Object
- HaveAPI::Client::Resource
- Defined in:
- lib/haveapi/client/resource.rb
Overview
An API resource.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#prepared_args ⇒ Object
Returns the value of attribute prepared_args.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
-
#_description ⇒ Object
Return resource description.
-
#_name ⇒ Object
Return resource name.
-
#initialize(client, api, name) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
-
#new ⇒ Object
Create a new instance of a resource.
- #setup(description) ⇒ Object
-
#setup_from_clone(original) ⇒ Object
Copy actions and resources from the
original
resource and create methods for this instance.
Constructor Details
#initialize(client, api, name) ⇒ Resource
Returns a new instance of Resource.
7 8 9 10 11 12 13 14 |
# File 'lib/haveapi/client/resource.rb', line 7 def initialize(client, api, name) @client = client @api = api @name = name @prepared_args = [] @actions = {} @resources = {} end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
4 5 6 |
# File 'lib/haveapi/client/resource.rb', line 4 def actions @actions end |
#prepared_args ⇒ Object
Returns the value of attribute prepared_args.
5 6 7 |
# File 'lib/haveapi/client/resource.rb', line 5 def prepared_args @prepared_args end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
4 5 6 |
# File 'lib/haveapi/client/resource.rb', line 4 def resources @resources end |
Instance Method Details
#_description ⇒ Object
Return resource description. Method is prefixed with an underscore to prevent name collision with ResourceInstance attributes.
69 70 71 |
# File 'lib/haveapi/client/resource.rb', line 69 def _description @description end |
#_name ⇒ Object
Return resource name. Method is prefixed with an underscore to prevent name collision with ResourceInstance attributes.
62 63 64 |
# File 'lib/haveapi/client/resource.rb', line 62 def _name @name end |
#inspect ⇒ Object
16 17 18 |
# File 'lib/haveapi/client/resource.rb', line 16 def inspect "#<#{self.class.name} @name=#{@name} @prepared_args=#{@prepared_args} @actions=#{@actions.keys.inspect} @resources=#{@resources.keys.inspect}>" end |
#new ⇒ Object
Create a new instance of a resource. The created instance is not persistent until ResourceInstance#save is called.
55 56 57 |
# File 'lib/haveapi/client/resource.rb', line 55 def new ResourceInstance.new(@client, @api, self, action: @actions[:create], persistent: false) end |
#setup(description) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/haveapi/client/resource.rb', line 20 def setup(description) @description = description description[:actions].each do |name, desc| action = HaveAPI::Client::Action.new(@client, @api, name, desc, []) define_action(action) @actions[name] = action end description[:resources].each do |name, desc| r = HaveAPI::Client::Resource.new(@client, @api, name) r.setup(desc) define_resource(r) @resources[name] = r end end |
#setup_from_clone(original) ⇒ Object
Copy actions and resources from the original
resource and create methods for this instance.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/haveapi/client/resource.rb', line 39 def setup_from_clone(original) @description = original._description original.actions.each do |name, action| define_action(action) @actions[name] = action end original.resources.each do |name, resource| define_resource(resource) @resources[name] = resource end end |