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 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
#_name ⇒ Object
Return resource name. Method is prefixed with an underscore to prevent name collision with ResourceInstance attributes.
58 59 60 |
# File 'lib/haveapi/client/resource.rb', line 58 def _name @name end |
#inspect ⇒ Object
45 46 47 |
# File 'lib/haveapi/client/resource.rb', line 45 def inspect super end |
#new ⇒ Object
Create a new instance of a resource. The created instance is not persistent until ResourceInstance#save is called.
51 52 53 |
# File 'lib/haveapi/client/resource.rb', line 51 def new ResourceInstance.new(@client, @api, self, action: @actions[:create], persistent: false) end |
#setup(description) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/haveapi/client/resource.rb', line 16 def setup(description) 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.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/haveapi/client/resource.rb', line 33 def setup_from_clone(original) 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 |