Class: TwoWayMapper::Mapping
- Inherits:
-
Object
- Object
- TwoWayMapper::Mapping
- Defined in:
- lib/two_way_mapper/mapping.rb
Constant Summary collapse
- DIRECTIONS =
[:left, :right].freeze
Instance Attribute Summary collapse
-
#left_class ⇒ Object
readonly
Returns the value of attribute left_class.
-
#left_options ⇒ Object
readonly
Returns the value of attribute left_options.
-
#right_class ⇒ Object
readonly
Returns the value of attribute right_class.
-
#right_options ⇒ Object
readonly
Returns the value of attribute right_options.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
-
#initialize ⇒ Mapping
constructor
A new instance of Mapping.
- #rule(left_selectors, right_selectors = {}, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Mapping
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_class ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute left_options.
7 8 9 |
# File 'lib/two_way_mapper/mapping.rb', line 7 def end |
#right_class ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute right_options.
7 8 9 |
# File 'lib/two_way_mapper/mapping.rb', line 7 def end |
#rules ⇒ Object (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 = {}, = {}) 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 = .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, .merge(left_opt)) end right_nodes = Array(right_selectors).map do |right_selector| right_class.new(right_selector, .merge(right_opt)) end @rules << Rule.new(left_nodes, right_nodes, opt) end |