Class: Inquery::Query
- Inherits:
-
Object
- Object
- Inquery::Query
- Includes:
- Mixins::RawSqlUtils, Mixins::SchemaValidation
- Defined in:
- lib/inquery/query.rb
Overview
Base query class that encapsulates database queries in reusable classes.
Subclasses must implement the ‘call’ method to define the query logic. Optionally, override ‘process’ to transform the query results.
Example:
class FetchActiveUsers < Inquery::Query
def call
User.where(active: true)
end
end
FetchActiveUsers.run # => Returns active users
Direct Known Subclasses
Defined Under Namespace
Classes: Chainable
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
-
.call(*args) ⇒ Object
Instantiates the query class with the given params and executes only the ‘call’ method, skipping post-processing.
-
.run(*args) ⇒ Object
Instantiates the query class with the given params and executes both ‘call’ and ‘process’ methods.
Instance Method Summary collapse
-
#call ⇒ Object
Override this method in subclasses to define the query logic.
-
#connection ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Returns the database connection to use for this query.
-
#initialize(params = {}) ⇒ Query
constructor
Initializes a new query instance with the given parameters.
-
#osparams ⇒ MethodAccessibleHash
Returns the query params wrapped in a MethodAccessibleHash for convenient access using dot notation.
-
#process(results) ⇒ Object
Override this method in subclasses to transform the query results.
-
#run ⇒ Object
Executes the query by calling ‘call’ and then ‘process’.
Constructor Details
#initialize(params = {}) ⇒ Query
Initializes a new query instance with the given parameters.
If a schema is defined using ‘schema’ or ‘schema3’, the params will be validated against it before the query is executed.
52 53 54 55 56 57 58 |
# File 'lib/inquery/query.rb', line 52 def initialize(params = {}) @params = params if _schema @params = _schema.validate!(@params) end end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
19 20 21 |
# File 'lib/inquery/query.rb', line 19 def params @params end |
Class Method Details
.call(*args) ⇒ Object
Instantiates the query class with the given params and executes only the ‘call’ method, skipping post-processing.
41 42 43 |
# File 'lib/inquery/query.rb', line 41 def self.call(*args) new(*args).call end |
.run(*args) ⇒ Object
Instantiates the query class with the given params and executes both ‘call’ and ‘process’ methods.
This is the primary way to execute queries. It runs the query logic defined in ‘call’ and then passes the results through ‘process’ for any post-processing.
31 32 33 |
# File 'lib/inquery/query.rb', line 31 def self.run(*args) new(*args).run end |
Instance Method Details
#call ⇒ Object
Override this method in subclasses to define the query logic.
71 72 73 |
# File 'lib/inquery/query.rb', line 71 def call fail NotImplementedError end |
#connection ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter
Returns the database connection to use for this query.
Override this method if you need to use a different connection than the default ActiveRecord connection.
106 107 108 |
# File 'lib/inquery/query.rb', line 106 def connection ActiveRecord::Base.connection end |
#osparams ⇒ MethodAccessibleHash
Returns the query params wrapped in a MethodAccessibleHash for convenient access using dot notation.
Example:
schema3 { str! :name }
def call
User.where(name: osparams.name) # Access via dot notation
end
96 97 98 |
# File 'lib/inquery/query.rb', line 96 def osparams @osparams ||= MethodAccessibleHash.new(params) end |
#process(results) ⇒ Object
Override this method in subclasses to transform the query results.
By default, returns the results unchanged. Common uses include converting to JSON, counting records, or extracting specific fields.
82 83 84 |
# File 'lib/inquery/query.rb', line 82 def process(results) results end |
#run ⇒ Object
Executes the query by calling ‘call’ and then ‘process’.
63 64 65 |
# File 'lib/inquery/query.rb', line 63 def run process(call) end |