Module: Foobara::Entity::Concerns::Queries::ClassMethods
- Defined in:
- foobara-0.2.6/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, load_paths: nil) ⇒ 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 'projects/entity/src/concerns/queries.rb', line 8 def all(&) current_transaction_table.all(&) end |
#all_exist?(record_ids) ⇒ Boolean
109 110 111 112 |
# File 'projects/entity/src/concerns/queries.rb', line 109 def all_exist?(record_ids) # TODO: support splat current_transaction_table.all_exist?(record_ids) end |
#count ⇒ Object
119 120 121 |
# File 'projects/entity/src/concerns/queries.rb', line 119 def count current_transaction_table.count end |
#exists?(record_id) ⇒ Boolean
114 115 116 117 |
# File 'projects/entity/src/concerns/queries.rb', line 114 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 '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 '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 '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 '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 '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 '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 '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 '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, load_paths: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'projects/entity/src/concerns/queries.rb', line 47 def load(record, load_paths: nil) record = if record.is_a?(Foobara::Entity) if record.loaded? record else current_transaction_table.load(record) end else current_transaction_table.load(record) end if load_paths if load_paths.is_a?(::Array) first = load_paths.first if first.is_a?(::Symbol) || (first.is_a?(::String) && !first.include?(".")) load_paths = [load_paths] end else load_paths = [load_paths] end load_paths.each do |data_path| DataPath.values_at(data_path, record).each do |value| if value.is_a?(Foobara::Entity) value.class.load(value) elsif value.is_a?(::Array) value.each { |r| r.class.load(r) } end end end end record 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
91 92 93 94 95 96 97 98 99 |
# File 'projects/entity/src/concerns/queries.rb', line 91 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
101 102 103 104 105 106 107 |
# File 'projects/entity/src/concerns/queries.rb', line 101 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'projects/entity/src/concerns/queries.rb', line 137 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
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'projects/entity/src/concerns/queries.rb', line 123 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 |