Class: Arstotzka::HashReader Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/hash_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 json / hash from instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#iniitalize(options_hash = {}) ⇒ HashReader #iniitalize(options) ⇒ HashReader

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.

Returns a new instance of HashReader

Overloads:



17
18
19
# File 'lib/arstotzka/hash_reader.rb', line 17

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

Instance Attribute Details

#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.



75
76
77
# File 'lib/arstotzka/hash_reader.rb', line 75

def options
  @options
end

Instance Method Details

#hashHash

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.

Retrieves the hash to be crawled from the instance

Examples:

Simple Usage

class Dummy
  def initialize(json = {})
    @json = json
  end

  private

  attr_reader :json
end

hash = { key: 'value' }
instance = Dummy.new(hash)

reader = Arstotzka::HashReader.new(
  instance: instance
)

reader.hash # returns { key: 'value' }

When fetching from class variable

class ClassVariable
  def self.json=(json)
    @@json = json
  end
end

hash = { key: 'value' }
ClassVariable.json = hash

instance = ClassVariable.new

reader = Arstotzka::HashReader.new(
  instance: instance, json: :@@json
)

reader.hash # returns { key: 'value' }

Returns:

  • (Hash)


62
63
64
65
66
67
68
69
70
71
# File 'lib/arstotzka/hash_reader.rb', line 62

def hash
  @hash ||= case json.to_s
            when /^@@.*/
              instance.class.class_variable_get(json)
            when /^@.*/
              instance.instance_variable_get(json)
            else
              instance.send(json)
            end
end