Class: Satoshi

Inherits:
Object
  • Object
show all
Defined in:
lib/satoshi.rb

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.



13
14
15
16
17
18
19
20
21
22
# File 'lib/satoshi.rb', line 13

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_unitObject (readonly)

Returns the value of attribute from_unit.



11
12
13
# File 'lib/satoshi.rb', line 11

def from_unit
  @from_unit
end

#to_unit(as: :number) ⇒ Object (readonly)

Returns the value of attribute to_unit.



11
12
13
# File 'lib/satoshi.rb', line 11

def to_unit
  @to_unit
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/satoshi.rb', line 11

def value
  @value
end

Instance Method Details

#*(i) ⇒ Object



79
80
81
# File 'lib/satoshi.rb', line 79

def *(i)
  self.to_i * i
end

#+(i) ⇒ Object



71
72
73
# File 'lib/satoshi.rb', line 71

def +(i)
  self.to_i + i
end

#-(i) ⇒ Object



75
76
77
# File 'lib/satoshi.rb', line 75

def -(i)
  self.to_i - i
end

#<(i) ⇒ Object



55
56
57
# File 'lib/satoshi.rb', line 55

def <(i)
  self.to_i < i
end

#<=(i) ⇒ Object



63
64
65
# File 'lib/satoshi.rb', line 63

def <=(i)
  self.to_i <= i
end

#==(i) ⇒ Object



67
68
69
# File 'lib/satoshi.rb', line 67

def ==(i)
  self.to_i == i
end

#>(i) ⇒ Object



51
52
53
# File 'lib/satoshi.rb', line 51

def >(i)
  self.to_i > i
end

#>=(i) ⇒ Object



59
60
61
# File 'lib/satoshi.rb', line 59

def >=(i)
  self.to_i >= i
end

#coerce(other) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/satoshi.rb', line 83

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



47
48
49
# File 'lib/satoshi.rb', line 47

def satoshi_value=(v)
  @value = v
end

#to_btc(as: :number) ⇒ Object



24
25
26
# File 'lib/satoshi.rb', line 24

def to_btc(as: :number)
  to_denomination(UNIT_DENOMINATIONS[:btc], as: as)
end

#to_iObject Also known as: satoshi_value



36
37
38
39
# File 'lib/satoshi.rb', line 36

def to_i
  return 0 if @value.nil?
  @value
end

#to_mbtc(as: :number) ⇒ Object



28
29
30
# File 'lib/satoshi.rb', line 28

def to_mbtc(as: :number)
  to_denomination(UNIT_DENOMINATIONS[:mbtc], as: as)
end