Class: CryptoUnit

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

Direct Known Subclasses

LitoshiUnit, SatoshiUnit

Defined Under Namespace

Classes: TooLarge, TooManyDigitsAfterDecimalPoint

Constant Summary collapse

UNIT_DENOMINATIONS =

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

{
  standart: 8,
  milli: 5,
  primary: 0,
}
MAX_VALUE =
0
NAME =
''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n = nil, from_unit: 'standart', to_unit: 'standart', unit: nil) ⇒ CryptoUnit

Returns a new instance of CryptoUnit.



18
19
20
21
22
23
24
25
26
27
# File 'lib/crypto_unit_base.rb', line 18

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

Instance Attribute Details

#from_unitObject (readonly)

Returns the value of attribute from_unit.



16
17
18
# File 'lib/crypto_unit_base.rb', line 16

def from_unit
  @from_unit
end

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

Returns the value of attribute to_unit.



16
17
18
# File 'lib/crypto_unit_base.rb', line 16

def to_unit
  @to_unit
end

#valueObject

Returns the value of attribute value.



16
17
18
# File 'lib/crypto_unit_base.rb', line 16

def value
  @value
end

Instance Method Details

#*(i) ⇒ Object

IMPORTANT: multiplication is done on primary units, not standart. 0.01*0.02 will be a larger value.



98
99
100
101
102
103
104
# File 'lib/crypto_unit_base.rb', line 98

def *(i)
  if i.kind_of?(CryptoUnit)
    self.class.new(self.to_i * i, from_unit: :primary)
  else
    self.to_i * i
  end
end

#+(i) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/crypto_unit_base.rb', line 80

def +(i)
  if i.kind_of?(CryptoUnit)
    self.class.new(self.to_i + i, from_unit: :primary)
  else
    self.to_i + i
  end
end

#-(i) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/crypto_unit_base.rb', line 88

def -(i)
  if i.kind_of?(CryptoUnit)
    self.class.new(self.to_i - i, from_unit: :primary)
  else
    self.to_i - i
  end
end

#<(i) ⇒ Object



64
65
66
# File 'lib/crypto_unit_base.rb', line 64

def <(i)
  self.to_i < i
end

#<=(i) ⇒ Object



72
73
74
# File 'lib/crypto_unit_base.rb', line 72

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

#==(i) ⇒ Object



76
77
78
# File 'lib/crypto_unit_base.rb', line 76

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

#>(i) ⇒ Object



60
61
62
# File 'lib/crypto_unit_base.rb', line 60

def >(i)
  self.to_i > i
end

#>=(i) ⇒ Object



68
69
70
# File 'lib/crypto_unit_base.rb', line 68

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

#coerce(other) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/crypto_unit_base.rb', line 106

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

#primary_value=(v) ⇒ Object



56
57
58
# File 'lib/crypto_unit_base.rb', line 56

def primary_value=(v)
  @value = v
end

#to_iObject Also known as: primary_value



41
42
43
44
# File 'lib/crypto_unit_base.rb', line 41

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

#to_milli(as: :number) ⇒ Object



33
34
35
# File 'lib/crypto_unit_base.rb', line 33

def to_milli(as: :number)
  to_denomination(UNIT_DENOMINATIONS[:milli], as: as)
end

#to_sObject



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

def to_s
  to_unit.to_s
end

#to_standart(as: :number) ⇒ Object



29
30
31
# File 'lib/crypto_unit_base.rb', line 29

def to_standart(as: :number)
  to_denomination(UNIT_DENOMINATIONS[:standart], as: as)
end