Class: Carbon::Registry::Registrar

Inherits:
Object
  • Object
show all
Defined in:
lib/carbon/registry.rb

Overview

Used internally when instance-eval'ing the emit_as DSL.

Instance Method Summary collapse

Instance Method Details

#provide(method_id, translation_options = {}) { ... } ⇒ nil

Note:

It's suggested that you use Object#try to cheaply avoid undefined method `iata_code` for nil:NilClass. It will be available because this class includes active_support/core_ext anyway.

Indicate that you will send in a piece of data about the emitter.

Called inside of ClassMethods#emit_as blocks.

Things to note in the MyFlight example:

  • Sending :origin to Brighter Planet as :origin_airport. Otherwise Brighter Planet won't recognize :origin.

  • Saying we're keying on one code or another. Otherwise Brighter Planet will first try against full names and possibly other columns.

  • Giving blocks to pull codes from MyAircraft and MyAirline objects. Otherwise you might get a querystring like +airline=#<MyAirline […]>+

Examples:

The canonical MyFlight example

emit_as 'Flight' do
  provide :segments_per_trip
  provide :trips
  provide :origin, :as => :origin_airport, :key => :iata_code
  provide :destination, :as => :destination_airport, :key => :iata_code
  provide(:airline, :key => :iata_code) { |f| f.airline.try(:iata_code) }
  provide(:aircraft, :key => :icao_code) { { |f| f.aircraft.try(:icao_code) }
end

Your method is named one thing but should be sent :as something else.

provide :my_distance, :as => :distance

You are keying on something well-known like IATA airline codes.

provide(:airline, :key => :iata_code) { |f| f.airline.try(:iata_code) }

Better to use a block

provide(:airline, :key => :iata_code) { |f| f.airline.try(:iata_code) }
# is equivalent to
def airline_iata_code
  airline.iata_code
end
provide :airline_iata_code, :as => :airline, :key => :iata_code

Parameters:

  • method_id (Symbol)

    What method to call to get the value in question.

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

    a customizable set of options

Options Hash (translation_options):

  • :as (Symbol) — default: name of the method

    If your method name does not match the Brighter Planet characteristic name.

  • :key (Symbol) — default: a number of columns

    What you are keying on. By default, we do a fuzzy match against a number of fields, including full names and various codes.

Yields:

  • Pass a block for the common use case of calling a method on a object.

Returns:

  • (nil)


68
69
70
71
72
73
74
# File 'lib/carbon/registry.rb', line 68

def provide(method_id, translation_options = {}, &blk)
  translation_options = translation_options.dup
  if block_given?
    translation_options[:blk] = blk
  end
  Registry.instance[@klass.name].characteristics[method_id] = translation_options
end