Sha256: f3abfc7642f9359b159d1d904a2c9c54f6cd18e729ffb4af4b2dba069e43f495

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 KB

Contents

module Inquery
  class Query
    include Mixins::SchemaValidation
    include Mixins::RawSqlUtils

    attr_reader :params

    # Instantiates the query class using the given arguments
    # and runs `call` and `process` on it.
    def self.run(*args)
      new(*args).run
    end

    # Instantiates the query class using the given arguments
    # and runs `call` on it.
    def self.call(*args)
      new(*args).call
    end

    # Instantiates the query class and validates the given params hash (if there
    # was a validation schema specified).
    def initialize(params = {})
      @params = params

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

    # Runs both `call` and `process`.
    def run
      process(call)
    end

    # Override this method to perform the actual query.
    def call
      fail NotImplementedError
    end

    # Override this method to perform an optional result postprocessing.
    def process(results)
      results
    end

    # Returns a copy of the query's params, wrapped in an OpenStruct object for
    # easyer access.
    def osparams
      @osparams ||= OpenStruct.new(params)
    end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
inquery-1.0.11 lib/inquery/query.rb
inquery-1.0.10 lib/inquery/query.rb
inquery-1.0.9 lib/inquery/query.rb
inquery-1.0.8 lib/inquery/query.rb
inquery-1.0.7 lib/inquery/query.rb
inquery-1.0.6 lib/inquery/query.rb
inquery-1.0.5 lib/inquery/query.rb
inquery-1.0.4 lib/inquery/query.rb
inquery-1.0.3 lib/inquery/query.rb