Sha256: 3ba2e2a0ed824df37081bc248aec50e826b3d86f3c9ac2e645d75ba19019ef3b

Contents?: true

Size: 1.18 KB

Versions: 29

Compression:

Stored size: 1.18 KB

Contents

module Krikri::MappingDSL
  ##
  # Returns a proc that can be run to add values for the property to
  # Passes value(s) through a block, if given.
  # @return [Proc] a proc that can be used to generate a value for the named
  # property.
  class ChildDeclaration < PropertyDeclaration
    def initialize(name, target_class, opts = {}, &block)
      @name = name
      @target_class = target_class
      @block = block
      @each = opts.delete(:each)
      @as = opts.delete(:as)
    end

    def to_proc
      block = @block if @block
      target_class = @target_class
      each_val = @each
      as_sym = @as
      lambda do |target, record|
        if each_val
          each_val.call(record).each do |value|
            map = ::Krikri::Mapping.new(target_class)
            map.define_singleton_method(as_sym) do
              each_val.dup.select { |v| v.value == value }
            end
            map.instance_eval(&block)
            target.send(name) << map.process_record(record)
          end
        else
          map = ::Krikri::Mapping.new(target_class)
          map.instance_eval(&block)
          target.send(setter, map.process_record(record))
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
krikri-0.3.3 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.3.2 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.3.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.2.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.2.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.1.3 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.1.2 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.1.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.1.0 lib/krikri/mapping_dsl/child_declaration.rb