Class: Serega::AttributeValueResolvers::HashAccessKeyword
- Inherits:
-
Object
- Object
- Serega::AttributeValueResolvers::HashAccessKeyword
- Defined in:
- lib/serega/attribute_value_resolvers/hash_access.rb
Overview
Value resolver for attributes with the :hash_access option
Instance Method Summary collapse
-
#call(object) ⇒ Object
Reads the key from the record.
-
#initialize(name, mode, allow_missing_key) ⇒ HashAccessKeyword
constructor
A new instance of HashAccessKeyword.
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
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 |