Sha256: 4e80120eba5004f05da9a520bf0b094e9d8a02ed51a8de2cd2a1b8f4eddde791

Contents?: true

Size: 597 Bytes

Versions: 3

Compression:

Stored size: 597 Bytes

Contents

class SPQuery
  attr_accessor :queries, :params

  def initialize(queries: {}, params: nil)
    @queries = queries
    @params = params
  end

  def add_attribute_query(attribute, query)
    @queries[attribute] << query
  end

  def set_attribute_query(attribute, query)
    @queries[attribute] = query
  end

  def build_final_query
    return if @queries.blank?

    @queries.map { |key, value|
      if key == @queries.keys.first
        value
      else
        or_and(value)
      end }.sum
  end

  def or_and(value)
    @params[:search].present? ? " OR #{value}" : " AND #{value}"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
seek_party-0.0.9 lib/seek_party/models/sp_query.rb
seek_party-0.0.8 lib/seek_party/models/sp_query.rb
seek_party-0.0.7 lib/seek_party/models/sp_query.rb