Class: BookingSync::API::Resource
- Inherits:
-
Hash
- Object
- Hash
- BookingSync::API::Resource
- Includes:
- Hashie::Extensions::MethodAccess
- Defined in:
- lib/bookingsync/api/resource.rb
Instance Attribute Summary collapse
-
#_client ⇒ Object
readonly
Returns the value of attribute _client.
-
#_rels ⇒ Object
readonly
Returns the value of attribute _rels.
-
#_resources_key ⇒ Object
readonly
Returns the value of attribute _resources_key.
Instance Method Summary collapse
-
#initialize(client, data = {}, rels = {}, resources_key = nil) ⇒ Resource
constructor
Initialize a Resource with the given relations and data.
-
#method_missing(method, *args) ⇒ Object
Make associations accessible.
-
#process_value(value) ⇒ Object
Process an individual value of this resource.
- #to_s ⇒ Object
Constructor Details
#initialize(client, data = {}, rels = {}, resources_key = nil) ⇒ Resource
Initialize a Resource with the given relations and data.
14 15 16 17 18 19 20 21 |
# File 'lib/bookingsync/api/resource.rb', line 14 def initialize(client, data = {}, rels = {}, resources_key = nil) @_client = client @_resources_key = resources_key data.each do |key, value| self[key.to_sym] = process_value(value) end @_rels = rels end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Make associations accessible
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bookingsync/api/resource.rb', line 40 def method_missing(method, *args) return self[method] if has_key?(method) # eager loaded with :include association_key = :"#{@_resources_key}.#{method}" if (polymorphic_association = find_polymorphic_association(self[:links], method)) attributes = polymorphic_association.last ids, type = Array(attributes[:id]), attributes[:type] resolved_association_key = :"#{@_resources_key}.#{type.downcase}" uri_association_key = "#{association_key}.id" extract_resources(ids, resolved_association_key, uri_association_key, *args) elsif self[:links] && self[:links].has_key?(method) ids = Array(self[:links][method]) extract_resources(ids, association_key, association_key, *args) else super end end |
Instance Attribute Details
#_client ⇒ Object (readonly)
Returns the value of attribute _client.
6 7 8 |
# File 'lib/bookingsync/api/resource.rb', line 6 def _client @_client end |
#_rels ⇒ Object (readonly)
Returns the value of attribute _rels.
6 7 8 |
# File 'lib/bookingsync/api/resource.rb', line 6 def _rels @_rels end |
#_resources_key ⇒ Object (readonly)
Returns the value of attribute _resources_key.
6 7 8 |
# File 'lib/bookingsync/api/resource.rb', line 6 def _resources_key @_resources_key end |
Instance Method Details
#process_value(value) ⇒ Object
Process an individual value of this resource. Hashes get exploded into another Resource, and Arrays get their values processed too.
28 29 30 31 32 33 34 |
# File 'lib/bookingsync/api/resource.rb', line 28 def process_value(value) case value when Hash then self.class.new(@_client, value) when Array then value.map { |v| process_value(v) } else value end end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/bookingsync/api/resource.rb', line 58 def to_s id.to_s end |