Class: Serega::AttributeValueResolvers::HashAccessKeyword

Inherits:
Object
  • Object
show all
Defined in:
lib/serega/attribute_value_resolvers/hash_access.rb

Overview

Value resolver for attributes with the :hash_access option

Instance Method Summary collapse

Constructor Details

#initialize(name, mode, allow_missing_key) ⇒ HashAccessKeyword

Returns a new instance of HashAccessKeyword.



53
54
55
56
# File 'lib/serega/attribute_value_resolvers/hash_access.rb', line 53

def initialize(name, mode, allow_missing_key)
  @key = (mode == :symbol) ? name.to_sym : name.to_s
  @allow_missing_key = allow_missing_key
end

Instance Method Details

#call(object) ⇒ Object

Reads the key from the record

Parameters:

  • object (Object)

    serialized object or delegation step value

Returns:

  • (Object)

    the value found



64
65
66
67
68
69
70
71
72
73
# File 'lib/serega/attribute_value_resolvers/hash_access.rb', line 64

def call(object)
  return object[@key] if @allow_missing_key

  object.fetch(@key) do
    default = object[@key]
    next default unless default.nil?

    raise KeyError.new("key not found: #{@key.inspect}", key: @key, receiver: object)
  end
end