lib/arstotzka/reader.rb in arstotzka-1.0.4 vs lib/arstotzka/reader.rb in arstotzka-1.1.0

- old
+ new

@@ -1,30 +1,37 @@ # frozen_string_literal: true module Arstotzka # @api private # - # Reads a value from a hash using the path as list of keys + # Reads a value from a hash using the keys as list of keys class Reader + include Base + # Creates a new instance of Reader # - # @param path [Array] path of keys broken down as array - # @param case_type [Symbol] Case of the keys - # - lower_camel: keys in the hash are lowerCamelCase - # - upper_camel: keys in the hash are UpperCamelCase - # - snake: keys in the hash are snake_case + # @overload initialize(options_hash={}) + # @param options_hash [Hash] options of initialization + # @option options_hash keys [Array] keys of keys broken down as array + # @option options_hash case [Symbol] Case of the keys + # - lower_camel: keys in the hash are lowerCamelCase + # - upper_camel: keys in the hash are UpperCamelCase + # - snake: keys in the hash are snake_case + # @overload initialize(options) + # @param options [Arstotzka::Options] options of initialization object # - # @return [Aristotzka::Reader] - def initialize(path:, case_type:) - @case_type = case_type - @path = path.map(&method(:change_case)) + # @return [Arstotzka::Reader] + def initialize(options_hash = {}) + self.options = options_hash + + @keys = options.keys.map(&method(:change_case)) end # Reads the value of one key in the hash # # @param hash [Hash] hash to be read - # @param index [Integer] Index of the key (in path) to be used + # @param index [Integer] Index of the key (in keys) to be used # # @return [Object] The value fetched from the hash # # @example # hash = { @@ -34,51 +41,51 @@ # { maker: 'Ford', 'model' => 'Model A' }, # { maker: 'BMW', 'model' => 'Jetta' } # ] # } # - # reader = Arstotzka::Reader.new(%w(person full_name), case_type: :snake) + # reader = Arstotzka::Reader.new(keys: %w(person full_name), case: :snake) # reader.read(hash, 1) # returns 'John' # # @example - # reader = Arstotzka::Reader.new(%w(person age), case_type: :upper_camel) + # reader = Arstotzka::Reader.new(keys: %w(person age), case: :upper_camel) # reader.read(hash, 1) # returns 23 # # @example - # reader = Arstotzka::Reader.new(%w(person car_collection model), case_type: :snake) + # reader = Arstotzka::Reader.new(keys: %w(person car_collection model), case: :snake) # reader.read(hash, 1) # raises {Arstotzka::Exception::KeyNotFound} # # @example - # reader = Arstotzka::Reader.new(%w(person car_collection model), case_type: :lower_camel) + # reader = Arstotzka::Reader.new(keys: %w(person car_collection model), case: :lower_camel) # reader.read(hash, 1) # returns [ # # { maker: 'Ford', 'model' => 'Model A' }, # # { maker: 'BMW', 'model' => 'Jetta' } # # ] def read(hash, index) - key = path[index] + key = keys[index] check_key!(hash, key) hash.key?(key) ? hash[key] : hash[key.to_sym] end # @private # - # Checks if index is within path range + # Checks if index is within keys range # # @example - # reader = Arstotzka::Reader.new(%w(person full_name), case_type: :snake) + # reader = Arstotzka::Reader.new(%w(person full_name), case: :snake) # reader.read(hash, 1) # returns false # reader.read(hash, 2) # returns true def ended?(index) - index >= path.size + index >= keys.size end private # @private - attr_reader :path, :case_type + attr_reader :keys, :options # @private # # Checks if a hash contains or not the key # @@ -113,17 +120,19 @@ # @private # # Transforms the key to have the correct case # - # the possible case_types (instance attribute) are + # 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 case_type + case options.case when :lower_camel key.camelize(:lower) when :upper_camel key.camelize(:upper) when :snake