Class: Picklive::Currency::Base

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/picklive/currency.rb

Overview

To create a currency object:

GBP.new(1000) # -> <GBP:10.0>
GBP[10]       # -> <GBP:10.0>
GBP['10.00']  # -> <GBP:10.0>
Chips[1000]   # -> <Chips:1000>

Direct Known Subclasses

Chips, GBP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(integer_amount) ⇒ Base

Returns a new instance of Base.



34
35
36
# File 'lib/picklive/currency.rb', line 34

def initialize integer_amount
  self.integer_amount = integer_amount.to_i
end

Instance Attribute Details

#integer_amountObject

Returns the value of attribute integer_amount.



31
32
33
# File 'lib/picklive/currency.rb', line 31

def integer_amount
  @integer_amount
end

Class Method Details

.[](human_value) ⇒ Object



38
39
40
41
# File 'lib/picklive/currency.rb', line 38

def self.[](human_value)
  v = human_value.to_f
  new((v * self.precision).round)
end

.cash?Boolean

Returns:

  • (Boolean)


27
# File 'lib/picklive/currency.rb', line 27

def self.cash?              ; real?     ; end

.fake?Boolean

Returns:

  • (Boolean)


26
# File 'lib/picklive/currency.rb', line 26

def self.fake?              ; not real? ; end

.html_symbolObject



29
# File 'lib/picklive/currency.rb', line 29

def self.html_symbol        ; symbol    ; end

.virtual?Boolean

Returns:

  • (Boolean)


28
# File 'lib/picklive/currency.rb', line 28

def self.virtual?           ; !cash?    ; end

Instance Method Details

#*(multiplier) ⇒ Object



54
# File 'lib/picklive/currency.rb', line 54

def *(multiplier); self.class.new(integer_amount * multiplier); end

#+(other) ⇒ Object



52
# File 'lib/picklive/currency.rb', line 52

def +(other); same_type!(other); self.class.new(integer_amount + num_from(other)); end

#-(other) ⇒ Object



53
# File 'lib/picklive/currency.rb', line 53

def -(other); same_type!(other); self.class.new(integer_amount - num_from(other)); end

#-@Object



56
# File 'lib/picklive/currency.rb', line 56

def -@; self.class.new(-integer_amount); end

#/(divider) ⇒ Object



55
# File 'lib/picklive/currency.rb', line 55

def /(divider); self.*(1/divider.to_f); end

#<=>(other) ⇒ Object



60
# File 'lib/picklive/currency.rb', line 60

def <=>(other); same_type!(other); self.integer_amount <=> num_from(other); end

#==(other) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/picklive/currency.rb', line 61

def ==(other)
  if other.is_a?(self.class)
    self.integer_amount == other.integer_amount
  else
    self.integer_amount == 0 && (other.respond_to?(:to_i) && other.to_i == 0)
  end
end

#absObject



57
# File 'lib/picklive/currency.rb', line 57

def abs; self.class.new(integer_amount.abs); end

#amountObject Also known as: to_decimal



43
44
45
46
47
48
49
# File 'lib/picklive/currency.rb', line 43

def amount
  if self.class.precision == 1
    integer_amount
  else
    integer_amount / self.class.precision.to_f
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
# File 'lib/picklive/currency.rb', line 68

def eql?(other); self == other; end

#for_sentenceObject



89
90
91
# File 'lib/picklive/currency.rb', line 89

def for_sentence
  to_s(:short => true)
end

#inspectObject



87
# File 'lib/picklive/currency.rb', line 87

def inspect; "<#{self.class.code}:#{amount}>"; end

#num_from(other) ⇒ Object



81
82
83
# File 'lib/picklive/currency.rb', line 81

def num_from(other)
  other.respond_to?(:integer_amount) ? other.integer_amount : other
end

#same_type!(other) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/picklive/currency.rb', line 70

def same_type!(other)
  unless other.is_a?(Fixnum) || other.is_a?(Float)
    unless other.is_a?(Picklive::Currency::Base)
      raise "Not a currency: #{other.inspect}"
    end
    if self.class.code != other.class.code
      raise ArgumentError.new("Different currencies: #{self.class.code} vs #{other.class.code}")
    end
  end
end

#to_fObject



86
# File 'lib/picklive/currency.rb', line 86

def to_f;  integer_amount.to_f;  end

#to_iObject



85
# File 'lib/picklive/currency.rb', line 85

def to_i;  integer_amount;       end