Class: Arstotzka::Reader Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/reader.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Reads a value from a hash using the keys as list of keys

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(options_hash = {}) ⇒ Arstotzka::Reader #initialize(options) ⇒ Arstotzka::Reader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance of Reader

Overloads:

  • #initialize(options_hash = {}) ⇒ Arstotzka::Reader

    Parameters:

    • options_hash (Hash) (defaults to: {})

      options of initialization

    Options Hash (options_hash):

    • keys (Array)

      keys of keys broken down as array

    • 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

  • #initialize(options) ⇒ Arstotzka::Reader

    Parameters:



23
24
25
# File 'lib/arstotzka/reader.rb', line 23

def initialize(options_hash = {})
  self.options = options_hash
end

Instance Method Details

#read(hash, index) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads the value of one key in the hash

Examples:

hash = {
  full_name: 'John',
  'Age' => 23,
  'carCollection' => [
    { maker: 'Ford', 'model' => 'Model A' },
    { maker: 'BMW', 'model' => 'Jetta' }
  ]
}

reader = Arstotzka::Reader.new(full_path: 'person.full_name', case: :snake)
reader.read(hash, 1) # returns 'John'
reader = Arstotzka::Reader.new(full_path: 'person.age', case: :upper_camel)
reader.read(hash, 1) # returns 23
reader = Arstotzka::Reader.new(full_path: 'person.car_collection.model', case: :snake)
reader.read(hash, 1) # raises {Arstotzka::Exception::KeyNotFound}
reader = Arstotzka::Reader.new(full_path: 'person.car_collection.model', case: :lower_camel)
reader.read(hash, 1) # returns [
                     #   { maker: 'Ford', 'model' => 'Model A' },
                     #   { maker: 'BMW', 'model' => 'Jetta' }
                     # ]

Parameters:

  • hash (Hash)

    hash to be read

  • index (Integer)

    Index of the key (in keys) to be used

Returns:

  • (Object)

    The value fetched from the hash



61
62
63
64
65
# File 'lib/arstotzka/reader.rb', line 61

def read(hash, index)
  key = keys[index]

  KeyReader.new(hash, key, options).read
end