Class: ActiveModel::Type::Document

Inherits:
Value
  • Object
show all
Defined in:
lib/active_model/type/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name: nil, cast_type: nil, collection: false, context: nil) ⇒ Document

Returns a new instance of Document.



13
14
15
16
17
18
# File 'lib/active_model/type/document.rb', line 13

def initialize(class_name: nil, cast_type: nil, collection: false, context: nil)
  @document_class = resolve_constant class_name, from: context if class_name
  @cast_type      = lookup_or_return cast_type if cast_type
  @collection     = collection
  @context        = context
end

Instance Attribute Details

#cast_typeObject (readonly)

Returns the value of attribute cast_type.



9
10
11
# File 'lib/active_model/type/document.rb', line 9

def cast_type
  @cast_type
end

#collectionObject (readonly)

Returns the value of attribute collection.



10
11
12
# File 'lib/active_model/type/document.rb', line 10

def collection
  @collection
end

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/active_model/type/document.rb', line 11

def context
  @context
end

#document_classObject (readonly)

Returns the value of attribute document_class.



9
10
11
# File 'lib/active_model/type/document.rb', line 9

def document_class
  @document_class
end

Instance Method Details

#cast(value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_model/type/document.rb', line 38

def cast(value)
  return unless value

  if collection?
    return value if value.respond_to? :document_class

    documents = value.map { |attributes| process attributes }

    collection_class.new(documents)
  else
    return value if value.respond_to? :id

    process value
  end
end

#changed_in_place?(old_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_model/type/document.rb', line 70

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

#collection?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/active_model/type/document.rb', line 20

def collection?
  collection
end

#collection_classObject



28
29
30
31
32
33
34
35
36
# File 'lib/active_model/type/document.rb', line 28

def collection_class
  return unless collection?

  if default_collection?
    @collection_class ||= ActiveModel::Embedding::Collection
  else
    @collection_class ||= resolve_constant collection, from: context
  end
end

#default_collection?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/active_model/type/document.rb', line 24

def default_collection?
  collection == true
end

#deserialize(json) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/active_model/type/document.rb', line 62

def deserialize(json)
  return unless json

  value = ActiveSupport::JSON.decode(json)

  cast value
end

#process(value) ⇒ Object



54
55
56
# File 'lib/active_model/type/document.rb', line 54

def process(value)
  cast_type ? cast_type.cast(value) : document_class.new(value)
end

#serialize(value) ⇒ Object



58
59
60
# File 'lib/active_model/type/document.rb', line 58

def serialize(value)
  value.to_json
end