Class: Gaskit::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/gaskit/repository.rb

Constant Summary collapse

COMMON_AR_METHODS =
i[
  find find_by find_by! find_each
  where order limit offset group having
  pluck select exists? create create!
  update update_all destroy destroy_all
  count any? none? all?
].freeze

Class Method Summary collapse

Class Method Details

.inherited(subclass) ⇒ void

This method returns an undefined value.

Prevents instantiation of repository classes.

This ensures that subclasses of ‘Gaskit::Repository` cannot be instantiated directly. If an attempt is made, a `TypeError` will be raised with the class name.

Parameters:

  • subclass (Class)

    the inheriting class

Raises:

  • (TypeError)

    if the subclass attempts to be instantiated



22
23
24
25
26
27
# File 'lib/gaskit/repository.rb', line 22

def inherited(subclass)
  subclass.define_singleton_method(:new) do
    raise TypeError, "Repositories cannot be instantiated: #{subclass.name}"
  end
  super
end

.log_execution_time(context: {}, log_level: :debug) { ... } ⇒ Object

Logs the execution time of a block of code.

This method measures the duration of the given block. It conditionally logs the timing information based on the log level and configuration settings.

If the block raises an exception, the duration is still logged, and the exception is re-raised after logging the error.

Examples:

Log execution time

UserRepository.log_execution_time(log_level: :info) do
  perform_work
end

Parameters:

  • context (Hash) (defaults to: {})

    Optional additional context to include in the log

  • log_level (Symbol) (defaults to: :debug)

    The log level (default: ‘:debug`)

Yields:

  • The block of code to be measured

Returns:

  • (Object)

    The result of the block execution

Raises:

  • (StandardError)

    If the block raises an error



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gaskit/repository.rb', line 84

def log_execution_time(context: {}, log_level: :debug, &block)
  return yield unless should_log_execution_time?(log_level)

  method_name = caller_locations(1, 1)&.first&.label
  duration, result = Gaskit::Helpers.time_execution(&block)

  logger.log(log_level, "#{method_name} completed", context: context.merge(duration: duration))

  result
rescue StandardError => e
  duration ||= "0.000000"
  logger.error("#{method_name} failed", context: context.merge(duration: duration, error: e.message))

  raise
end

.loggerGaskit::Logger

Returns a logger instance for this repository.

The logger is specifically scoped to the repository class, allowing for structured and context-based logging.

Examples:

Log a message

UserRepository.logger.debug("This is a debug message")

Returns:



61
62
63
# File 'lib/gaskit/repository.rb', line 61

def logger
  @logger ||= Gaskit::Logger.new(self)
end

.model(klass = nil) ⇒ Class?

Defines or retrieves the base model for this repository.

When a model is defined, common ActiveRecord-like methods are delegated to the model automatically. The model can only be set once per repository class.

Examples:

Define a model

UserRepository.model(User)

Retrieve the model

UserRepository.model

Parameters:

  • klass (Class, nil) (defaults to: nil)

    The model class (e.g., an ActiveRecord model)

Returns:

  • (Class, nil)

    Returns the currently defined model

Raises:

  • (StandardError)

    If a model is set more than once



43
44
45
46
47
48
49
50
# File 'lib/gaskit/repository.rb', line 43

def model(klass = nil)
  return get_model_class! unless klass

  raise "#{name} already has a model set" if instance_variable_defined?(:@model_class)

  instance_variable_set(:@model_class, klass)
  delegate_common_model_methods
end