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.8.2 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.7.6 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.7.5 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.7.4 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.7.3 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.7.2 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.7.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.7.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.6.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.7 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.6 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.5 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.4 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.3 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.2 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.5.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.4.0 lib/krikri/mapping_dsl/child_declaration.rb