Class: Gaskit::Repository
- Inherits:
-
Object
- Object
- Gaskit::Repository
- 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
-
.inherited(subclass) ⇒ void
Prevents instantiation of repository classes.
-
.log_execution_time(context: {}, log_level: :debug) { ... } ⇒ Object
Logs the execution time of a block of code.
-
.logger ⇒ Gaskit::Logger
Returns a logger instance for this repository.
-
.model(klass = nil) ⇒ Class?
Defines or retrieves the base model for this repository.
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.
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.
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.)) raise end |
.logger ⇒ Gaskit::Logger
Returns a logger instance for this repository.
The logger is specifically scoped to the repository class, allowing for structured and context-based logging.
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.
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 |