Sha256: 0c44a2ed5d94fd9b1a5b769b8e3fce53ac7cd8c0548074efdf115456142656d3

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module SeekParty
  class SeekPartyEngine
    attr_accessor :seek_party_attribute,
                  :seek_party_query,
                  :inspected_class

    def initialize(inspected_class, params: {}, white_list: nil, black_list: nil, scopes: {})
      @seek_party_attribute = SeekPartyAttribute.new(inspected_class, white_list, black_list)
      @seek_party_query = SeekPartyQuery.new(params)
      @inspected_class = inspected_class
      @scopes = scopes
    end

    def search
      attributes = seek_party_attribute.discover_attributes
      final_query = seek_party_query.build_query(attributes)
      run_search(final_query)
    end

    private

    def run_search(final_query)
      # If scopes were passed, iterate trough\ them.
      setup_scopes if @scopes

      # Execute final query (alongside any scopes that have been passed)
      @inspected_class.where(final_query)
    end

    def setup_scopes
      # Iterate trough the scopes that may have been
      # passed. PS: This is more of a luxury than anything
      # else, as the returned object from run_search is an
      # ActiveRecord_Relation object that would accept
      # method chaining
      @scopes.each do |key, value|
        @inspected_class = @inspected_class.method(key).call(value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
seek_party-0.0.2 lib/seek_party/seek_party_engine.rb
seek_party-0.0.1 lib/seek_party/seek_party_engine.rb