Class: Anchormodel::ActiveModelTypeValueSingle

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/anchormodel/active_model_type_value_single.rb

Overview

Direct Known Subclasses

ActiveModelTypeValueMulti

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ ActiveModelTypeValueSingle

Returns a new instance of ActiveModelTypeValueSingle.



5
6
7
8
# File 'lib/anchormodel/active_model_type_value_single.rb', line 5

def initialize(attribute)
  super()
  @attribute = attribute
end

Instance Attribute Details

#attributeObject (readonly)



3
4
5
# File 'lib/anchormodel/active_model_type_value_single.rb', line 3

def attribute
  @attribute
end

Instance Method Details

#cast(value) ⇒ Object

This converts DB or input to an Anchormodel instance



15
16
17
18
19
# File 'lib/anchormodel/active_model_type_value_single.rb', line 15

def cast(value)
  value = value.presence
  return value if value.is_a?(@attribute.anchormodel_class)
  return @attribute.anchormodel_class.find(value)
end

#changed_in_place?(raw_old_value, value) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/anchormodel/active_model_type_value_single.rb', line 32

def changed_in_place?(raw_old_value, value)
  old_value = deserialize(raw_old_value)
  old_value != value
end

#serializable?(value) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/anchormodel/active_model_type_value_single.rb', line 27

def serializable?(value)
  return value.all? { |v| scalar_serializable?(v) } if value.is_a?(Array)
  scalar_serializable?(value)
end

#serialize(value) ⇒ Object

This converts an Anchormodel instance to string for DB



22
23
24
25
# File 'lib/anchormodel/active_model_type_value_single.rb', line 22

def serialize(value)
  return value.map { |v| serialize_scalar(v) } if value.is_a?(Array)
  serialize_scalar(value)
end

#typeObject



10
11
12
# File 'lib/anchormodel/active_model_type_value_single.rb', line 10

def type
  :anchormodel
end