Class: TwoWayMapper::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/two_way_mapper/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right, opt = {}) ⇒ Rule

Returns a new instance of Rule.



7
8
9
10
11
# File 'lib/two_way_mapper/rule.rb', line 7

def initialize(left, right, opt = {})
  @left = left
  @right = right
  @options = opt
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



5
6
7
# File 'lib/two_way_mapper/rule.rb', line 5

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



5
6
7
# File 'lib/two_way_mapper/rule.rb', line 5

def right
  @right
end

Instance Method Details

#from_left_to_right(left_obj, right_obj) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/two_way_mapper/rule.rb', line 13

def from_left_to_right(left_obj, right_obj)
  return right_obj if from_right_to_left_only?

  value = left.read(left_obj)
  value = map_value(value, true)
  if @options[:on_left_to_right].respond_to?(:call)
    value = @options[:on_left_to_right].call(value, left_obj, right_obj)
  end
  right.write(right_obj, value)

  right_obj
end

#from_left_to_right_only?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/two_way_mapper/rule.rb', line 43

def from_left_to_right_only?
  @options[:from_left_to_right_only]
end

#from_right_to_left(left_obj, right_obj) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/two_way_mapper/rule.rb', line 26

def from_right_to_left(left_obj, right_obj)
  return left_obj if from_left_to_right_only?

  value = right.read(right_obj)
  value = map_value(value, false)
  if @options[:on_right_to_left].respond_to?(:call)
    value = @options[:on_right_to_left].call(value, left_obj, right_obj)
  end
  left.write(left_obj, value)

  left_obj
end

#from_right_to_left_only?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/two_way_mapper/rule.rb', line 39

def from_right_to_left_only?
  @options[:from_right_to_left_only]
end