Class: Odin::Types::OdinCurrency

Inherits:
OdinValue show all
Defined in:
lib/odin/types/values.rb

Instance Attribute Summary collapse

Attributes inherited from OdinValue

#directives, #modifiers

Instance Method Summary collapse

Methods inherited from OdinValue

#array?, #binary?, #boolean?, #confidential?, #currency?, #date?, #deprecated?, #duration?, #integer?, #null?, #number?, #numeric?, #object?, #percent?, #reference?, #required?, #string?, #temporal?, #time?, #timestamp?, #verb?

Constructor Details

#initialize(value, currency_code: nil, decimal_places: 2, raw: nil, **kwargs) ⇒ OdinCurrency

Returns a new instance of OdinCurrency.



221
222
223
224
225
226
227
228
# File 'lib/odin/types/values.rb', line 221

def initialize(value, currency_code: nil, decimal_places: 2, raw: nil, **kwargs)
  super(**kwargs)
  @value = value.is_a?(BigDecimal) ? value : BigDecimal(value.to_s)
  @currency_code = currency_code&.freeze
  @decimal_places = decimal_places
  @raw = raw&.freeze
  freeze
end

Instance Attribute Details

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



219
220
221
# File 'lib/odin/types/values.rb', line 219

def currency_code
  @currency_code
end

#decimal_placesObject (readonly)

Returns the value of attribute decimal_places.



219
220
221
# File 'lib/odin/types/values.rb', line 219

def decimal_places
  @decimal_places
end

#rawObject (readonly)

Returns the value of attribute raw.



219
220
221
# File 'lib/odin/types/values.rb', line 219

def raw
  @raw
end

#valueObject (readonly)

Returns the value of attribute value.



219
220
221
# File 'lib/odin/types/values.rb', line 219

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



232
233
234
235
# File 'lib/odin/types/values.rb', line 232

def ==(other)
  other.is_a?(OdinCurrency) && value == other.value &&
    currency_code == other.currency_code
end

#hashObject



238
239
240
# File 'lib/odin/types/values.rb', line 238

def hash
  [ValueType::CURRENCY, value, currency_code].hash
end

#to_sObject



242
243
244
245
# File 'lib/odin/types/values.rb', line 242

def to_s
  s = raw || value.to_s("F")
  currency_code ? "#{s}:#{currency_code}" : s
end

#typeObject



230
# File 'lib/odin/types/values.rb', line 230

def type; ValueType::CURRENCY; end

#with_directives(dirs) ⇒ Object



252
253
254
255
# File 'lib/odin/types/values.rb', line 252

def with_directives(dirs)
  OdinCurrency.new(value, currency_code: currency_code, decimal_places: decimal_places,
                          raw: raw, modifiers: modifiers, directives: dirs)
end

#with_modifiers(mods) ⇒ Object



247
248
249
250
# File 'lib/odin/types/values.rb', line 247

def with_modifiers(mods)
  OdinCurrency.new(value, currency_code: currency_code, decimal_places: decimal_places,
                          raw: raw, modifiers: mods, directives: directives)
end