Module: FamilySearch::Gedcomx::SuperCoercion::InstanceMethods

Defined in:
lib/familysearch/gedcomx/super_coercion.rb

Instance Method Summary collapse

Instance Method Details

#[]=(key, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 40

def []=(key, value)
  if value && [Array,Hash].include?(value.class)
    into = self.class.key_coercion(key) || self.class.value_coercion(value)
    if value && into
      if into.class == Array && value.class == Array
        value = value.collect do |v| 
          if into[0].respond_to?(:coerce)
            into[0].coerce(v)
          else
            into[0].new(v)
          end
        end
      elsif into.class == Hash && value.class == Hash
        into_value = into['key']
        value = value.inject({}) do |h, (k, v)| 
          if into_value.respond_to?(:coerce)
            h[k] = into_value.coerce(v)
          else
            h[k] = into_value.new(v)
          end
          h
        end
      else
        if into.respond_to?(:coerce)
          value = into.coerce(value)
        else
          value = into.new(value)
        end
      end
    end
  end

  super(key, value)
end

#custom_writer(key, value) ⇒ Object



75
76
77
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 75

def custom_writer(key, value)
  self[key] = value
end

#replace(other_hash) ⇒ Object



79
80
81
82
83
# File 'lib/familysearch/gedcomx/super_coercion.rb', line 79

def replace(other_hash)
  (keys - other_hash.keys).each { |key| delete(key) }
  other_hash.each { |key, value| self[key] = value }
  self
end