Class: ActiveRecord::AttributeSet::YAMLEncoder

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(default_types) ⇒ YAMLEncoder

:nodoc:



6
7
8
# File 'lib/active_record/attribute_set/yaml_encoder.rb', line 6

def initialize(default_types)
  @default_types = default_types
end

Instance Method Details

#decode(coder) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/attribute_set/yaml_encoder.rb', line 20

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



10
11
12
13
14
15
16
17
18
# File 'lib/active_record/attribute_set/yaml_encoder.rb', line 10

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