Module: Arstotzka::ClassMethods

Defined in:
lib/arstotzka/class_methods.rb

Overview

As Arstotzka extends ActiveSupport::Concern, Arstotzka::ClassMethods define methods that will be available when defining a class that includes Arstotka

Instance Method Summary collapse

Instance Method Details

#add_fetcher(attribute, options) ⇒ Artotzka::FetcherBuilder

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.

Create builder that will be used to create Fetchers

Parameters:

  • attribute (Symbol, String)

    attribute key

  • options (Arstotzka::Options)

    fetcher options

Returns:

  • (Artotzka::FetcherBuilder)


15
16
17
# File 'lib/arstotzka/class_methods.rb', line 15

def add_fetcher(attribute, options)
  fetcher_builders[attribute.to_sym] = FetcherBuilder.new(options.merge(key: attribute))
end

#expose(*attr_names, **options_hash) ⇒ Array<Sinclair::MethodDefinition>

Expose a field from the json/hash as a method

Examples:

class MyModel
  include Arstotzka

  attr_reader :json

  expose :first_name, full_path: 'name.first'
  expose :age, 'cars', type: :integer

  def initialize(json)
    @json = json
  end
end

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

instance.first_name # returns 'John'
instance.age        # returns 20
instance.cars       # returns 2

Returns:

  • (Array<Sinclair::MethodDefinition>)

See Also:



70
71
72
73
# File 'lib/arstotzka/class_methods.rb', line 70

def expose(*attr_names, **options_hash)
  options = Options.new(options_hash.symbolize_keys)
  MethodBuilder.new(attr_names, self, options).build
end

#fetcher_for(attribute, instance) ⇒ Arstotzka::Fetcher

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.

Return the fetcher for an attribute and instance

a new fetcher is built everytime this method is called

Parameters:

  • attribute (Symbol, String)

    Name of method that will use this Fetcher

  • instance (Object)

    instance that will contain the Hash needed by fetcher

Returns:



29
30
31
# File 'lib/arstotzka/class_methods.rb', line 29

def fetcher_for(attribute, instance)
  fetcher_builders[attribute.to_sym].build(instance)
end