Class: Radiator::Type::Amount

Inherits:
Serializer show all
Defined in:
lib/radiator/type/amount.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakS, #pakStr, #pakc, #paks, #send_log, #unhexlify, #varint, #warning

Constructor Details

#initialize(value) ⇒ Amount

Returns a new instance of Amount.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/radiator/type/amount.rb', line 8

def initialize(value)
  super(:amount, value)
  
  case value
  when ::Array
    a, @precision, @nai = value
    @asset = case @nai
    when '@@000000013' then 'SBD'
    when '@@000000021' then 'STEEM'
    when '@@000000037' then 'VESTS'
    else; raise TypeError, "Asset #{@asset} unknown."
    end
    @amount = "%.#{@precision}f" % (a.to_f / 10 ** @precision)
  else
    @amount, @asset = value.strip.split(' ')
    @precision = case @asset
    when 'STEEM' then 3
    when 'VESTS' then 6
    when 'SBD' then 3
    when 'CORE' then 3
    when 'CESTS' then 6
    when 'TEST' then 3
    else; raise TypeError, "Asset #{@asset} unknown."
    end
  end
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



6
7
8
# File 'lib/radiator/type/amount.rb', line 6

def amount
  @amount
end

#assetObject (readonly)

Returns the value of attribute asset.



6
7
8
# File 'lib/radiator/type/amount.rb', line 6

def asset
  @asset
end

#naiObject (readonly)

Returns the value of attribute nai.



6
7
8
# File 'lib/radiator/type/amount.rb', line 6

def nai
  @nai
end

#precisionObject (readonly)

Returns the value of attribute precision.



6
7
8
# File 'lib/radiator/type/amount.rb', line 6

def precision
  @precision
end

Instance Method Details

#to_aObject



44
45
46
47
48
49
50
51
# File 'lib/radiator/type/amount.rb', line 44

def to_a
  case @asset
  when 'STEEM' then [(@amount.to_f * 1000).to_i.to_s, 3, '@@000000021']
  when 'VESTS' then [(@amount.to_f * 1000000).to_i.to_s, 6, '@@000000037']
  when 'SBD' then [(@amount.to_f * 1000).to_i.to_s, 3, '@@000000013']
  else; raise TypeError, "Asset #{@asset} unknown."
  end
end

#to_bytesObject



35
36
37
38
39
40
41
42
# File 'lib/radiator/type/amount.rb', line 35

def to_bytes
  asset = @asset.ljust(7, "\x00")
  amount = (@amount.to_f * 10 ** @precision).round
  
  [amount].pack('q') +
  [@precision].pack('c') +
  asset
end

#to_sObject



53
54
55
# File 'lib/radiator/type/amount.rb', line 53

def to_s
  "#{@amount} #{@asset}"
end