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).

Examples:

a basic declaration

class Book; attr_accessor :author; end

dec = Krikri::MappingDSL::PropertyDeclaration.new(:author, 
  ['Moomin', 'Snuffkin'])

book = Book.new
dec.to_proc.call(book, nil) # nil stands in for a record.

book.author # => ['Moomin', 'Snuffkin']

a declaration with a callable value

class Book; attr_accessor :author; end

values = lambda { |_| ['Moomin', 'Snuffkin'] }
dec = Krikri::MappingDSL::PropertyDeclaration.new(:author, values)

book = Book.new
dec.to_proc.call(book, nil) # nil stands in for a record.

book.author # => ['Moomin', 'Snuffkin']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes a declaration with a given name and value. ‘value` may respond to `#call`, which will be called with a record to generate the values.

Parameters:

  • name (Symbol)
  • value (#call, Object)
  • _opts (Hash) (defaults to: {})

    A hash of options for for the declaration. default: {}

Raises:

  • ArgumentError when a block with arity other than 1 is passed



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 43

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

  @name = name
  @value = value
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 31

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



31
32
33
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 31

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.

Returns:

  • (Proc)

    a proc that can be used to generate a value for the named property.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 65

def to_proc
  block = @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