Class: TypedEAV::Field::Decimal

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

Overview

Decimal-typed field with optional min/max guards and optional ‘precision_scale` rounding. Declares its own `decimal_value` storage and `:min`/`:max`/`:precision_scale` `store_accessor`; the `validate :max, comparison:` macro guards against inverted bounds at field-save. `validate_range` is inherited from `Field::RangeBounded`. `Field::Percentage` extends this class to add the 0..1 invariant (chain depth becomes `Percentage < Decimal < RangeBounded < Base`).

Direct Known Subclasses

Percentage

Constant Summary

Constants inherited from Base

Base::RESERVED_NAMES

Constants included from TypedStorage

TypedStorage::DEFAULT_OPERATORS_BY_COLUMN, TypedStorage::FALLBACK_OPERATORS

Instance Method Summary collapse

Methods inherited from Base

#allowed_option_values, #array_field?, #backfill_default!, #clear_option_cache!, #default_value, #default_value=, export_schema, #field_type_name, import_schema, #insert_at, #move_higher, #move_lower, #move_to_bottom, #move_to_top, #optionable?

Methods included from TypedStorage

#after_snapshot, #apply_default, #before_snapshot, #read_value, #value_changed?, #write_value

Instance Method Details

#cast(raw) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/typed_eav/field/decimal.rb', line 20

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



33
34
35
# File 'app/models/typed_eav/field/decimal.rb', line 33

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