Class: ActiveEntity::Type::Modifiers::Array

Inherits:
ActiveModel::Type::Value
  • Object
show all
Includes:
ActiveModel::Type::Helpers::Mutable
Defined in:
lib/active_entity/type/modifiers/array.rb

Overview

:nodoc:

Direct Known Subclasses

ArrayWithoutBlank

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subtype, delimiter = ",") ⇒ Array

Returns a new instance of Array.



12
13
14
15
# File 'lib/active_entity/type/modifiers/array.rb', line 12

def initialize(subtype, delimiter = ",")
  @subtype = subtype
  @delimiter = delimiter
end

Instance Attribute Details

#delimiterObject (readonly)

Returns the value of attribute delimiter.



9
10
11
# File 'lib/active_entity/type/modifiers/array.rb', line 9

def delimiter
  @delimiter
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



9
10
11
# File 'lib/active_entity/type/modifiers/array.rb', line 9

def subtype
  @subtype
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
45
46
# File 'lib/active_entity/type/modifiers/array.rb', line 42

def ==(other)
  other.is_a?(Array) &&
    subtype == other.subtype &&
    delimiter == other.delimiter
end

#cast(value) ⇒ Object



26
27
28
29
30
31
# File 'lib/active_entity/type/modifiers/array.rb', line 26

def cast(value)
  if value.is_a?(::String)
    value = value.split(@delimiter)
  end
  type_cast_array(value, :cast)
end

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:



52
53
54
# File 'lib/active_entity/type/modifiers/array.rb', line 52

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

#deserialize(value) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/active_entity/type/modifiers/array.rb', line 17

def deserialize(value)
  case value
  when ::String
    type_cast_array(value.split(@delimiter), :deserialize)
  else
    super
  end
end

#force_equality?(value) ⇒ Boolean

Returns:



56
57
58
# File 'lib/active_entity/type/modifiers/array.rb', line 56

def force_equality?(value)
  value.is_a?(::Array)
end

#map(value, &block) ⇒ Object



48
49
50
# File 'lib/active_entity/type/modifiers/array.rb', line 48

def map(value, &block)
  value.map(&block)
end

#serialize(value) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/active_entity/type/modifiers/array.rb', line 33

def serialize(value)
  if value.is_a?(::Array)
    casted_values = type_cast_array(value, :serialize)
    casted_values.join(@delimiter)
  else
    super
  end
end