Class: Krikri::MappingDSL::PropertyDeclaration
- Inherits:
-
Object
- Object
- Krikri::MappingDSL::PropertyDeclaration
- 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).
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name, value, _opts = {}, &block) ⇒ PropertyDeclaration
constructor
Initializes a declaration with a given name and value.
-
#to_proc ⇒ Proc
Returns a proc that can be run to add values for the property to Passes value(s) through a block, if given.
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.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'lib/krikri/mapping_dsl/property_declaration.rb', line 31 def name @name end |
#value ⇒ Object (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_proc ⇒ Proc
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.
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 |