Class: BillerBotResource::Product::Context

Inherits:
Resource
  • Object
show all
Defined in:
lib/biller_bot_resource/product.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#productObject

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.

Parameters:

  • type (String)

    The type of context to be searching for

Returns:



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_selfArray<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_persistedObject

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

#parentObject



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_optionsObject



8
9
10
# File 'lib/biller_bot_resource/product.rb', line 8

def prefix_options
  { :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