Sha256: 995c9dd96389462160b5232c17ee9732563e8c87576352a900cda8b5bcf3446a
Contents?: true
Size: 1.69 KB
Versions: 2
Compression:
Stored size: 1.69 KB
Contents
# frozen_string_literal: true module Arstotzka # @api private # # Class responsible for reading values from a hash class KeyReader include Base # Creates a new instance of Reader # # @param [Hash] hash Hash where the key will be found # @param [String] base_key The key to be checked # (before case change) def initialize(hash, base_key, options_hash = {}) self.options = options_hash @hash = hash @base_key = base_key end # Reads value from hash key # # @raise Arstotzka::Exception::KeyNotFound # # @return [Object] # # @example Simple usage # hash = { theKey: 'value' } # key = { 'the_key' } # reader = Arstotzka::KeyReader.new(hash, key) # # reader.read # returns 'value' # # @example # hash = { 'the_key' => 'value' } # key = 'TheKey' # reader = Arstotzka::KeyReader.new(hash, key) # # reader.read # returns 'value' # def read raise Exception::KeyNotFound unless key? hash.key?(key) ? hash[key] : hash[key.to_sym] end private attr_reader :hash, :base_key, :options delegate :key, to: :key_changer # @private # # Checks if hash contains or not the key # # The check first happens using String key and, # in case of not found, searches as symbol # # @return [Boolean] # # @see #check_key! def key? return unless hash hash.key?(key) || hash.key?(key.to_sym) end # @private # # Returns key changer for getting the correct key # # @return [KeyChanger] def key_changer @key_changer ||= KeyChanger.new(base_key, options) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
arstotzka-1.4.2 | lib/arstotzka/key_reader.rb |
arstotzka-1.4.1 | lib/arstotzka/key_reader.rb |