Class: TwoWayMapper::Mapping

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

Constant Summary collapse

DIRECTIONS =
[:left, :right].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMapping

Returns a new instance of Mapping.



9
10
11
# File 'lib/two_way_mapper/mapping.rb', line 9

def initialize
  @rules = []
end

Instance Attribute Details

#left_classObject (readonly)

Returns the value of attribute left_class.



7
8
9
# File 'lib/two_way_mapper/mapping.rb', line 7

def left_class
  @left_class
end

#left_optionsObject (readonly)

Returns the value of attribute left_options.



7
8
9
# File 'lib/two_way_mapper/mapping.rb', line 7

def left_options
  @left_options
end

#right_classObject (readonly)

Returns the value of attribute right_class.



7
8
9
# File 'lib/two_way_mapper/mapping.rb', line 7

def right_class
  @right_class
end

#right_optionsObject (readonly)

Returns the value of attribute right_options.



7
8
9
# File 'lib/two_way_mapper/mapping.rb', line 7

def right_options
  @right_options
end

#rulesObject (readonly)

Returns the value of attribute rules.



7
8
9
# File 'lib/two_way_mapper/mapping.rb', line 7

def rules
  @rules
end

Instance Method Details

#rule(left_selectors, right_selectors = {}, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/two_way_mapper/mapping.rb', line 37

def rule(left_selectors, right_selectors = {}, options = {})
  raise 'You need to set left before calling rule' unless left_class
  raise 'You need to set right before calling rule' unless right_class

  opt = options.dup
  left_opt = opt.delete(:left) || {}
  right_opt = opt.delete(:right) || {}

  if left_selectors.is_a?(Hash)
    raise ArgumentError if left_selectors.count < 2

    opt = left_selectors
    left_selectors = opt.keys.first
    left_opt.merge! opt.delete(left_selectors)
    right_selectors = opt.keys.first
    right_opt.merge!(opt.delete(right_selectors))
  end

  left_nodes = Array(left_selectors).map do |left_selector|
    left_class.new(left_selector, left_options.merge(left_opt))
  end
  right_nodes = Array(right_selectors).map do |right_selector|
    right_class.new(right_selector, right_options.merge(right_opt))
  end

  @rules << Rule.new(left_nodes, right_nodes, opt)
end