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 |
# 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 self[:links] && self[:links].has_key?(method) ids = Array(self[:links][method]) return [] if ids.empty? = {uri: {association_key => ids}} .merge!(query: args.first) if args.first.is_a?(Hash) @_rels[association_key].get().resources 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
54 55 56 |
# File 'lib/bookingsync/api/resource.rb', line 54 def to_s id.to_s end |