Class: ArDocStore::Types::EmbedsMany

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name) ⇒ EmbedsMany

Returns a new instance of EmbedsMany.



8
9
10
# File 'lib/ar_doc_store/types/embeds_many.rb', line 8

def initialize(class_name)
  @class_name = class_name
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



6
7
8
# File 'lib/ar_doc_store/types/embeds_many.rb', line 6

def class_name
  @class_name
end

Instance Method Details

#cast(values) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ar_doc_store/types/embeds_many.rb', line 12

def cast(values)
  @class_name = @class_name.constantize if class_name.respond_to?(:constantize)
  collection = EmbeddedCollection.new
  values && values.each do |value|
    collection << if value.nil?
                    value
                  elsif value.kind_of?(class_name)
                    value
                  elsif value.respond_to?(:to_hash)
                    class_name.new value
                  else
                    nil
                  end
  end
  collection
end

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ar_doc_store/types/embeds_many.rb', line 49

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

#deserialize(value) ⇒ Object



45
46
47
# File 'lib/ar_doc_store/types/embeds_many.rb', line 45

def deserialize(value)
  cast(value)
end

#serialize(values) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ar_doc_store/types/embeds_many.rb', line 29

def serialize(values)
  return if values.nil?

  if values.respond_to?(:each)
    values.map { |value|
      if value.nil?
        nil
      elsif value.kind_of?(class_name)
        value.serializable_hash
      else
        cast(value).serializable_hash
      end
    }.compact
  end
end