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_selector, right_selector = {}, options = {}) ⇒ Object



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

def rule(left_selector, right_selector = {}, 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_selector.is_a?(Hash)
    raise ArgumentError if left_selector.count < 2

    opt = left_selector
    left_selector = opt.keys.first
    left_opt.merge! opt.delete(left_selector)
    right_selector = opt.keys.first
    right_opt.merge!(opt.delete(right_selector))
  end

  left = left_class.new(left_selector, left_options.merge(left_opt))
  right = right_class.new(right_selector, right_options.merge(right_opt))

  @rules << Rule.new(left, right, opt)
end