Class: Arstotzka::MethodBuilder Private

Inherits:
Sinclair
  • Object
show all
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 = { full_path: 'name.first' }
builder = Arstotzka::MethodBuilder.new([ :first_name ], MyModel, options)
builder.build

instance.first_name # returns 'John'

options = { 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

Constructor Details

#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

Parameters:

  • attr_names (Array<Symbol>)

    list of attributes to be fetched from the hash/json

  • klass (Class)

    class to receive the methods (using Sinclair)

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

    hash containing extra options

See Also:



50
51
52
53
54
55
56
# File 'lib/arstotzka/method_builder.rb', line 50

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

  @attr_names = attr_names
  init
end