Class: ODDB::Util::Money

Inherits:
Object show all
Includes:
Comparable, OddbYaml
Defined in:
src/util/money.rb,
ext/export/src/oddb_yaml.rb

Constant Summary

EXPORT_PROPERTIES =
[
  '@amount',
  '@authority',
  '@origin',
  '@type',
  '@valid_from',
]

Constants included from OddbYaml

EXPORT_PROPERTIES, YAML_URI

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from OddbYaml

#to_yaml_properties, #to_yaml_type

Constructor Details

- (Money) initialize(amount, type = nil, country = nil)

A new instance of Money



10
11
12
13
14
# File 'src/util/money.rb', line 10

def initialize(amount, type=nil, country=nil)
  self.amount = amount
  @type, @country = type.to_s.downcase, country.to_s.upcase
  @valid_from = Time.now
end

Instance Attribute Details

- (Object) authority

Returns the value of attribute authority



8
9
10
# File 'src/util/money.rb', line 8

def authority
  @authority
end

- (Object) country

Returns the value of attribute country



8
9
10
# File 'src/util/money.rb', line 8

def country
  @country
end

- (Object) credits (readonly)

Returns the value of attribute credits



7
8
9
# File 'src/util/money.rb', line 7

def credits
  @credits
end

- (Object) mutation_code

Returns the value of attribute mutation_code



8
9
10
# File 'src/util/money.rb', line 8

def mutation_code
  @mutation_code
end

- (Object) origin

Returns the value of attribute origin



8
9
10
# File 'src/util/money.rb', line 8

def origin
  @origin
end

- (Object) type

Returns the value of attribute type



8
9
10
# File 'src/util/money.rb', line 8

def type
  @type
end

- (Object) valid_from

Returns the value of attribute valid_from



8
9
10
# File 'src/util/money.rb', line 8

def valid_from
  @valid_from
end

Instance Method Details

- (Object) *(other)



42
43
44
# File 'src/util/money.rb', line 42

def *(other)
  Money.new((@amount || to_f) * other.to_f)
end

- (Object) +(other)



36
37
38
# File 'src/util/money.rb', line 36

def +(other)
  Money.new((@amount || to_f) + other.to_f)
end

- (Object) -(other)



39
40
41
# File 'src/util/money.rb', line 39

def -(other)
  Money.new((@amount || to_f) - other.to_f)
end

- (Object) /(other)



45
46
47
# File 'src/util/money.rb', line 45

def /(other)
  Money.new((@amount || to_f) / other.to_f)
end

- (Object) <=>(other)



48
49
50
51
52
53
54
55
# File 'src/util/money.rb', line 48

def <=>(other)
  case other
  when Money
    @credits.to_i <=> other.credits.to_i
  else
    to_f <=> other.to_f
  end
end

- (Object) amount=(amount)



15
16
17
18
19
20
21
22
# File 'src/util/money.rb', line 15

def amount=(amount)
  if amount
    @amount = amount.to_f
    @credits = (@amount * 100).round
  else
    @amount = @credits = nil
  end
end

- (Boolean) is_for?(type, country)

Returns:

  • (Boolean)


23
24
25
# File 'src/util/money.rb', line 23

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

- (Object) to_f Also known as: amount



26
27
28
# File 'src/util/money.rb', line 26

def to_f
  @amount || (@credits.to_f / 100)
end

- (Object) to_i



30
31
32
# File 'src/util/money.rb', line 30

def to_i
  @credits.to_i
end

- (Object) to_s



33
34
35
# File 'src/util/money.rb', line 33

def to_s
  sprintf("%1.2f", to_f)
end