Class: Hive::Type::Amount
Overview
Constant Summary collapse
- ASSET_HBD =
'HBD'
- NAI_HBD =
'@@000000013'
- ASSET_HIVE =
'HIVE'
- NAI_HIVE =
'@@000000021'
- ASSET_VESTS =
'VESTS'
- NAI_VESTS =
'@@000000037'
- ASSET_TBD =
'TBD'
- NAI_TBD =
'@@000000013'
- ASSET_TESTS =
'TESTS'
- NAI_TESTS =
'@@000000021'
- VALID_ASSETS =
[ASSET_HBD, ASSET_HIVE, ASSET_VESTS, ASSET_TBD, ASSET_TESTS]
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#asset ⇒ Object
readonly
Returns the value of attribute asset.
-
#nai ⇒ Object
readonly
Returns the value of attribute nai.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(value) ⇒ Amount
constructor
A new instance of Amount.
- #to_a ⇒ Object
- #to_bytes ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ Amount
Returns a new instance of Amount.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/hive/type/amount.rb', line 27 def initialize(value) super(:amount, value) case value when Array @amount, @precision, @nai = value @asset = case @nai when NAI_HBD then ASSET_HBD when NAI_HIVE then ASSET_HIVE when NAI_VESTS then ASSET_VESTS else; raise TypeError, "Asset #{@nai} unknown." end @amount = "%.#{@precision}f" % (@amount.to_f / 10 ** @precision) when Hash @amount, @precision, @nai = value.map do |k, v| v if %i(amount precision nai).include? k.to_sym end.compact @asset = case @nai when NAI_HBD then ASSET_HBD when NAI_HIVE then ASSET_HIVE when NAI_VESTS then ASSET_VESTS else; raise TypeError, "Asset #{@nai} unknown." end @amount = "%.#{@precision}f" % (@amount.to_f / 10 ** @precision) when Amount @precision = value.precision @nai = value.nai @asset = value.asset @amount = value.amount else @amount, @asset = value.strip.split(' ') rescue ['', ''] # Usually, we will see unexpected 'STEEM' and 'SBD' from Hive-HF23 # because deserialization contains them for app compatibility. @asset = case @asset when 'STEEM' then ASSET_HIVE when 'SBD' then ASSET_HBD else warn "Got unexpected asset: #{value}" unless VALID_ASSETS.include? @asset @asset end @precision = case @asset when ASSET_HIVE then 3 when ASSET_VESTS then 6 when ASSET_HBD then 3 when ASSET_TESTS then 3 when ASSET_TBD then 3 else; raise TypeError, "Asset #{@asset} unknown." end end end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
13 14 15 |
# File 'lib/hive/type/amount.rb', line 13 def amount @amount end |
#asset ⇒ Object (readonly)
Returns the value of attribute asset.
13 14 15 |
# File 'lib/hive/type/amount.rb', line 13 def asset @asset end |
#nai ⇒ Object (readonly)
Returns the value of attribute nai.
13 14 15 |
# File 'lib/hive/type/amount.rb', line 13 def nai @nai end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
13 14 15 |
# File 'lib/hive/type/amount.rb', line 13 def precision @precision end |
Class Method Details
.to_bytes(amount) ⇒ Object
23 24 25 |
# File 'lib/hive/type/amount.rb', line 23 def self.to_bytes(amount) new(amount).to_bytes end |
.to_h(amount) ⇒ Object
15 16 17 |
# File 'lib/hive/type/amount.rb', line 15 def self.to_h(amount) new(amount).to_h end |
.to_s(amount) ⇒ Object
19 20 21 |
# File 'lib/hive/type/amount.rb', line 19 def self.to_s(amount) new(amount).to_s end |
Instance Method Details
#to_a ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/hive/type/amount.rb', line 93 def to_a case @asset when ASSET_HIVE then [(@amount.to_f * 1000).to_i.to_s, 3, NAI_HIVE] when ASSET_VESTS then [(@amount.to_f * 1000000).to_i.to_s, 6, NAI_VESTS] when ASSET_HBD then [(@amount.to_f * 1000).to_i.to_s, 3, NAI_HBD] end end |
#to_bytes ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/hive/type/amount.rb', line 84 def to_bytes asset = @asset.ljust(7, "\x00") amount = (@amount.to_f * 10 ** @precision).round [amount].pack('q') + [@precision].pack('c') + asset end |
#to_h ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/hive/type/amount.rb', line 101 def to_h case @asset when ASSET_HIVE then { amount: (@amount.to_f * 1000).to_i.to_s, precision: 3, nai: NAI_HIVE } when ASSET_VESTS then { amount: (@amount.to_f * 1000000).to_i.to_s, precision: 6, nai: NAI_VESTS } when ASSET_HBD then { amount: (@amount.to_f * 1000).to_i.to_s, precision: 3, nai: NAI_HBD } end end |
#to_s ⇒ Object
121 122 123 |
# File 'lib/hive/type/amount.rb', line 121 def to_s "#{@amount} #{@asset}" end |