Sha256: 6c2b3e80ebd3da842a3f541ca7df823617f3a26ef730dfbbd2b52676b0a6352a

Contents?: true

Size: 990 Bytes

Versions: 2

Compression:

Stored size: 990 Bytes

Contents

module Krikri
  ##
  # Handles transformation of OriginalRecords into a target class.
  #
  #   map = Mapping.new(MyModelClass)
  #   map.dsl_method args
  #   map.process_record(my_original_record)
  #   # => #<MyModelClass:0x3ff8b7459210()>
  #
  class Mapping
    include MappingDSL

    attr_reader :klass, :parser

    ##
    # @param klass [Class] The model class to build in the mapping process.
    # @param parser [Class] The parser class with which to process resources.
    def initialize(klass = DPLA::MAP::Aggregation, parser = Krikri::XmlParser)
      @klass = klass
      @parser = parser
    end

    ##
    # @param record [OriginalRecord] An original record to process.
    # @return [Object] A model object of type @klass, processed through the
    #   mapping DSL
    def process_record(record)
      mapped_record = klass.new
      properties.each do |prop|
        prop.to_proc.call(mapped_record, parser.parse(record))
      end
      mapped_record
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
krikri-0.1.1 lib/krikri/mapping.rb
krikri-0.1.0 lib/krikri/mapping.rb