Class: TypedEAV::Field::Decimal

Inherits:
Base show all
Defined in:
app/models/typed_eav/field/decimal.rb

Constant Summary

Constants inherited from Base

Base::RESERVED_NAMES

Constants included from ColumnMapping

ColumnMapping::DEFAULT_OPERATORS_BY_COLUMN, ColumnMapping::FALLBACK_OPERATORS

Instance Method Summary collapse

Methods inherited from Base

#allowed_option_values, #array_field?, #clear_option_cache!, #default_value, #default_value=, #field_type_name, #optionable?

Instance Method Details

#cast(raw) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/typed_eav/field/decimal.rb', line 12

def cast(raw)
  return [nil, false] if raw.nil?

  result = BigDecimal(raw.to_s, exception: false)
  return [nil, !raw.to_s.strip.empty?] if result.nil?
  return [result, false] if precision_scale.blank?

  scale = Kernel.Integer(precision_scale, exception: false)
  return [result, false] unless scale && scale >= 0

  [result.round(scale), false]
end

#validate_typed_value(record, val) ⇒ Object



25
26
27
# File 'app/models/typed_eav/field/decimal.rb', line 25

def validate_typed_value(record, val)
  validate_range(record, val)
end