Class: BillerBotResource::Product

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

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Methods inherited from Resource

#cache_key, config, configure, instantiate_collection, query_string, #save

Constructor Details

#initialize(*args) ⇒ Product

Returns a new instance of Product.



64
65
66
67
68
69
70
# File 'lib/biller_bot_resource/product.rb', line 64

def initialize(*args)
  super
  contexts.each do |c|
    c.product = self
    c.force_persisted # See comments on method
  end
end

Instance Method Details

#all_contextsObject



77
78
79
# File 'lib/biller_bot_resource/product.rb', line 77

def all_contexts
  contexts.map(&:descendants_with_self).flatten
end

#context(id) ⇒ Object

Raises:

  • (ActiveResource::ResourceNotFound)


81
82
83
84
85
# File 'lib/biller_bot_resource/product.rb', line 81

def context(id)
  context = all_contexts.select { |c| c.id.try(:to_i) == id.to_i }.first
  raise ActiveResource::ResourceNotFound.new(nil) if context.nil?
  context
end

#contextsObject



72
73
74
75
# File 'lib/biller_bot_resource/product.rb', line 72

def contexts
  @attributes[:contexts] ||= []
  @attributes[:contexts]
end

#root_context_for_account_id(account_id) ⇒ BillerBotResource::ProductContext|nil

Fetch the root product context for the given account ID.

Parameters:

  • account_id (Integer)

Returns:

  • (BillerBotResource::ProductContext|nil)


92
93
94
# File 'lib/biller_bot_resource/product.rb', line 92

def ()
  contexts.select { |c| c.type == "AccountProductContext" && c. ==  }.first
end