Class: Satoshi

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

Defined Under Namespace

Classes: 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.



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

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.



13
14
15
# File 'lib/satoshi.rb', line 13

def from_unit
  @from_unit
end

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

Returns the value of attribute to_unit.



13
14
15
# File 'lib/satoshi.rb', line 13

def to_unit
  @to_unit
end

#valueObject

Returns the value of attribute value.



13
14
15
# File 'lib/satoshi.rb', line 13

def value
  @value
end

Instance Method Details

#*(i) ⇒ Object



85
86
87
# File 'lib/satoshi.rb', line 85

def *(i)
  self.to_i * i
end

#+(i) ⇒ Object



77
78
79
# File 'lib/satoshi.rb', line 77

def +(i)
  self.to_i + i
end

#-(i) ⇒ Object



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

def -(i)
  self.to_i - i
end

#<(i) ⇒ Object



61
62
63
# File 'lib/satoshi.rb', line 61

def <(i)
  self.to_i < i
end

#<=(i) ⇒ Object



69
70
71
# File 'lib/satoshi.rb', line 69

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

#==(i) ⇒ Object



73
74
75
# File 'lib/satoshi.rb', line 73

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

#>(i) ⇒ Object



57
58
59
# File 'lib/satoshi.rb', line 57

def >(i)
  self.to_i > i
end

#>=(i) ⇒ Object



65
66
67
# File 'lib/satoshi.rb', line 65

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

#coerce(other) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/satoshi.rb', line 89

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



53
54
55
# File 'lib/satoshi.rb', line 53

def satoshi_value=(v)
  @value = v
end

#to_btc(as: :number) ⇒ Object



26
27
28
# File 'lib/satoshi.rb', line 26

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

#to_iObject Also known as: satoshi_value



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

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

#to_mbtc(as: :number) ⇒ Object



30
31
32
# File 'lib/satoshi.rb', line 30

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

#to_sObject



44
45
46
# File 'lib/satoshi.rb', line 44

def to_s
  to_unit.to_s
end