Class: Krikri::Mapping
- Inherits:
-
Object
- Object
- Krikri::Mapping
- 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.
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#parser_args ⇒ Object
readonly
Returns the value of attribute parser_args.
Instance Method Summary collapse
-
#initialize(klass = DPLA::MAP::Aggregation, parser = Krikri::XmlParser, *parser_args) ⇒ Mapping
constructor
A new instance of Mapping.
-
#process_record(record) ⇒ Object
A model object of type @klass, processed through the mapping DSL.
Methods included from MappingDSL
#method_missing, #properties, #respond_to_missing?
Methods included from Krikri::MappingDSL::RdfSubjects
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.
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
32 33 34 |
# File 'lib/krikri/mapping.rb', line 32 def klass @klass end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
32 33 34 |
# File 'lib/krikri/mapping.rb', line 32 def parser @parser end |
#parser_args ⇒ Object (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.
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 |