Class: BillerBotResource::Product::Context
- Inherits:
-
Resource
- Object
- ActiveResource::Base
- Resource
- BillerBotResource::Product::Context
- Defined in:
- lib/biller_bot_resource/product.rb
Instance Attribute Summary collapse
-
#product ⇒ Object
Returns the value of attribute product.
Instance Method Summary collapse
-
#children_with_type(type) ⇒ Array<BillerBotResource::Product::Context] The matching contexts
Fetch all the children contexts with the given context type.
-
#descendants_with_self ⇒ Array<BillerBotResource::Product::Context>
Fetch all the child contexts in a flat structure.
-
#force_persisted ⇒ Object
ActiveResource treats our nested models as non-persisted models.
- #parent ⇒ Object
- #prefix_options ⇒ Object
- #with_child(context) ⇒ Object
Methods inherited from Resource
#cache_key, config, configure, #initialize, instantiate_collection, query_string, #save
Constructor Details
This class inherits a constructor from BillerBotResource::Resource
Instance Attribute Details
#product ⇒ Object
Returns the value of attribute product.
6 7 8 |
# File 'lib/biller_bot_resource/product.rb', line 6 def product @product end |
Instance Method Details
#children_with_type(type) ⇒ Array<BillerBotResource::Product::Context] The matching contexts
Fetch all the children contexts with the given context type.
31 32 33 |
# File 'lib/biller_bot_resource/product.rb', line 31 def children_with_type(type) contexts.select { |c| (c.type.downcase == type.to_s.downcase) } end |
#descendants_with_self ⇒ Array<BillerBotResource::Product::Context>
Fetch all the child contexts in a flat structure. Also include myself.
39 40 41 |
# File 'lib/biller_bot_resource/product.rb', line 39 def descendants_with_self [self, contexts.map(&:descendants_with_self)].flatten.uniq end |
#force_persisted ⇒ Object
ActiveResource treats our nested models as non-persisted models. We make this little hack so that the contexts can be loaded with the product, but saved independently.
16 17 18 19 20 21 22 23 24 |
# File 'lib/biller_bot_resource/product.rb', line 16 def force_persisted @persisted = true # Propogate to all children too. contexts.each do |c| c.product = product c.force_persisted end end |
#parent ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/biller_bot_resource/product.rb', line 43 def parent return nil unless product product.contexts.each do |c| r = c.with_child(self) return r if r end nil end |
#prefix_options ⇒ Object
8 9 10 |
# File 'lib/biller_bot_resource/product.rb', line 8 def { :product_id => product.id } end |
#with_child(context) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/biller_bot_resource/product.rb', line 53 def with_child(context) return self if contexts.include?(context) contexts.each do |c| result = c.with_child(context) return result if result end nil end |