Class: Vendor::Products

Inherits:
Object
  • Object
show all
Defined in:
lib/project/products.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(products = [], &block) ⇒ Products

Returns a new instance of Products.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/project/products.rb', line 9

def initialize(products=[], &block)
  # Set up products array
  @products = []

  # Set up block
  @block = block

  # Set up blocks for giving a callback
  @blocks = []

  # Initialize buy block
  @buy = Vendor::Buy.new

  # Set up info for products
  products.each do |product|
    # Set default options
    default_options = {
      :id => "no_id",
      :secret => "no_secret",
      :subscription => false,
      :price => "0.99",
      :price_locale => nil,
      :title => "No Title",
      :desc => "No Description.",
      :sk_product => nil
    }
    options = default_options.merge(product)

    # Update product and set exists variable
    @products << Vendor::Product.new(options.to_object, @buy) do |block|
      @blocks << block
      @block.call(@blocks) unless @block.nil? || @blocks.count!=@products.count
    end
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/project/products.rb', line 3

def block
  @block
end

#productsObject (readonly)

Returns the value of attribute products.



3
4
5
# File 'lib/project/products.rb', line 3

def products
  @products
end

Instance Method Details

#[](key) ⇒ Object



5
6
7
# File 'lib/project/products.rb', line 5

def [](key)
  return @products.select{ |p| p.params[:name]==key.to_s }.first
end