Module: Carbon::ClassMethods

Defined in:
lib/carbon.rb

Overview

Mixed into any class that includes Carbon.

Instance Method Summary collapse

Instance Method Details

#emit_as(emitter, &blk) ⇒ nil

DSL for declaring how to represent this class an an emitter.

See also Registry::Registrar#provide.

You get this when you include Carbon in a class.

Examples:

MyFlight

# A a flight in your data warehouse
class MyFlight
  def airline
    # ... => MyAirline(:name, :icao_code, ...)
  end
  def aircraft
    # ... => MyAircraft(:name, :icao_code, ...)
  end
  def origin
    # ... => String
  end
  def destination
    # ... => String
  end
  def segments_per_trip
    # ... => Integer
  end
  def trips
    # ... => Integer
  end
  include Carbon
  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
end

Parameters:

Returns:

  • (nil)


191
192
193
194
195
# File 'lib/carbon.rb', line 191

def emit_as(emitter, &blk)
  emitter = emitter.to_s.singularize.camelcase
  registrar = Registry::Registrar.new self, emitter
  registrar.instance_eval(&blk)
end