class Fulfil::Query

Public Class Methods

new() click to toggle source
# File lib/fulfil/query.rb, line 3
def initialize
  @matchers = []
end

Public Instance Methods

exclude(*args) click to toggle source
# File lib/fulfil/query.rb, line 24
def exclude(*args)
  options = args.first { |arg| arg.is_a?(Hash) && arg.keys.include?(:options) }.fetch(:options, {})

  terms = args.flat_map do |arg|
            arg.map do |field, value|
              next if value == options
              build_exclude_term(field: field, value: value, options: options)
            end
          end

  if terms.length > 1
    @matchers.push(["OR"].concat(terms))
  else
    @matchers.concat(terms.first)
  end

  self
end
query() click to toggle source
# File lib/fulfil/query.rb, line 7
def query
  @matchers
end

Private Instance Methods

build_exclude_term(prefix: nil, field:, value:, options:) click to toggle source
# File lib/fulfil/query.rb, line 93
def build_exclude_term(prefix: nil, field:, value:, options:)
  key = [prefix, field.to_s].compact.join('.')

  case value.class.name
  when 'Array'
    [[key, 'not in', value]]
  when 'Fixnum', 'Integer'
    [[key, '!=', value]]
  when 'Range'
    [
      [key, '<', value.first],
      [key, '>', value.last],
    ]
  when 'Hash'
    value.flat_map { |nested_field, nested_value|
      build_exclude_term(prefix: field, field: nested_field, value: nested_value, options: options)
    }
  else
    raise "Unhandled value type: #{value} (#{value.class.name})"
  end
end
build_search_term(prefix: nil, field:, value:, options:) click to toggle source

Fulfil Query Syntax:

Exact Match: (Integer)

* =

Case Insensitive Match: (String)

* 'ilike'

Case Sensitive Match: (String)

* 'like'

Comparison Operators: (Numeric/Currency, Integer, Float, Date, Datetime)

* >
* >=
* <
* <=
* !=

IN, NOT IN: (Array)

# File lib/fulfil/query.rb, line 65
def build_search_term(prefix: nil, field:, value:, options:)
  key = [prefix, field.to_s].compact.join('.')

  case value.class.name
  when 'Array'
    [[key, 'in', value]]
  when 'Fixnum', 'Integer'
    [[key, '=', value]]
  when 'Range'
    [
      [key, '>=', value.first],
      [key, '<=', value.last],
    ]
  when 'String'
    if options[:case_sensitive]
      [[key, 'like', value]]
    else
      [[key, 'ilike', value]]
    end
  when 'Hash'
    value.flat_map { |nested_field, nested_value|
      build_search_term(prefix: field, field: nested_field, value: nested_value, options: options)
    }
  else
    raise "Unhandled value type: #{value} (#{value.class.name})"
  end
end