Class: NEAR::Balance

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

Overview

Represents a NEAR balance.

Constant Summary collapse

ZERO =

The canonical zero balance.

self.new(0).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quantity) ⇒ Balance

Returns a new instance of Balance.

Parameters:

  • quantity (Numeric)

Raises:

  • (ArgumentError)

    if quantity is not a valid number



19
20
21
22
23
24
25
26
27
28
# File 'lib/near/balance.rb', line 19

def initialize(quantity)
  @quantity = case quantity
    when BigDecimal then quantity
    when Rational then quantity
    when Integer then quantity
    when Float then quantity
    when String then BigDecimal(quantity)
    else raise ArgumentError, "invalid quantity: #{quantity.inspect}"
  end
end

Class Method Details

.from_near(s) ⇒ Object



12
13
14
# File 'lib/near/balance.rb', line 12

def self.from_near(s)
  self.new(s)
end

.parse(s) ⇒ Object



8
9
10
# File 'lib/near/balance.rb', line 8

def self.parse(s)
  self.new(s.to_f / 10**24)
end

Instance Method Details

#inspectString

The balance as a Ⓝ-prefixed string.

Returns:

  • (String)


38
39
40
# File 'lib/near/balance.rb', line 38

def inspect
  "Ⓝ #{@quantity.to_f}"
end

#to_fFloat

The balance as a floating-point number.

Returns:

  • (Float)


69
# File 'lib/near/balance.rb', line 69

def to_f; @quantity.to_f; end

#to_iInteger

The balance as an integer.

Returns:

  • (Integer)


63
# File 'lib/near/balance.rb', line 63

def to_i; @quantity.to_i; end

#to_rRational

The balance as a rational number.

Returns:

  • (Rational)


57
# File 'lib/near/balance.rb', line 57

def to_r; @quantity.to_r; end

#to_sString

The balance as a string.

Returns:

  • (String)


46
47
48
49
50
51
# File 'lib/near/balance.rb', line 46

def to_s
  case @quantity
    when BigDecimal then @quantity.to_s('F')
    else @quantity.to_s
  end
end