Class: DPay::Type::Amount

Inherits:
BaseType show all
Defined in:
lib/dpay/type/amount.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Amount

Returns a new instance of Amount.



16
17
18
19
20
21
22
23
24
25
26
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
# File 'lib/dpay/type/amount.rb', line 16

def initialize(value)
  super(:amount, value)

  case value
  when Array
    @amount, @precision, @nai = value
    @asset = case @nai
    when '@@000000013' then 'BBD'
    when '@@000000021' then 'BEX'
    when '@@000000037' then '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 '@@000000013' then 'BBD'
    when '@@000000021' then 'BEX'
    when '@@000000037' then '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 ['', '']
    @precision = case @asset
    when 'BEX' then 3
    when 'VESTS' then 6
    when 'BBD' 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/dpay/type/amount.rb', line 6

def amount
  @amount
end

#assetObject (readonly)

Returns the value of attribute asset.



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

def asset
  @asset
end

#naiObject (readonly)

Returns the value of attribute nai.



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

def nai
  @nai
end

#precisionObject (readonly)

Returns the value of attribute precision.



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

def precision
  @precision
end

Class Method Details

.to_h(amount) ⇒ Object



8
9
10
# File 'lib/dpay/type/amount.rb', line 8

def self.to_h(amount)
  new(amount).to_h
end

.to_s(amount) ⇒ Object



12
13
14
# File 'lib/dpay/type/amount.rb', line 12

def self.to_s(amount)
  new(amount).to_s
end

Instance Method Details

#to_aObject



68
69
70
71
72
73
74
# File 'lib/dpay/type/amount.rb', line 68

def to_a
  case @asset
  when 'BEX' 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 'BBD' then [(@amount.to_f * 1000).to_i.to_s, 3, '@@000000013']
  end
end

#to_bytesObject



59
60
61
62
63
64
65
66
# File 'lib/dpay/type/amount.rb', line 59

def to_bytes
  asset = @asset.ljust(7, "\x00")
  amount = (@amount.to_f * 10 ** @precision).round

  [amount].pack('q') +
  [@precision].pack('c') +
  asset
end

#to_hObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dpay/type/amount.rb', line 76

def to_h
  case @asset
  when 'BEX' then {
    amount: (@amount.to_f * 1000).to_i.to_s,
    precision: 3,
    nai: '@@000000021'
  }
  when 'VESTS' then {
    amount: (@amount.to_f * 1000000).to_i.to_s,
    precision: 6,
    nai: '@@000000037'
  }
when 'BBD' then {
    amount: (@amount.to_f * 1000).to_i.to_s,
    precision: 3,
    nai: '@@000000013'
  }
  end
end

#to_sObject



96
97
98
# File 'lib/dpay/type/amount.rb', line 96

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