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) (defaults to: {})

    fetcher options

Returns:

  • (Artotzka::FetcherBuilder)


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

def add_fetcher(attribute, options = {})
  fetcher_builders[attribute] = 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

Parameters:

  • attr_names (Array<String,Symbol>)

    attributes being exposed

  • options_hash (Hash)

    exposing options

Options Hash (**options_hash):

  • after (String, Symbol)

    instance method to be called on the returning value returned by Arstotzka::Crawler before being returned by Fetcher. The result of the method call will be the actual value (used by PostProcessor)

  • after_each (String, Symbol)

    instance method to be called on each element found when crawling the hash. If an array should be returned, after_each will be called on each individual elements (even nil elements) while after is only called on the collection. (used by Wrapper)

  • cached (Boolean, Symbol)

    Flag to ensure there value is cached after first fetching. (used by Fetcher::Cache)

    • false/nil : no cache, fetcher will always look in the hash for the value

    • true : Simple cache, nil values are always considered not cached

    • :full : cache even nil values

  • case (Symbol)

    case type of the keys in the hash (used by KeyChanger)

    • :snake : snake_cased keys

    • :lower_camel : lowerCamelCased keys

    • :upper_camel : UperCamelCased keys

  • compact (Boolean)

    flag signallying if nil values should be removed of array (applying Array#compact) (used by Arstotzka::Crawler)

  • default (Object)

    default value to be returned when failing to fetch a value (used by Arstotzka::Crawler)

  • flatten (Boolean)

    flag signallying if multi levels arrays should be flattened to one level array (applying Array#flatten) (used by PostProcessor)

  • full_path (String)

    full path of keys to be used in when not wanting to have the attribute defined as key. (eg. ‘person.name’) (used by Arstotzka::Crawler)

  • klass (Class)

    class to wrap the value (used by Wrapper)

  • json (String, Symbol)

    name of the method or variable used to fetch the hash to be crawled (eg. :hash, :@hash, “@@config”) (used by HashReader)

  • path (String, Symbol)

    base path to be crawled. (used by Arstotzka::Crawler)

  • type (String, Symbol)

    type to cast the value. The possible type_cast is defined by TypeCast (used by Wrapper)

    • integer

    • string

    • float

Returns:

  • (Array<Sinclair::MethodDefinition>)

See Also:



161
162
163
# File 'lib/arstotzka/class_methods.rb', line 161

def expose(*attr_names, **options_hash)
  MethodBuilder.new(attr_names.map(&:to_sym), self, options_hash).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:

Raises:



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

def fetcher_for(attribute, instance)
  return builder_for(attribute).build(instance) if fetcher_for?(attribute)

  raise Exception::FetcherBuilderNotFound.new(attribute, self)
end