Class: ActiveModel::AttributeSet::YAMLEncoder

Inherits:
Object
  • Object
show all
Defined in:
activemodel/lib/active_model/attribute_set/yaml_encoder.rb

Overview

Attempts to do more intelligent YAML dumping of an ActiveModel::AttributeSet to reduce the size of the resulting string

Instance Method Summary collapse

Constructor Details

#initialize(default_types) ⇒ YAMLEncoder

:nodoc:



8
9
10
# File 'activemodel/lib/active_model/attribute_set/yaml_encoder.rb', line 8

def initialize(default_types)
  @default_types = default_types
end

Instance Method Details

#decode(coder) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'activemodel/lib/active_model/attribute_set/yaml_encoder.rb', line 22

def decode(coder)
  if coder["attributes"]
    coder["attributes"]
  else
    attributes_hash = Hash[coder["concise_attributes"].map do |attr|
      if attr.type.nil?
        attr = attr.with_type(default_types[attr.name])
      end
      [attr.name, attr]
    end]
    AttributeSet.new(attributes_hash)
  end
end

#encode(attribute_set, coder) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'activemodel/lib/active_model/attribute_set/yaml_encoder.rb', line 12

def encode(attribute_set, coder)
  coder["concise_attributes"] = attribute_set.each_value.map do |attr|
    if attr.type.equal?(default_types[attr.name])
      attr.with_type(nil)
    else
      attr
    end
  end
end