Class: Krikri::MappingDSL::PropertyDeclaration

Inherits:
Object
  • Object
show all
Defined in:
lib/krikri/mapping_dsl/property_declaration.rb

Overview

Specifies a mapping between a property name and its mapped value(s). Deals with static properties (given a specific value or values), and dynamic properties (where values are modified by a block).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, _opts = {}, &block) ⇒ PropertyDeclaration


9
10
11
12
13
14
15
16
17
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 9

def initialize(name, value, _opts = {}, &block)
  if block_given?
    raise 'Block must have arity of 1 to be applied to property' unless
      block.arity == 1
    @block = block
  end
  @name = name
  @value = value
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 7

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value


7
8
9
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 7

def value
  @value
end

Instance Method Details

#to_procProc

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

If value is a callable object (e.g. a Proc), calls it with the OriginalRecord as an argument to determine the value.

property.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 28

def to_proc
  block = @block if @block
  value = @value
  lambda do |target, record|
    value = value.call(record) if value.respond_to? :call
    return target.send(setter, value) unless block
    if value.is_a? Enumerable
      values = value.map { |v| instance_exec(v, &block) }
      target.send(setter, values)
    else
      target.send(setter, instance_exec(value, &block))
    end
  end
end