Class: Arstotzka::FetcherBuilder Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/fetcher_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 for building fetcher that will be used to create instance fetchers

Examples:

Building a simple fetcher

class MyModel
  include Arstotzka

  attr_reader :json

  def initialize(json)
    @json = json
  end
end

options =  Arstotzka::Options.new(key: :id, path: :person)
builder = Arstotzka::FetcherBuilder.new(options)
instance = MyModel.new(
  person: {
    id: 101
  }
)

fetcher = builder.build(instance)

fetcher.fetch  # returns 101

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(options_hash = {}) ⇒ FetcherBuilder #initialize(options) ⇒ 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.

Creates an instance of Artotzka::FetcherBuilder

Overloads:



41
42
43
# File 'lib/arstotzka/fetcher_builder.rb', line 41

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

Instance Method Details

#build(instance) ⇒ Object

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.

Builds a fetcher responsible for fetchin a value

Examples:

Building a fetcher using full path

class MyModel
  include Arstotzka

  attr_reader :json

  def initialize(json)
    @json = json
  end
end

options =  Arstotzka::Options.new(
  key:       :player_ids,
  full_path: 'teams.players.person_id',
  flatten:   true,
  case:      :snake
)
builder = Arstotzka::FetcherBuilder.new(options)
hash = {
  teams: [
    {
      name: 'Team War',
      players: [
        { person_id: 101 },
        { person_id: 102 }
      ]
    }, {
      name: 'Team not War',
      players: [
        { person_id: 201 },
        { person_id: 202 }
      ]
    }
  ]
}
instance = MyModel.new(hash)

fetcher = builder.build(instance)

fetcher.fetch  # returns [101, 102, 201, 202]

Post processing results

class StarGazer
  include Arstotzka

  attr_reader :json

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

  private

  def only_yellow(stars)
    stars.select(&:yellow?)
  end
end

class Star
  attr_reader :name, :color

  def initialize(name:, color: 'yellow')
    @name = name
    @color = color
  end

  def yellow?
    color == 'yellow'
  end
end

hash = {
  teams: [
    {
      name: 'Team War',
      players: [
        { person_id: 101 },
        { person_id: 102 }
      ]
    }, {
      name: 'Team not War',
      players: [
        { person_id: 201 },
        { person_id: 202 }
      ]
    }
  ]
}

instance = StarGazer.new(hash)

options = Arstotzka::Options.new(
  key: :stars, klass: Star, after: :only_yellow
)

builder = Arstotzka::FetcherBuilder.new(options)
fetcher = builder.build(instance)

fetcher.fetch # returns [
              #   Star.new(name: 'Sun', color: 'yellow'),
              #   Star.new(name: 'HB0124-C', color: 'yellow'),
              # ]

Building a simple fetcher

class MyModel
  include Arstotzka

  attr_reader :json

  def initialize(json)
    @json = json
  end
end

options =  Arstotzka::Options.new(key: :id, path: :person)
builder = Arstotzka::FetcherBuilder.new(options)
instance = MyModel.new(
  person: {
    id: 101
  }
)

fetcher = builder.build(instance)

fetcher.fetch  # returns 101

Parameters:

  • instance (Object)

    object that includes Arstotzka

    instance should be able to provide the hash where values will be fetched from

Returns:

  • Arstotzka::Fetcher



158
159
160
# File 'lib/arstotzka/fetcher_builder.rb', line 158

def build(instance)
  Fetcher.new(options.merge(instance: instance))
end