Class: Inquery::Query

Inherits:
Object
  • Object
show all
Includes:
Mixins::RawSqlUtils, Mixins::SchemaValidation
Defined in:
lib/inquery/query.rb

Direct Known Subclasses

Chainable

Defined Under Namespace

Classes: Chainable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Query

Instantiates the query class and validates the given params hash (if there was a validation schema specified).



22
23
24
25
26
27
28
# File 'lib/inquery/query.rb', line 22

def initialize(params = {})
  @params = params

  if _schema
    @params = _schema.validate!(@params)
  end
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/inquery/query.rb', line 6

def params
  @params
end

Class Method Details

.call(*args) ⇒ Object

Instantiates the query class using the given arguments and runs call on it.



16
17
18
# File 'lib/inquery/query.rb', line 16

def self.call(*args)
  new(*args).call
end

.run(*args) ⇒ Object

Instantiates the query class using the given arguments and runs call and process on it.



10
11
12
# File 'lib/inquery/query.rb', line 10

def self.run(*args)
  new(*args).run
end

Instance Method Details

#callObject

Override this method to perform the actual query.



36
37
38
# File 'lib/inquery/query.rb', line 36

def call
  fail NotImplementedError
end

#connectionObject

Provides a connection to the database. May be overridden if a different connection is desired. Defaults to ActiveRecord::Base.connection.



53
54
55
# File 'lib/inquery/query.rb', line 53

def connection
  ActiveRecord::Base.connection
end

#osparamsObject

Returns a copy of the query's params, wrapped in an OpenStruct object for easyer access.



47
48
49
# File 'lib/inquery/query.rb', line 47

def osparams
  @osparams ||= OpenStruct.new(params)
end

#process(results) ⇒ Object

Override this method to perform an optional result postprocessing.



41
42
43
# File 'lib/inquery/query.rb', line 41

def process(results)
  results
end

#runObject

Runs both call and process.



31
32
33
# File 'lib/inquery/query.rb', line 31

def run
  process(call)
end