lib/arstotzka/reader.rb in arstotzka-1.3.1 vs lib/arstotzka/reader.rb in arstotzka-1.3.2
- old
+ new
@@ -21,11 +21,11 @@
#
# @return [Arstotzka::Reader]
def initialize(options_hash = {})
self.options = options_hash
- @keys = options.keys.map(&method(:change_case))
+ @keys = options.keys
end
# Reads the value of one key in the hash
#
# @param hash [Hash] hash to be read
@@ -61,13 +61,11 @@
# # { maker: 'BMW', 'model' => 'Jetta' }
# # ]
def read(hash, index)
key = keys[index]
- check_key!(hash, key)
-
- hash.key?(key) ? hash[key] : hash[key.to_sym]
+ KeyReader.new(hash, key, options).read
end
# @private
#
# Checks if index is within keys range
@@ -82,63 +80,7 @@
private
# @private
attr_reader :keys, :options
-
- # @private
- #
- # Checks if a hash contains or not the key
- #
- # if the key is not found, an execption is raised
- #
- # @raise Arstotzka::Exception::KeyNotFound
- #
- # @return [NilClass]
- #
- # @see #key?
- def check_key!(hash, key)
- return if key?(hash, key)
- raise Exception::KeyNotFound
- end
-
- # @private
- #
- # Checks if a hash contains or not the key
- #
- # The check first happens using String key and,
- # in case of not found, searches as symbol
- #
- # @param [Hash] hash Hash where the key will be found
- # @param [String] key The key to be checked
- #
- # @return [Boolean]
- #
- # @see #check_key!
- def key?(hash, key)
- hash&.key?(key) || hash&.key?(key.to_sym)
- end
-
- # @private
- #
- # Transforms the key to have the correct case
- #
- # the possible cases (instance attribute) are
- # - lower_camel: for cammel case with first letter lowercase
- # - upper_camel: for cammel case with first letter uppercase
- # - snake: for snake case
- #
- # @param [String] key the key to be transformed
- #
- # @return [String] the string transformed
- def change_case(key)
- case options.case
- when :lower_camel
- key.camelize(:lower)
- when :upper_camel
- key.camelize(:upper)
- when :snake
- key.underscore
- end
- end
end
end