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.

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

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.


20
21
22
23
24
25
26
# File 'lib/krikri/mapping.rb', line 20

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


13
14
15
# File 'lib/krikri/mapping.rb', line 13

def klass
  @klass
end

#parserObject (readonly)

Returns the value of attribute parser


13
14
15
# File 'lib/krikri/mapping.rb', line 13

def parser
  @parser
end

#parser_argsObject (readonly)

Returns the value of attribute parser_args


13
14
15
# File 'lib/krikri/mapping.rb', line 13

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


32
33
34
35
36
37
38
# File 'lib/krikri/mapping.rb', line 32

def process_record(record)
  mapped_record = klass.new
  properties.each do |prop|
    prop.to_proc.call(mapped_record, parser.parse(record, *@parser_args))
  end
  mapped_record
end