Method: Attributor::Hash#get

Defined in:
lib/attributor/types/hash.rb

#get(key, context: generate_subcontext(Attributor::DEFAULT_ROOT_CONTEXT, key)) ⇒ Object

Raises:



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/attributor/types/hash.rb', line 323

def get(key, context: generate_subcontext(Attributor::DEFAULT_ROOT_CONTEXT, key))
  key = self.class.key_attribute.load(key, context)

  return self.get_generic(key, context) if self.class.keys.empty?
  value = @contents[key]

  # FIXME: getting an unset value here should not force it in the hash
  if (attribute = self.class.keys[key])
    loaded_value = attribute.load(value, context)
    return nil if loaded_value.nil?
    return self[key] = loaded_value
  end

  if self.class.options[:case_insensitive_load]
    key = self.class.insensitive_map[key.downcase]
    return get(key, context: context)
  end

  if self.class.options[:allow_extra]
    return @contents[key] = self.class.value_attribute.load(value, context) if self.class.extra_keys.nil?
    extra_keys_key = self.class.extra_keys

    if @contents.key? extra_keys_key
      return @contents[extra_keys_key].get(key, context: context)
    end

  end

  raise LoadError, "Unknown key received: #{key.inspect} for #{Attributor.humanize_context(context)}"
end