Class: Arstotzka::FetcherBuilder Private

Inherits:
Object
  • Object
show all
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 = { 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

Constructor Details

#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

Parameters:

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

    options (see Options)



35
36
37
# File 'lib/arstotzka/fetcher_builder.rb', line 35

def initialize(options = {})
  @options = options
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 = {
  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 = {
  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 = { 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



152
153
154
155
156
157
158
# File 'lib/arstotzka/fetcher_builder.rb', line 152

def build(instance)
  fetcher_options = Arstotzka.config.options(
    options.merge(instance: instance)
  )

  Fetcher.new(fetcher_options)
end