Class: Krikri::MappingDSL::ChildDeclaration

Inherits:
PropertyDeclaration show all
Defined in:
lib/krikri/mapping_dsl/child_declaration.rb

Overview

Returns a proc that can be run to add values for the property to Passes value(s) through a block, if given. property.

Instance Attribute Summary

Attributes inherited from PropertyDeclaration

#name, #value

Instance Method Summary collapse

Constructor Details

#initialize(name, target_class, opts = {}, &block) ⇒ ChildDeclaration


8
9
10
11
12
13
14
# File 'lib/krikri/mapping_dsl/child_declaration.rb', line 8

def initialize(name, target_class, opts = {}, &block)
  @name = name
  @target_class = target_class
  @block = block
  @each = opts.delete(:each)
  @as = opts.delete(:as)
end

Instance Method Details

#to_procObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/krikri/mapping_dsl/child_declaration.rb', line 16

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