Sha256: bb8d5e09afb7439c4eba54305ef0ec5011d1ff52296a0bcee5c9de4d76dc936f

Contents?: true

Size: 1.2 KB

Versions: 14

Compression:

Stored size: 1.2 KB

Contents

module Krikri
  ##
  # 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()>
  #
  class Mapping
    include MappingDSL

    attr_reader :klass, :parser, :parser_args

    ##
    # @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.
    def initialize(klass = DPLA::MAP::Aggregation,
                   parser = Krikri::XmlParser,
                   *parser_args)
      @klass = klass
      @parser = parser
      @parser_args = parser_args
    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, *@parser_args))
      end
      mapped_record
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
krikri-0.8.6 lib/krikri/mapping.rb
krikri-0.8.5 lib/krikri/mapping.rb
krikri-0.8.4 lib/krikri/mapping.rb
krikri-0.8.3 lib/krikri/mapping.rb
krikri-0.8.2 lib/krikri/mapping.rb
krikri-0.8.1 lib/krikri/mapping.rb
krikri-0.8.0 lib/krikri/mapping.rb
krikri-0.7.6 lib/krikri/mapping.rb
krikri-0.7.5 lib/krikri/mapping.rb
krikri-0.7.4 lib/krikri/mapping.rb
krikri-0.7.3 lib/krikri/mapping.rb
krikri-0.7.2 lib/krikri/mapping.rb
krikri-0.7.1 lib/krikri/mapping.rb
krikri-0.7.0 lib/krikri/mapping.rb