Class: Krikri::Mapping

Inherits:
Object
  • Object
show all
Includes:
MappingDSL
Defined in:
lib/krikri/mapping.rb

Overview

Handles transformation of OriginalRecords into a target class.

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

Examples:

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

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 Also:

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MappingDSL

#method_missing, #properties, #respond_to_missing?

Methods included from Krikri::MappingDSL::RdfSubjects

#uri

Methods included from Krikri::MappingDSL::ParserMethods

#header, #local_name, #record, #record_uri

Constructor Details

#initialize(klass = DPLA::MAP::Aggregation, parser = Krikri::XmlParser, *parser_args) ⇒ Mapping

Returns a new instance of Mapping.

Parameters:

  • klass (Class) (defaults to: DPLA::MAP::Aggregation)

    The model class to build in the mapping process.

  • parser (Class) (defaults to: Krikri::XmlParser)

    The parser class with which to process resources.

  • parser_args (Array)

    The arguments to pass to the parser when processing records.



39
40
41
42
43
44
45
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Krikri::MappingDSL

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



32
33
34
# File 'lib/krikri/mapping.rb', line 32

def klass
  @klass
end

#parserObject (readonly)

Returns the value of attribute parser.



32
33
34
# File 'lib/krikri/mapping.rb', line 32

def parser
  @parser
end

#parser_argsObject (readonly)

Returns the value of attribute parser_args.



32
33
34
# File 'lib/krikri/mapping.rb', line 32

def parser_args
  @parser_args
end

Instance Method Details

#process_record(record) ⇒ Object

Returns A model object of type @klass, processed through the mapping DSL.

Parameters:

Returns:

  • (Object)

    A model object of type @klass, processed through the mapping DSL

Raises:

  • (Krikri::Mapper::Error)

    when an error is thrown when handling any of the properties



54
55
56
57
58
59
60
61
62
63
64
65
# 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