Class: SatoshiProc

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

Defined Under Namespace

Classes: TooLarge, TooManyDigitsAfterDecimalPoint

Constant Summary collapse

UNIT_DENOMINATIONS =

Says how many digits after the decimal point a denomination has.

{
  proc:     8,
  mproc:    5,
  satoshiproc: 0
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n = nil, from_unit: 'proc', to_unit: 'proc', unit: nil) ⇒ SatoshiProc

Returns a new instance of SatoshiProc.



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

def initialize(n=nil, from_unit: 'proc', to_unit: 'proc', 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_satoshiproc(n) if n
end

Instance Attribute Details

#from_unitObject (readonly)

Returns the value of attribute from_unit.



14
15
16
# File 'lib/satoshiproc.rb', line 14

def from_unit
  @from_unit
end

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

Returns the value of attribute to_unit.



14
15
16
# File 'lib/satoshiproc.rb', line 14

def to_unit
  @to_unit
end

#valueObject

Returns the value of attribute value.



14
15
16
# File 'lib/satoshiproc.rb', line 14

def value
  @value
end

Instance Method Details

#*(i) ⇒ Object

IMPORTANT: multiplication is done on satoshiprocs, not proc. 0.01*0.02 PROC will be a larger value.



96
97
98
99
100
101
102
# File 'lib/satoshiproc.rb', line 96

def *(i)
  if i.kind_of?(SatoshiProc)
    SatoshiProc.new(self.to_i * i, from_unit: :satoshiproc)
  else
    self.to_i * i
  end
end

#+(i) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/satoshiproc.rb', line 78

def +(i)
  if i.kind_of?(SatoshiProc)
    SatoshiProc.new(self.to_i + i, from_unit: :satoshiproc)
  else
    self.to_i + i
  end
end

#-(i) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/satoshiproc.rb', line 86

def -(i)
  if i.kind_of?(SatoshiProc)
    SatoshiProc.new(self.to_i - i, from_unit: :satoshiproc)
  else
    self.to_i - i
  end
end

#<(i) ⇒ Object



62
63
64
# File 'lib/satoshiproc.rb', line 62

def <(i)
  self.to_i < i
end

#<=(i) ⇒ Object



70
71
72
# File 'lib/satoshiproc.rb', line 70

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

#==(i) ⇒ Object



74
75
76
# File 'lib/satoshiproc.rb', line 74

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

#>(i) ⇒ Object



58
59
60
# File 'lib/satoshiproc.rb', line 58

def >(i)
  self.to_i > i
end

#>=(i) ⇒ Object



66
67
68
# File 'lib/satoshiproc.rb', line 66

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

#coerce(other) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/satoshiproc.rb', line 104

def coerce(other)
  if other.kind_of?(Integer)
    [other, self.to_i]
  else
    raise TypeError, message: "SatoshiProc cannot be coerced into anything but Integer"
  end
end

#satoshiproc_value=(v) ⇒ Object



54
55
56
# File 'lib/satoshiproc.rb', line 54

def satoshiproc_value=(v)
  @value = v
end

#to_iObject Also known as: satoshiproc_value



39
40
41
42
# File 'lib/satoshiproc.rb', line 39

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

#to_mproc(as: :number) ⇒ Object



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

def to_mproc(as: :number)
  to_denomination(UNIT_DENOMINATIONS[:mproc], as: as)
end

#to_proc(as: :number) ⇒ Object



27
28
29
# File 'lib/satoshiproc.rb', line 27

def to_proc(as: :number)
  to_denomination(UNIT_DENOMINATIONS[:proc], as: as)
end

#to_sObject



45
46
47
# File 'lib/satoshiproc.rb', line 45

def to_s
  to_unit.to_s
end