Class: Polyseerio::Resource::Base
- Inherits:
-
Object
- Object
- Polyseerio::Resource::Base
- Defined in:
- lib/resource/base.rb
Overview
Base class for any resource.
Instance Attribute Summary collapse
-
#eid ⇒ Object
Returns the value of attribute eid.
Instance Method Summary collapse
-
#copts ⇒ Object
Returns the client options this class was constructed with.
-
#get(key, default = nil) ⇒ Object
TODO: move towards this.
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
-
#method_missing(name, *args) ⇒ Object
rubocop:disable all.
-
#new? ⇒ Boolean
True if the resource is new (not yet saved).
-
#override_properties(properties) ⇒ Object
Set a property hash on the instance.
-
#properties ⇒ Object
Returns a copy of class attributes.
-
#request ⇒ Object
Returns the client’s request object.
- #respond_to_missing?(method_name) ⇒ Boolean
-
#set(key, value) ⇒ Object
TODO: move towards this.
-
#type ⇒ Object
Returns the type of the class.
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 16 17 |
# File 'lib/resource/base.rb', line 10 def initialize(attributes = {}) @eid = if attributes.include? :eid attributes[:eid] else Polyseerio::SDK::Helper.resolve_eid(copts) end @attributes = attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
rubocop:disable all
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/resource/base.rb', line 61 def method_missing(name, *args) # rubocop:disable all # Setter. if name =~ /^(\w+)=$/ name = :"#{$1}" # rubocop:disable all @attributes[:"#{$1}"] = args[0] # rubocop:disable all end # Getter. @attributes.fetch(name, nil) end |
Instance Attribute Details
#eid ⇒ Object
Returns the value of attribute eid.
7 8 9 |
# File 'lib/resource/base.rb', line 7 def eid @eid end |
Instance Method Details
#copts ⇒ Object
Returns the client options this class was constructed with.
37 38 39 |
# File 'lib/resource/base.rb', line 37 def copts self.class.copts end |
#get(key, default = nil) ⇒ Object
TODO: move towards this
52 53 54 |
# File 'lib/resource/base.rb', line 52 def get(key, default = nil) @attributes.fetch(key, default) end |
#new? ⇒ Boolean
True if the resource is new (not yet saved).
47 48 49 |
# File 'lib/resource/base.rb', line 47 def new? id.nil? end |
#override_properties(properties) ⇒ Object
Set a property hash on the instance.
20 21 22 23 24 |
# File 'lib/resource/base.rb', line 20 def override_properties(properties) properties.each_with_object(self) do |(key, value), this| this.send(:"#{key}=", value) end end |
#properties ⇒ Object
Returns a copy of class attributes.
27 28 29 |
# File 'lib/resource/base.rb', line 27 def properties attributes.clone end |
#request ⇒ Object
Returns the client’s request object.
42 43 44 |
# File 'lib/resource/base.rb', line 42 def request self.class.request end |
#respond_to_missing?(method_name) ⇒ Boolean
73 74 75 |
# File 'lib/resource/base.rb', line 73 def respond_to_missing?(method_name) !methods.include(method_name) end |
#set(key, value) ⇒ Object
TODO: move towards this
57 58 59 |
# File 'lib/resource/base.rb', line 57 def set(key, value) @attributes[key] = value end |
#type ⇒ Object
Returns the type of the class.
32 33 34 |
# File 'lib/resource/base.rb', line 32 def type self.class.type end |