module Krikri::Mapper

Provides the public interface for defining and running metadata Mappings. Define mappings by passing a block to define with registered DSL methods; in the simple case pass values to property names:

Krikri::Mapper.define do
  property_name   value
  property_two    another_value do |val|
    transform_value(val)
  end

  nested_resource_property :class => DPLA::MAP::Agent do
    agent_property  agent_value
  end
end

define accepts :class as an option, to specify the model class to use. The default is DPLA::MAP::Aggregation:

Krikri::Mapper.define :class => MyModelClass {}

Once a Mapping is defined, create mapped records with map to return objects of the specified class.

@see Mapping @see MappingDSL

Constants

Registry

An application-wide registry of defined mappings

Public Instance Methods

define(name, opts = {}, &block) click to toggle source

Creates mappings and passes DSL methods through to them, then adds them to a global registry.

@param name [Symbol] a unique name for the mapper in the registry. @param opts [Hash] options to pass to the mapping instance, options are:

:class, :parser, and :parser_args

@yield A block passed through to the mapping instance containing the

mapping in the language specified by MappingDSL
# File lib/krikri/mapper.rb, line 40
def define(name, opts = {}, &block)
  klass = opts.fetch(:class, DPLA::MAP::Aggregation)
  parser = opts.fetch(:parser, Krikri::XmlParser)
  parser_args = opts.fetch(:parser_args, nil)
  map = Krikri::Mapping.new(klass, parser, *parser_args)
  map.instance_eval(&block) if block_given?
  Registry.register!(name, map)
end
map(name, records) click to toggle source

Maps OriginalRecords to the specified model class using a registered Krikri::Mapping.

@param name [Symbol] the name of a registered mapper to use @param records A record or list of records that respond to to_s @return [Array] An array of objects of the model class, with properties

set by the mapping.

@see Mapping

# File lib/krikri/mapper.rb, line 59
def map(name, records)
  mapping = Registry.get(name)
  records = Array(records) unless records.is_a? Enumerable

  result = records.map do |rec|
    begin
      mapping.process_record(rec)
    rescue => e
      Rails.logger.error(e.message)
      nil
    end
  end
end