Module: CompositePrimaryKeys::Predicates
- Extended by:
- Predicates
- Included in:
- Predicates
- Defined in:
- lib/composite_primary_keys/composite_predicates.rb
Instance Method Summary collapse
- #cpk_and_predicate(predicates) ⇒ Object
- #cpk_id_predicate(table, keys, values) ⇒ Object
- #cpk_in_predicate(table, primary_keys, ids) ⇒ Object
- #cpk_join_predicate(table1, key1, table2, key2) ⇒ Object
- #cpk_or_predicate(predicates, group = true) ⇒ Object
Instance Method Details
#cpk_and_predicate(predicates) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/composite_primary_keys/composite_predicates.rb', line 7 def cpk_and_predicate(predicates) if predicates.length == 1 predicates.first else Arel::Nodes::And.new(predicates) end end |
#cpk_id_predicate(table, keys, values) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/composite_primary_keys/composite_predicates.rb', line 34 def cpk_id_predicate(table, keys, values) eq_predicates = keys.zip(values).map do |key, value| table[key].eq(value) end cpk_and_predicate(eq_predicates) end |
#cpk_in_predicate(table, primary_keys, ids) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/composite_primary_keys/composite_predicates.rb', line 52 def cpk_in_predicate(table, primary_keys, ids) and_predicates = ids.map do |id| cpk_id_predicate(table, primary_keys, id) end cpk_or_predicate(and_predicates) end |
#cpk_join_predicate(table1, key1, table2, key2) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/composite_primary_keys/composite_predicates.rb', line 41 def cpk_join_predicate(table1, key1, table2, key2) key1_fields = Array(key1).map {|key| table1[key]} key2_fields = Array(key2).map {|key| table2[key]} eq_predicates = key1_fields.zip(key2_fields).map do |key_field1, key_field2| key_field2 = Arel::Nodes::Quoted.new(key_field2) unless Arel::Attributes::Attribute === key_field2 key_field1.eq(key_field2) end cpk_and_predicate(eq_predicates) end |
#cpk_or_predicate(predicates, group = true) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/composite_primary_keys/composite_predicates.rb', line 15 def cpk_or_predicate(predicates, group = true) if predicates.length <= 1 predicates.first else split_point = predicates.length / 2 predicates_first_half = predicates[0...split_point] predicates_second_half = predicates[split_point..-1] or_predicate = ::Arel::Nodes::Or.new(cpk_or_predicate(predicates_first_half, false), cpk_or_predicate(predicates_second_half, false)) if group ::Arel::Nodes::Grouping.new(or_predicate) else or_predicate end end end |