Module: ActiveRecord::Remote::Helpers::SerializationHelper

Included in:
Base
Defined in:
lib/active_record/remote/helpers/serialization_helper.rb

Instance Method Summary collapse

Instance Method Details

#_attribute_name(attribute) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/active_record/remote/helpers/serialization_helper.rb', line 19

def _attribute_name(attribute)
  if !!attribute.options[:as]
    attribute.options[:as]
  elsif !!attribute.options[:strict]
    attribute.name
  else
    attribute.name.upcase
  end
end

#_serialize(serialized, attribute = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_record/remote/helpers/serialization_helper.rb', line 29

def _serialize(serialized, attribute = nil)
  if serialized.respond_to?(:serializable_hash)
    serialized.serializable_hash
  else
    case serialized
    when Array
      serialized.map { |attr| _serialize(attr) }
    when BigDecimal
      serialized.to_s("F")
    when Hash
      Hash[
        serialized.map do |k, v|
          k = attribute.options[:strict] ? k : k.upcase
          [k, v]
        end
      ]
    else
      serialized
    end
  end
end

#serializable_hashObject



5
6
7
8
9
10
11
# File 'lib/active_record/remote/helpers/serialization_helper.rb', line 5

def serializable_hash
  Hash.new.tap do |attribute_hash|
    attribute_set.each do |attribute|
      serialize_attribute(attribute_hash, attribute)
    end
  end
end

#serialize_attribute(attribute_hash, attribute) ⇒ Object



13
14
15
16
17
# File 'lib/active_record/remote/helpers/serialization_helper.rb', line 13

def serialize_attribute(attribute_hash, attribute)
  return if attributes[attribute.name].nil?
  name = _attribute_name(attribute)
  attribute_hash[name] = _serialize(attributes[attribute.name], attribute)
end