Class: Picklive::Currency::Base
- Inherits:
-
Object
- Object
- Picklive::Currency::Base
- Includes:
- Comparable
- Defined in:
- lib/picklive/currency.rb
Overview
Instance Attribute Summary collapse
-
#integer_amount ⇒ Object
readonly
Returns the value of attribute integer_amount.
Class Method Summary collapse
- .[](human_value) ⇒ Object
- .cash? ⇒ Boolean
- .fake? ⇒ Boolean
- .html_symbol ⇒ Object
- .virtual? ⇒ Boolean
Instance Method Summary collapse
- #*(multiplier) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(divider) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #abs ⇒ Object
- #amount ⇒ Object (also: #to_decimal)
- #eql?(other) ⇒ Boolean
- #for_sentence ⇒ Object
-
#initialize(integer_amount) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #num_from(other) ⇒ Object
- #same_type!(other) ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
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_amount ⇒ Object
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
27 |
# File 'lib/picklive/currency.rb', line 27 def self.cash? ; real? ; end |
.fake? ⇒ Boolean
26 |
# File 'lib/picklive/currency.rb', line 26 def self.fake? ; not real? ; end |
.html_symbol ⇒ Object
29 |
# File 'lib/picklive/currency.rb', line 29 def self.html_symbol ; symbol ; end |
.virtual? ⇒ 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 |
#abs ⇒ Object
57 |
# File 'lib/picklive/currency.rb', line 57 def abs; self.class.new(integer_amount.abs); end |
#amount ⇒ Object 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
68 |
# File 'lib/picklive/currency.rb', line 68 def eql?(other); self == other; end |
#for_sentence ⇒ Object
89 90 91 |
# File 'lib/picklive/currency.rb', line 89 def for_sentence to_s(:short => true) end |
#inspect ⇒ Object
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_f ⇒ Object
86 |
# File 'lib/picklive/currency.rb', line 86 def to_f; integer_amount.to_f; end |
#to_i ⇒ Object
85 |
# File 'lib/picklive/currency.rb', line 85 def to_i; integer_amount; end |