Class: Arstotzka::MethodBuilder Private

Inherits:
Sinclair
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/method_builder.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 to orchestrate the addtion of method that will crawl the hash for value

Examples:

class MyModel
  include Arstotzka

  attr_reader :json

  def initialize(json)
    @json = json
  end
end

instance = MyModel.new(
  'name' => { first: 'John', last: 'Williams' },
  :age => '20',
  'cars' => 2.0
)

options = Arstotzka::Options.new(full_path: 'name.first')
builder = Arstotzka::MethodBuilder.new([ :first_name ], MyModel, options)
builder.build

instance.first_name # returns 'John'

options = Arstotzka::Options.new(type: :integer)
builder = Arstotzka::MethodBuilder.new([ :age, 'cars' ], MyModel, options)
builder.build

instance.age  # returns 20
instance.cars # returns 2

See Also:

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(attr_names, klass, options_hash = {}) ⇒ MethodBuilder #initialize(attr_names, klass, options) ⇒ MethodBuilder

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 new instance of Arstotzka::MethodBuilder

Overloads:

  • #initialize(attr_names, klass, options_hash = {}) ⇒ MethodBuilder

    Parameters:

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

      hash containing extra options

  • #initialize(attr_names, klass, options) ⇒ MethodBuilder

    Parameters:

Parameters:

  • attr_names (Array)

    list of attributes to be fetched from the hash/json

  • klass (Class)

    class to receive the methods (using Sinclair)

See Also:



56
57
58
59
60
61
62
# File 'lib/arstotzka/method_builder.rb', line 56

def initialize(attr_names, klass, options = {})
  super(klass)
  self.options = options

  @attr_names = attr_names
  init
end

Instance Method Details

#attr_fetcher(attribute) ⇒ String (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.

Returns the code needed to initialize fetcher

Parameters:

  • attribute (String, Symbol)

    name of method / attribute

Returns:

  • (String)

    method code

See Also:

  • Sinclair
  • Artotzka::Fetcher


103
104
105
106
107
108
109
# File 'lib/arstotzka/method_builder.rb', line 103

def attr_fetcher(attribute)
  <<-CODE
    begin
      self.class.fetcher_for(:#{attribute}, self).fetch
    end
  CODE
end

#cached_fetcher(attribute) ⇒ String (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.

Returns the code needed to initialize a fetche and cache it

Parameters:

  • attribute (String, Symbol)

    name of method / attribute

Returns:

  • (String)

    method code

See Also:



118
119
120
121
122
# File 'lib/arstotzka/method_builder.rb', line 118

def cached_fetcher(attribute)
  <<-CODE
    @#{attribute} ||= #{attr_fetcher(attribute)}
  CODE
end