Class: ODDB::Business::Price

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/oddb/business/price.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, country = nil, amount = nil) ⇒ Price

Returns a new instance of Price.



9
10
11
12
13
14
# File 'lib/oddb/business/price.rb', line 9

def initialize type=nil, country=nil, amount=nil
  @country = country.to_s.upcase
  @type = type.to_s.downcase
  @values = []
  self.value = amount if amount
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



27
28
29
# File 'lib/oddb/business/price.rb', line 27

def method_missing *args, &block
  current.send *args, &block
end

Instance Attribute Details

#countryObject

Returns the value of attribute country.



8
9
10
# File 'lib/oddb/business/price.rb', line 8

def country
  @country
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/oddb/business/price.rb', line 8

def type
  @type
end

#valuesObject (readonly)

Returns the value of attribute values.



8
9
10
# File 'lib/oddb/business/price.rb', line 8

def values
  @values
end

Instance Method Details

#<=>(other) ⇒ Object



55
56
57
# File 'lib/oddb/business/price.rb', line 55

def <=>(other)
  current <=> other
end

#at(time = Time.now) ⇒ Object



15
16
17
# File 'lib/oddb/business/price.rb', line 15

def at time=Time.now
  @values.find do |price| price.valid_from <= time end
end

#currentObject



21
22
23
# File 'lib/oddb/business/price.rb', line 21

def current
  at Time.now
end

#is_for?(type, country) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/oddb/business/price.rb', line 24

def is_for? type, country
  @type == type.to_s.downcase && @country == country.to_s.upcase
end

#to_fObject



30
31
32
# File 'lib/oddb/business/price.rb', line 30

def to_f
  current.to_f
end

#to_sObject



33
34
35
# File 'lib/oddb/business/price.rb', line 33

def to_s
  current.to_s
end

#value=(args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/oddb/business/price.rb', line 39

def value=(args)
  money, valid_from, _ = args
  if money.is_a?(Numeric)
    money = Util::Money.new money.to_f
    if valid_from.is_a?(Time)
      money.valid_from = valid_from
    end
  elsif !money.is_a?(Util::Money)
    raise ArgumentError,
      "Cannot set the value of a Price to '#{money}'. Only Numeric or Money are allowed"
  end
  cur = at(money.valid_from)
  return cur if cur == money
  @values = @values.push(money).sort_by(&:valid_from).reverse
  money
end