Module: Hashie::Extensions::Coercion::InstanceMethods

Defined in:
lib/hashie/extensions/coercion.rb

Instance Method Summary collapse

Instance Method Details

#custom_writer(key, value, _convert = true) ⇒ Object



47
48
49
# File 'lib/hashie/extensions/coercion.rb', line 47

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

#replace(other_hash) ⇒ Object



51
52
53
54
55
# File 'lib/hashie/extensions/coercion.rb', line 51

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

#set_value_with_coercion(key, value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hashie/extensions/coercion.rb', line 33

def set_value_with_coercion(key, value)
  into = self.class.key_coercion(key) || self.class.value_coercion(value)

  unless value.nil? || into.nil?
    begin
      value = self.class.fetch_coercion(into).call(value)
    rescue NoMethodError, TypeError => e
      raise CoercionError, "Cannot coerce property #{key.inspect} from #{value.class} to #{into}: #{e.message}"
    end
  end

  set_value_without_coercion(key, value)
end