class Krikri::Mapping

Handles transformation of OriginalRecords into a target class.

@example

map = Mapping.new(MyModelClass)
map.dsl_method args
map.process_record(my_original_record)
# => #<MyModelClass:0x3ff8b7459210()>

When one or more errors are encoutered during processing, they are collected in a `Krikri::Kapping::Error` and re-raised.

@example When an error is thrown during property mapping

map = Mapping.new(MyModelClass)
begin
  map.process_record(my_original_record)
rescue Mapping::Error => e
  e.message
end
# => Property failed on subject:
#       {subject error message}
#       {subject error backtrace}
#    Property failed on title:
#       {title error message}
#       {title error backtrace}

@see Krikri::MappingDSL

Attributes

klass[R]
parser[R]
parser_args[R]

Public Class Methods

new(klass = DPLA::MAP::Aggregation, parser = Krikri::XmlParser, *parser_args) click to toggle source

@param klass [Class] The model class to build in the mapping process. @param parser [Class] The parser class with which to process resources. @param parser_args [Array] The arguments to pass to the parser when

processing records.
# File lib/krikri/mapping.rb, line 39
def initialize(klass = DPLA::MAP::Aggregation,
               parser = Krikri::XmlParser,
               *parser_args)
  @klass = klass
  @parser = parser
  @parser_args = parser_args
end

Public Instance Methods

process_record(record) click to toggle source

@param record [OriginalRecord] An original record to process.

@return [Object] A model object of type @klass, processed through the

mapping DSL

@raise [Krikri::Mapper::Error] when an error is thrown when handling any

of the properties
# File lib/krikri/mapping.rb, line 54
def process_record(record)
  mapped_record = klass.new
  error = properties.each_with_object(Error.new(record)) do |prop, error|
    begin
      prop.to_proc.call(mapped_record, parser.parse(record, *@parser_args))
    rescue => e
      error.add(prop.name, e)
    end
  end
  raise error unless error.errors.empty?
  mapped_record
end