Module: Foobara::Entity::Concerns::Queries::ClassMethods
- Defined in:
- foobara-0.0.110/projects/entity/src/concerns/queries.rb
Instance Method Summary collapse
- #all ⇒ Object
- #all_exist?(record_ids) ⇒ Boolean
- #count ⇒ Object
- #exists?(record_id) ⇒ Boolean
- #find_all_by_attribute(attribute_name, value) ⇒ Object
- #find_all_by_attribute_any_of(attribute_name, values) ⇒ Object
- #find_all_by_attribute_containing_any_of(attribute_name, values) ⇒ Object
- #find_by(attributes) ⇒ Object
- #find_by_attribute(attribute_name, value) ⇒ Object
- #find_by_attribute_containing(attribute_name, value) ⇒ Object
- #find_many_by(attributes) ⇒ Object
- #first ⇒ Object
- #load(record) ⇒ Object
- #load_aggregate(record_or_record_id) ⇒ Object
- #load_many(*record_ids) ⇒ Object
- #that_own(record, filters = []) ⇒ Object
- #that_owns(record, filters = []) ⇒ Object
Instance Method Details
#all ⇒ Object
8 9 10 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 8 def all(&) current_transaction_table.all(&) end |
#all_exist?(record_ids) ⇒ Boolean
79 80 81 82 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 79 def all_exist?(record_ids) # TODO: support splat current_transaction_table.all_exist?(record_ids) end |
#count ⇒ Object
89 90 91 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 89 def count current_transaction_table.count end |
#exists?(record_id) ⇒ Boolean
84 85 86 87 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 84 def exists?(record_id) # TODO: support splat current_transaction_table.exists?(record_id) end |
#find_all_by_attribute(attribute_name, value) ⇒ Object
23 24 25 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 23 def find_all_by_attribute(attribute_name, value) current_transaction_table.find_all_by_attribute(attribute_name, value) end |
#find_all_by_attribute_any_of(attribute_name, values) ⇒ Object
35 36 37 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 35 def find_all_by_attribute_any_of(attribute_name, values) current_transaction_table.find_all_by_attribute_any_of(attribute_name, values) end |
#find_all_by_attribute_containing_any_of(attribute_name, values) ⇒ Object
31 32 33 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 31 def find_all_by_attribute_containing_any_of(attribute_name, values) current_transaction_table.find_all_by_attribute_containing_any_of(attribute_name, values) end |
#find_by(attributes) ⇒ Object
39 40 41 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 39 def find_by(attributes) current_transaction_table.find_by(attributes) end |
#find_by_attribute(attribute_name, value) ⇒ Object
19 20 21 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 19 def find_by_attribute(attribute_name, value) current_transaction_table.find_by_attribute(attribute_name, value) end |
#find_by_attribute_containing(attribute_name, value) ⇒ Object
27 28 29 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 27 def find_by_attribute_containing(attribute_name, value) current_transaction_table.find_by_attribute_containing(attribute_name, value) end |
#find_many_by(attributes) ⇒ Object
43 44 45 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 43 def find_many_by(attributes) current_transaction_table.find_many_by(attributes) end |
#first ⇒ Object
12 13 14 15 16 17 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 12 def first # TODO: don't all queries need to do this??? Foobara::Namespace.use entity_type do current_transaction_table.first end end |
#load(record) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 47 def load(record) if !record.is_a?(Foobara::Entity) || !record.loaded? current_transaction_table.load(record) end rescue ::Foobara::Persistence::EntityAttributesCrudDriver::Table::CannotFindError primary_key = if record.is_a?(Foobara::Entity) record.primary_key else record end raise NotFoundError.for(primary_key, entity_class: self) end |
#load_aggregate(record_or_record_id) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 61 def load_aggregate(record_or_record_id) record = if record_or_record_id.is_a?(Entity) record_or_record_id else thunk(record_or_record_id) end current_transaction.load_aggregate(record) end |
#load_many(*record_ids) ⇒ Object
71 72 73 74 75 76 77 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 71 def load_many(*record_ids) if record_ids.size == 1 && record_ids.first.is_a?(::Array) record_ids = record_ids.first end current_transaction_table.load_many(record_ids) end |
#that_own(record, filters = []) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 107 def that_own(record, filters = []) association_key = association_for([record.class, *filters]) possible_direct_owner_path = DataPath.new(association_key) # We need to find the second-to-last entity in the association path attribute_path = [] owning_entity_class = nil begin attribute_path.unshift(possible_direct_owner_path.last) possible_direct_owner_path = DataPath.new(possible_direct_owner_path.path[0..-2]) owning_entity_class = if possible_direct_owner_path.empty? self else deep_associations[possible_direct_owner_path.to_s]&.target_class end end until owning_entity_class if attribute_path.size == 1 attribute_path = attribute_path.first end if owning_entity_class == self owning_entity_class.find_all_by_attribute(attribute_path, record) else Enumerator.new do |yielder| owners = owning_entity_class.find_all_by_attribute(attribute_path, record) owners.each do |owner| that_own(owner, filters).each do |r| yielder.yield(r) end end end end end |
#that_owns(record, filters = []) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'foobara-0.0.110/projects/entity/src/concerns/queries.rb', line 93 def that_owns(record, filters = []) containing_records = that_own(record, filters).to_a unless containing_records.empty? if containing_records.size == 1 containing_records.first else # :nocov: raise "Expected only one record to own #{record} but found #{containing_records.size}" # :nocov: end end end |