Sha256: dc9085361123d31f71d70222391e772e9ae96f287b66118105b3d28481804fa6

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 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
        Schemacop.validate!(self.class._schema, @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-0.1.0 lib/inquery/query.rb
inquery-0.0.1 lib/inquery/query.rb