Class: StoreModel::Types::ManyBase

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/store_model/types/many_base.rb

Overview

Implements ActiveModel::Type::Value type for handling an array of StoreModel::Model

Direct Known Subclasses

Many, ManyPolymorphic

Instance Method Summary collapse

Instance Method Details

#cast_value(value) ⇒ Object

Casts value from DB or user to StoreModel::Model instance

Parameters:

  • value (Object)

    a value to cast

Returns:

  • StoreModel::Model



22
23
24
25
26
27
28
29
30
# File 'lib/store_model/types/many_base.rb', line 22

def cast_value(value)
  case value
  when String then decode_and_initialize(value)
  when Array then ensure_model_class(value)
  when nil then value
  else
    raise_cast_error(value)
  end
end

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Determines whether the mutable value has been modified since it was read

Parameters:

  • raw_old_value (Object)

    old value

  • new_value (Object)

    new value

Returns:

  • (Boolean)


53
54
55
# File 'lib/store_model/types/many_base.rb', line 53

def changed_in_place?(raw_old_value, new_value)
  cast_value(raw_old_value) != new_value
end

#serialize(value) ⇒ String

Casts a value from the ruby type to a type that the database knows how to understand.

Parameters:

  • value (Object)

    value to serialize

Returns:

  • (String)

    serialized value



38
39
40
41
42
43
44
45
# File 'lib/store_model/types/many_base.rb', line 38

def serialize(value)
  case value
  when Array
    ActiveSupport::JSON.encode(value)
  else
    super
  end
end

#typeSymbol

Returns type

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/store_model/types/many_base.rb', line 13

def type
  raise NotImplementedError
end