Class: TypedEAV::FieldStorageContract

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_eav/field_storage_contract.rb

Overview

One field-owned seam for native typed-column storage behavior.

The contract intentionally delegates to the existing field type hooks so custom field authors keep the same public extension points while callers stop knowing which pieces belong together.

Direct Known Subclasses

CurrencyStorageContract

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ FieldStorageContract

Returns a new instance of FieldStorageContract.



10
11
12
# File 'lib/typed_eav/field_storage_contract.rb', line 10

def initialize(field)
  @field = field
end

Instance Method Details

#after_snapshot(value_record, change_type) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/typed_eav/field_storage_contract.rb', line 53

def after_snapshot(value_record, change_type)
  case change_type.to_sym
  when :create, :update
    value_columns.to_h { |column| [column.to_s, value_record[column]] }
  when :destroy
    {}
  else
    raise ArgumentError, "Unsupported change_type: #{change_type.inspect}"
  end
end

#apply_default(value_record) ⇒ Object



30
31
32
# File 'lib/typed_eav/field_storage_contract.rb', line 30

def apply_default(value_record)
  field.apply_default_to(value_record)
end

#before_snapshot(value_record, change_type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/typed_eav/field_storage_contract.rb', line 38

def before_snapshot(value_record, change_type)
  case change_type.to_sym
  when :create
    {}
  when :update
    value_columns.to_h do |column|
      [column.to_s, value_record.attribute_before_last_save(column.to_s)]
    end
  when :destroy
    value_columns.to_h { |column| [column.to_s, value_record[column]] }
  else
    raise ArgumentError, "Unsupported change_type: #{change_type.inspect}"
  end
end

#changed?(value_record) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/typed_eav/field_storage_contract.rb', line 34

def changed?(value_record)
  value_columns.any? { |column| value_record.saved_change_to_attribute?(column) }
end

#query_column(operator) ⇒ Object



18
19
20
# File 'lib/typed_eav/field_storage_contract.rb', line 18

def query_column(operator)
  field.class.operator_column(operator)
end

#read(value_record) ⇒ Object



22
23
24
# File 'lib/typed_eav/field_storage_contract.rb', line 22

def read(value_record)
  field.read_value(value_record)
end

#value_columnsObject



14
15
16
# File 'lib/typed_eav/field_storage_contract.rb', line 14

def value_columns
  field.class.value_columns
end

#write(value_record, casted) ⇒ Object



26
27
28
# File 'lib/typed_eav/field_storage_contract.rb', line 26

def write(value_record, casted)
  field.write_value(value_record, casted)
end