Sha256: e701edae14f4be7740d9454f0b69f63581676e5955712ab7b3e52c0e8d47dd31

Contents?: true

Size: 1.32 KB

Versions: 18

Compression:

Stored size: 1.32 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
          iter = each_val.respond_to?(:call) ? each_val.call(record) : each_val
          iter.each do |value|
            map = ::Krikri::Mapping.new(target_class)
            map.define_singleton_method(as_sym) do
              each_val.dup.select do |v|
                v = v.value if v.respond_to? :value
                v == value 
              end
            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

18 entries across 18 versions & 1 rubygems

Version Path
krikri-0.12.4 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.12.3 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.12.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.12.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.12.0.pre.rc.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.11.2 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.11.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.11.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.10.1 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.10.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.9.0 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.9 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.8 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.7 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.6 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.5 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.4 lib/krikri/mapping_dsl/child_declaration.rb
krikri-0.8.3 lib/krikri/mapping_dsl/child_declaration.rb