Class: Lutaml::Qea::Repositories::BaseRepository
- Inherits:
-
Object
- Object
- Lutaml::Qea::Repositories::BaseRepository
- Includes:
- Enumerable
- Defined in:
- lib/lutaml/qea/repositories/base_repository.rb
Overview
Base repository for querying model collections
This class provides common query methods for all model repositories. Subclasses can extend with model-specific query methods.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#records ⇒ Array
readonly
The collection of records.
Instance Method Summary collapse
-
#all ⇒ Array
Get all records.
-
#any?(conditions = nil) {|record| ... } ⇒ Boolean
Check if any records match conditions.
-
#count(conditions = nil) {|record| ... } ⇒ Integer
Count records.
-
#distinct(attribute) ⇒ Array
Get unique values for an attribute.
-
#each {|record| ... } ⇒ Enumerator
Iterates over all records.
-
#empty? ⇒ Boolean
Check if repository is empty.
-
#find(id) ⇒ Object?
Find a record by primary key.
-
#find_by_key(key, id) ⇒ Object?
Find a record by key and id.
-
#find_first(conditions = nil) {|record| ... } ⇒ Object?
Find first record matching conditions.
-
#group_by(attribute) ⇒ Hash
Group records by an attribute.
-
#initialize(records) ⇒ BaseRepository
constructor
Initialize repository with a collection.
-
#none?(conditions = nil) {|record| ... } ⇒ Boolean
Check if no records match conditions.
-
#order_by(attribute, order = :asc) ⇒ Array
Sort records by an attribute.
-
#pluck(*attributes) ⇒ Array<Hash>
Select specific attributes from records.
-
#size ⇒ Integer
(also: #length)
Get the size of the collection.
-
#where(conditions = nil) {|record| ... } ⇒ Array
Filter records by conditions.
Constructor Details
#initialize(records) ⇒ BaseRepository
Initialize repository with a collection
25 26 27 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 25 def initialize(records) @records = records.freeze end |
Instance Attribute Details
#records ⇒ Array (readonly)
Returns The collection of records.
20 21 22 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 20 def records @records end |
Instance Method Details
#all ⇒ Array
Get all records
42 43 44 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 42 def all @records end |
#any?(conditions = nil) {|record| ... } ⇒ Boolean
Check if any records match conditions
130 131 132 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 130 def any?(conditions = nil, &block) !where(conditions, &block).empty? end |
#count(conditions = nil) {|record| ... } ⇒ Integer
Count records
104 105 106 107 108 109 110 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 104 def count(conditions = nil, &block) if block || conditions where(conditions, &block).size else @records.size end end |
#distinct(attribute) ⇒ Array
Get unique values for an attribute
193 194 195 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 193 def distinct(attribute) @records.map { |record| record.send(attribute) }.uniq end |
#each {|record| ... } ⇒ Enumerator
Iterates over all records
33 34 35 36 37 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 33 def each(&block) return to_enum(:each) unless block @records.each(&block) end |
#empty? ⇒ Boolean
Check if repository is empty
200 201 202 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 200 def empty? @records.empty? end |
#find(id) ⇒ Object?
Find a record by primary key
50 51 52 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 50 def find(id) @records.find { |record| record.primary_key == id } end |
#find_by_key(key, id) ⇒ Object?
Find a record by key and id
59 60 61 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 59 def find_by_key(key, id) @records.find { |record| record.send(key) == id } end |
#find_first(conditions = nil) {|record| ... } ⇒ Object?
Find first record matching conditions
121 122 123 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 121 def find_first(conditions = nil, &block) where(conditions, &block).first end |
#group_by(attribute) ⇒ Hash
Group records by an attribute
167 168 169 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 167 def group_by(attribute) @records.group_by { |record| record.send(attribute) } end |
#none?(conditions = nil) {|record| ... } ⇒ Boolean
Check if no records match conditions
139 140 141 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 139 def none?(conditions = nil, &block) where(conditions, &block).empty? end |
#order_by(attribute, order = :asc) ⇒ Array
Sort records by an attribute
180 181 182 183 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 180 def order_by(attribute, order = :asc) sorted = @records.sort_by { |record| record.send(attribute) } order == :desc ? sorted.reverse : sorted end |
#pluck(*attributes) ⇒ Array<Hash>
Select specific attributes from records
151 152 153 154 155 156 157 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 151 def pluck(*attributes) @records.map do |record| attributes.each_with_object({}) do |attr, hash| hash[attr] = record.send(attr) if record.respond_to?(attr) end end end |
#size ⇒ Integer Also known as: length
Get the size of the collection
207 208 209 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 207 def size @records.size end |
#where(conditions = nil) {|record| ... } ⇒ Array
Filter records by conditions
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 76 def where(conditions = nil, &block) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity if block @records.select(&block) elsif conditions.is_a?(Hash) @records.select do |record| conditions.all? do |attr, value| record.respond_to?(attr) && record.send(attr) == value end end else @records end end |