class Cuprum::Collections::QueryBuilder

Internal class that handles parsing and applying criteria to a query.

Attributes

base_query[R]

@return [Cuprum::Collections::Query] the original query.

Public Class Methods

new(base_query) click to toggle source

@param base_query [Cuprum::Collections::Query] The original query.

# File lib/cuprum/collections/query_builder.rb, line 13
def initialize(base_query)
  @base_query = base_query
end

Public Instance Methods

call(where:, strategy: nil) click to toggle source

Returns a copy of the query updated with the generated criteria.

Classifies the parameters to determine parsing strategy, then uses that strategy to parse the parameters into an array of criteria. Then, copies the original query and updates the copy with the parsed criteria.

@param strategy [Symbol, nil] The specified strategy for parsing the given

filter into criteria. If nil, the builder will attempt to guess the
strategy based on the given filter.

@param where [Object] The filter used to match items in the collection.

@return [Cuprum::Collections::Query] the copied and updated query.

# File lib/cuprum/collections/query_builder.rb, line 32
def call(where:, strategy: nil)
  criteria =
    if strategy == :unsafe
      where
    else
      parse_criteria(strategy: strategy, where: where)
    end

  build_query(criteria)
end

Private Instance Methods

build_query(criteria) click to toggle source
# File lib/cuprum/collections/query_builder.rb, line 45
def build_query(criteria)
  base_query
    .dup
    .send(:with_criteria, criteria)
end
parse_criteria(strategy:, where:) click to toggle source
# File lib/cuprum/collections/query_builder.rb, line 51
def parse_criteria(strategy:, where:)
  result = Cuprum::Collections::Queries::Parse
    .new
    .call(strategy: strategy, where: where)

  return result.value if result.success?

  raise ParseError, result.error.message
end