Class: Lutaml::Qea::Repositories::BaseRepository

Inherits:
Object
  • Object
show all
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.

Examples:

Using base repository

repository = BaseRepository.new(records)
all_records = repository.all
record = repository.find(123)
filtered = repository.where { |r| r.name == "Test" }

Direct Known Subclasses

ObjectRepository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ BaseRepository

Initialize repository with a collection

Parameters:

  • records (Array)

    Array of model instances



25
26
27
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 25

def initialize(records)
  @records = records.freeze
end

Instance Attribute Details

#recordsArray (readonly)

Returns The collection of records.

Returns:

  • (Array)

    The collection of records



20
21
22
# File 'lib/lutaml/qea/repositories/base_repository.rb', line 20

def records
  @records
end

Instance Method Details

#allArray

Get all records

Returns:

  • (Array)

    All records in the collection



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

Parameters:

  • conditions (Hash, nil) (defaults to: nil)

    Optional conditions

Yields:

  • (record)

    Optional block for custom filtering

Returns:

  • (Boolean)

    true if any records match



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

Examples:

Count all

repository.count

Count with conditions

repository.count(type: "Class")

Count with block

repository.count { |r| r.name.include?("Test") }

Parameters:

  • conditions (Hash, nil) (defaults to: nil)

    Optional conditions to filter

Yields:

  • (record)

    Optional block for custom filtering

Returns:

  • (Integer)

    Number of 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

Examples:

repository.distinct(:type)
# => ["Class", "Interface", "Component"]

Parameters:

  • attribute (Symbol)

    Attribute name

Returns:

  • (Array)

    Unique values



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

Yields:

  • (record)

    Each record in the collection

Returns:

  • (Enumerator)

    if no block given



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

Returns:

  • (Boolean)

    true if no records



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

Parameters:

  • id (Object)

    Primary key value

Returns:

  • (Object, nil)

    The record or nil if not found



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

Parameters:

  • key (Symbol)

    key attribute name

  • id (Object)

    key value

Returns:

  • (Object, nil)

    The record or nil if not found



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

Examples:

repository.find_first(name: "Test")
repository.find_first { |r| r.name.start_with?("Test") }

Parameters:

  • conditions (Hash, nil) (defaults to: nil)

    Optional conditions

Yields:

  • (record)

    Optional block for custom filtering

Returns:

  • (Object, nil)

    First matching record or nil



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

Examples:

repository.group_by(:type)
# => {"Class" => [obj1, obj2], "Interface" => [obj3]}

Parameters:

  • attribute (Symbol)

    Attribute name to group by

Returns:

  • (Hash)

    Hash with attribute values as keys, arrays as values



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

Parameters:

  • conditions (Hash, nil) (defaults to: nil)

    Optional conditions

Yields:

  • (record)

    Optional block for custom filtering

Returns:

  • (Boolean)

    true if no records match



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

Examples:

repository.order_by(:name)
repository.order_by(:created_at, :desc)

Parameters:

  • attribute (Symbol)

    Attribute name to sort by

  • order (Symbol) (defaults to: :asc)

    :asc or :desc (default: :asc)

Returns:

  • (Array)

    Sorted records



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

Examples:

repository.pluck(:id, :name)
# => [{id: 1, name: "Test"}, {id: 2, name: "Other"}]

Parameters:

  • attributes (Array<Symbol>)

    Attribute names to select

Returns:

  • (Array<Hash>)

    Array of hashes with selected attributes



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

#sizeInteger Also known as: length

Get the size of the collection

Returns:

  • (Integer)

    Number of records



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

Examples:

Hash conditions

repository.where(name: "Test", type: "Class")

Block condition

repository.where { |r| r.name.start_with?("Test") }

Parameters:

  • conditions (Hash) (defaults to: nil)

    Hash of attribute/value pairs

Yields:

  • (record)

    Optional block for custom filtering

Yield Parameters:

  • record (Object)

    Each record

Yield Returns:

  • (Boolean)

    true to include record

Returns:

  • (Array)

    Filtered records



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