Class: Rubybear::Type::Amount

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

Overview

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.



6
7
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
# File 'lib/rubybear/type/amount.rb', line 6

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

Instance Method Details

#to_aObject



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

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

#to_bytesObject



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

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



52
53
54
# File 'lib/rubybear/type/amount.rb', line 52

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