Class: Satoshi
- Inherits:
-
Object
show all
- Defined in:
- lib/satoshi.rb
Defined Under Namespace
Classes: TooLarge, TooManyDigitsAfterDecimalPoint
Constant Summary
collapse
- UNIT_DENOMINATIONS =
Says how many digits after the decimal point a denomination has.
{
btc: 8,
mbtc: 5,
satoshi: 0
}
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(n = nil, from_unit: 'btc', to_unit: 'btc', unit: nil) ⇒ Satoshi
Returns a new instance of Satoshi.
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/satoshi.rb', line 16
def initialize(n=nil, from_unit: 'btc', to_unit: 'btc', unit: nil)
n = 0 if n.nil?
if unit
@from_unit = @to_unit = unit.downcase.to_sym
else
@from_unit = from_unit.downcase.to_sym
@to_unit = to_unit.downcase.to_sym
end
@value = convert_to_satoshi(n) if n
end
|
Instance Attribute Details
#from_unit ⇒ Object
Returns the value of attribute from_unit.
14
15
16
|
# File 'lib/satoshi.rb', line 14
def from_unit
@from_unit
end
|
#to_unit(as: :number) ⇒ Object
Returns the value of attribute to_unit.
14
15
16
|
# File 'lib/satoshi.rb', line 14
def to_unit
@to_unit
end
|
#value ⇒ Object
Returns the value of attribute value.
14
15
16
|
# File 'lib/satoshi.rb', line 14
def value
@value
end
|
Instance Method Details
#*(i) ⇒ Object
IMPORTANT: multiplication is done on satoshis, not btc. 0.01*0.02 BTC will be a larger value.
96
97
98
99
100
101
102
|
# File 'lib/satoshi.rb', line 96
def *(i)
if i.kind_of?(Satoshi)
Satoshi.new(self.to_i * i, from_unit: :satoshi)
else
self.to_i * i
end
end
|
#+(i) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/satoshi.rb', line 78
def +(i)
if i.kind_of?(Satoshi)
Satoshi.new(self.to_i + i, from_unit: :satoshi)
else
self.to_i + i
end
end
|
#-(i) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/satoshi.rb', line 86
def -(i)
if i.kind_of?(Satoshi)
Satoshi.new(self.to_i - i, from_unit: :satoshi)
else
self.to_i - i
end
end
|
#<(i) ⇒ Object
62
63
64
|
# File 'lib/satoshi.rb', line 62
def <(i)
self.to_i < i
end
|
#<=(i) ⇒ Object
70
71
72
|
# File 'lib/satoshi.rb', line 70
def <=(i)
self.to_i <= i
end
|
#==(i) ⇒ Object
74
75
76
|
# File 'lib/satoshi.rb', line 74
def ==(i)
self.to_i == i
end
|
#>(i) ⇒ Object
58
59
60
|
# File 'lib/satoshi.rb', line 58
def >(i)
self.to_i > i
end
|
#>=(i) ⇒ Object
66
67
68
|
# File 'lib/satoshi.rb', line 66
def >=(i)
self.to_i >= i
end
|
#coerce(other) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/satoshi.rb', line 104
def coerce(other)
if other.kind_of?(Integer)
[other, self.to_i]
else
raise TypeError, message: "Satoshi cannot be coerced into anything but Integer"
end
end
|
#satoshi_value=(v) ⇒ Object
54
55
56
|
# File 'lib/satoshi.rb', line 54
def satoshi_value=(v)
@value = v
end
|
#to_btc(as: :number) ⇒ Object
27
28
29
|
# File 'lib/satoshi.rb', line 27
def to_btc(as: :number)
to_denomination(UNIT_DENOMINATIONS[:btc], as: as)
end
|
#to_i ⇒ Object
Also known as:
satoshi_value
39
40
41
42
|
# File 'lib/satoshi.rb', line 39
def to_i
return 0 if @value.nil?
@value
end
|
#to_mbtc(as: :number) ⇒ Object
31
32
33
|
# File 'lib/satoshi.rb', line 31
def to_mbtc(as: :number)
to_denomination(UNIT_DENOMINATIONS[:mbtc], as: as)
end
|
#to_s ⇒ Object
45
46
47
|
# File 'lib/satoshi.rb', line 45
def to_s
to_unit.to_s
end
|