Sha256: d83d0a092980a0441a9438cc6d99fc4fdc009ab52aa91d46db09d6d2e23ccedf

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

module Inquery
  class Query
    include Mixins::SchemaValidation

    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 self.class._schema
        self.class._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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
inquery-1.0.1 lib/inquery/query.rb
inquery-1.0.0 lib/inquery/query.rb