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
-
#_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 |
# File 'lib/haveapi/client/resource.rb', line 7 def initialize(client, api, name) @client = client @api = api @name = name @prepared_args = [] 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
#_name ⇒ Object
Return resource name. Method is prefixed with an underscore to prevent name collision with ResourceInstance attributes.
57 58 59 |
# File 'lib/haveapi/client/resource.rb', line 57 def _name @name end |
#inspect ⇒ Object
44 45 46 |
# File 'lib/haveapi/client/resource.rb', line 44 def inspect super end |
#new ⇒ Object
Create a new instance of a resource. The created instance is not persistent until ResourceInstance#save is called.
50 51 52 |
# File 'lib/haveapi/client/resource.rb', line 50 def new ResourceInstance.new(@client, @api, self, action: @actions[:create], persistent: false) end |
#setup(description) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/haveapi/client/resource.rb', line 14 def setup(description) @actions = {} @resources = {} description[:actions].each do |name, desc| action = HaveAPI::Client::Action.new(@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.
34 35 36 37 38 39 40 41 42 |
# File 'lib/haveapi/client/resource.rb', line 34 def setup_from_clone(original) original.actions.each_value do |action| define_action(action) end original.resources.each_value do |resource| define_resource(resource) end end |