Class: Arstotzka::KeyReader Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/key_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.

Class responsible for reading values from a hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(hash, base_key, options_hash = {}) ⇒ KeyReader

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

Parameters:

  • hash (Hash)

    Hash where the key will be found

  • base_key (String)

    The key to be checked (before case change)



15
16
17
18
19
20
# File 'lib/arstotzka/key_reader.rb', line 15

def initialize(hash, base_key, options_hash = {})
  self.options = options_hash

  @hash     = hash
  @base_key = base_key
end

Instance Attribute Details

#base_keyObject (readonly, private)

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.



50
51
52
# File 'lib/arstotzka/key_reader.rb', line 50

def base_key
  @base_key
end

#hashObject (readonly, private)

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.



50
51
52
# File 'lib/arstotzka/key_reader.rb', line 50

def hash
  @hash
end

#optionsObject (readonly, private)

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.



50
51
52
# File 'lib/arstotzka/key_reader.rb', line 50

def options
  @options
end

Instance Method Details

#readObject

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 value from hash key

Examples:

Simple usage

hash = { theKey: 'value' }
key  = { 'the_key' }
reader = Arstotzka::KeyReader.new(hash, key)

reader.read # returns 'value'
hash = { 'the_key' => 'value' }
key  = 'TheKey'
reader = Arstotzka::KeyReader.new(hash, key)

reader.read # returns 'value'

Returns:

  • (Object)

Raises:

  • Arstotzka::Exception::KeyNotFound



42
43
44
45
46
# File 'lib/arstotzka/key_reader.rb', line 42

def read
  raise Exception::KeyNotFound unless key?

  hash.key?(key) ? hash[key] : hash[key.to_sym]
end