Module: Quo::ComposedInstance
- Included in:
- ComposedCollectionBackedQuery, ComposedRelationBackedQuery
- Defined in:
- lib/quo/composed_instance.rb,
sig/generated/quo/composed_instance.rbs
Instance Method Summary collapse
- #apply_to_operand(operand, prop_name, value) ⇒ Object
- #both_relations?(lr, rr) ⇒ Boolean
- #copy(**overrides) ⇒ Quo::Query
- #fan_override(prop_name, value) ⇒ Quo::Query
- #is_relation?(rel) ⇒ Boolean
- #left_enumerable_right_relation?(lr, rr) ⇒ Boolean
- #left_relation_right_enumerable?(lr, rr) ⇒ Boolean
- #merge_active_record_relations(left_rel, right_rel) ⇒ ActiveRecord::Relation
- #merge_left_and_right ⇒ ActiveRecord::Relation, Enumerable[untyped]
- #operand_accepts?(operand, prop_name) ⇒ Boolean
- #own_property_names ⇒ Array[Symbol]
- #unwrap_operand(operand) ⇒ ActiveRecord::Relation, Enumerable[untyped]
Instance Method Details
#apply_to_operand(operand, prop_name, value) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/quo/composed_instance.rb', line 69 def apply_to_operand(operand, prop_name, value) case operand when Quo::ComposedInstance operand.send(:fan_override, prop_name, value) when Quo::Query operand.copy(prop_name => value) end end |
#both_relations?(lr, rr) ⇒ Boolean
126 127 128 |
# File 'lib/quo/composed_instance.rb', line 126 def both_relations?(lr, rr) is_relation?(lr) && is_relation?(rr) end |
#copy(**overrides) ⇒ Quo::Query
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/quo/composed_instance.rb', line 9 def copy(**overrides) return super if overrides.empty? own_keys = own_property_names own_overrides = overrides.slice(*own_keys) fan_overrides = overrides.except(*own_keys) result = own_overrides.empty? ? self : super(**own_overrides) fan_overrides.each do |key, value| result = result.send(:fan_override, key, value) end result end |
#fan_override(prop_name, value) ⇒ Quo::Query
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/quo/composed_instance.rb', line 28 def fan_override(prop_name, value) right_match = operand_accepts?(right, prop_name) left_match = operand_accepts?(left, prop_name) unless right_match || left_match raise ArgumentError, "unknown property #{prop_name.inspect} on #{self.class}" end result = self if right_match result = result.copy(right: apply_to_operand(right, prop_name, value)) end if left_match result = result.copy(left: apply_to_operand(left, prop_name, value)) end result end |
#is_relation?(rel) ⇒ Boolean
119 120 121 |
# File 'lib/quo/composed_instance.rb', line 119 def is_relation?(rel) rel.is_a?(::ActiveRecord::Relation) end |
#left_enumerable_right_relation?(lr, rr) ⇒ Boolean
140 141 142 |
# File 'lib/quo/composed_instance.rb', line 140 def left_enumerable_right_relation?(lr, rr) !is_relation?(lr) && is_relation?(rr) end |
#left_relation_right_enumerable?(lr, rr) ⇒ Boolean
133 134 135 |
# File 'lib/quo/composed_instance.rb', line 133 def left_relation_right_enumerable?(lr, rr) is_relation?(lr) && !is_relation?(rr) end |
#merge_active_record_relations(left_rel, right_rel) ⇒ ActiveRecord::Relation
112 113 114 115 |
# File 'lib/quo/composed_instance.rb', line 112 def merge_active_record_relations(left_rel, right_rel) left_rel = left_rel.joins(merge_joins) if merge_joins left_rel.merge(right_rel) end |
#merge_left_and_right ⇒ ActiveRecord::Relation, Enumerable[untyped]
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/quo/composed_instance.rb', line 79 def merge_left_and_right left_rel = unwrap_operand(left) right_rel = unwrap_operand(right) if both_relations?(left_rel, right_rel) merge_active_record_relations(left_rel, right_rel) elsif left_relation_right_enumerable?(left_rel, right_rel) left_rel.to_a + right_rel.to_a elsif left_enumerable_right_relation?(left_rel, right_rel) && left_rel.respond_to?(:+) left_rel.to_a + right_rel.to_a elsif left_rel.respond_to?(:+) left_rel + right_rel else raise ArgumentError, "Cannot merge #{left.class} with #{right.class}" end end |
#operand_accepts?(operand, prop_name) ⇒ Boolean
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/quo/composed_instance.rb', line 53 def operand_accepts?(operand, prop_name) case operand when Quo::ComposedInstance operand.send(:operand_accepts?, operand.right, prop_name) || operand.send(:operand_accepts?, operand.left, prop_name) when Quo::Query operand.class.literal_properties.properties_index.key?(prop_name) else false end end |
#own_property_names ⇒ Array[Symbol]
46 47 48 |
# File 'lib/quo/composed_instance.rb', line 46 def own_property_names self.class.literal_properties.properties_index.keys end |