Class: GOBL::Value

Inherits:
Struct show all
Defined in:
lib/gobl/value.rb

Overview

Base class for single value structs in the GOBL Schema

Instance Method Summary collapse

Methods inherited from Struct

from_data, from_json!, #to_json

Constructor Details

#initialize(value) ⇒ Value

Initializes a new value object that corresponds to a given value. The value can be a ‘Symbol`, a `String` or anything coercible into one (via `#to_s`).

Parameters:

  • value (Symbol, String, #to_s)

    the value of the object.



12
13
14
15
# File 'lib/gobl/value.rb', line 12

def initialize(value)
  super()
  self._value = value.to_s
end

Instance Method Details

#==(other) ⇒ Boolean

Returns whether the current value is equal to a given one. In addition to objects of the same type, the current object can be compared to a ‘Symbol`, a `String` or anything coercible into one (via `#to_s`)

Parameters:

  • other (Value, Symbol, String, #to_s)

    the other value to compare with

Returns:

  • (Boolean)

    ‘true` if the values are equal, `false` otherwise



38
39
40
41
42
43
44
45
46
47
# File 'lib/gobl/value.rb', line 38

def ==(other)
  case other
  when self.class
    _value == other._value
  when Symbol
    to_sym == other
  else
    to_s == other.to_s
  end
end

#as_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
# File 'lib/gobl/value.rb', line 69

def as_json(...)
  _value.as_json(...)
end

#eql?(other) ⇒ Boolean

Returns whether the current value is equal to a given one, unlike ‘#==`, without doing any coercion. That is, the other object must be of the same class to return true.

Parameters:

  • other (Object)

    the other value to compare with

Returns:

  • (Boolean)

    ‘true` if the values are equal, `false` otherwise



56
57
58
# File 'lib/gobl/value.rb', line 56

def eql?(other)
  self.class == other.class && _value.eql?(other._value)
end

#hashInteger

Returns a integer hash code of the current value that respects the following property: Given objects ‘a` and `b`. If `a.eql?(b)`, then `a.hash == b.hash`.

Returns:

  • (Integer)

    hash code of the current value



64
65
66
# File 'lib/gobl/value.rb', line 64

def hash
  [self.class.name, _value].hash
end

#to_sString

Returns the string representation of the current object

Returns:

  • (String)

    the string representation of the current object



20
21
22
# File 'lib/gobl/value.rb', line 20

def to_s
  _value.to_s
end

#to_symSymbol

Returns the symbol representation of the current object

Returns:

  • (Symbol)

    the symbol representation of the current object



27
28
29
# File 'lib/gobl/value.rb', line 27

def to_sym
  to_s.parameterize.underscore.to_sym
end