class Query

Constants

TERMINATING_FUNCTIONS

Attributes

argument[R]
attribute[R]
condition[R]

Public Class Methods

new(attribute, condition, argument) click to toggle source
# File lib/query.rb, line 5
def initialize(attribute, condition, argument)
  @attribute = attribute
  @condition = condition
  @argument = argument
end

Public Instance Methods

argument_is_block?() click to toggle source
# File lib/query.rb, line 21
def argument_is_block?
  @argument.respond_to?(:call)
end
attribute_present?(records) click to toggle source
# File lib/query.rb, line 17
def attribute_present?(records)
  records.any? { |record| record.keys.include? @attribute }
end
conditional(row, condition, argument) click to toggle source
# File lib/query.rb, line 48
def conditional(row, condition, argument)
  thing_to_check = row[@attribute] # for direct comparison methods

  case condition
  when :gt
    thing_to_check > argument
  when :lt
    thing_to_check < argument
  when :eq
    thing_to_check == argument
  when :between
    argument.include?(thing_to_check) 
  when :ilike
    thing_to_check.downcase.include? argument.downcase
  when :lambda
    argument.call(OpenStruct.new(row)) # lambda takes the whole row
  else
    raise "Unknown Conditional: #{condition}"
  end
end
filter(results) click to toggle source
# File lib/query.rb, line 29
def filter(results)
  if @attribute.empty?
    handle_terminating_function(results)
  else
    results.select do |result|
      conditional(result, @condition, @argument)
    end
  end
end
handle_terminating_function(results) click to toggle source
# File lib/query.rb, line 39
def handle_terminating_function(results)
  case @condition
  when :pluck
    results.map { |r| r[@argument] }
  else
    raise "Unknown Terminating Function: #{@condition}"
  end
end
terminating_function?() click to toggle source
# File lib/query.rb, line 25
def terminating_function?
  TERMINATING_FUNCTIONS.include?(@condition)
end
valid?(records) click to toggle source
# File lib/query.rb, line 11
def valid?(records)
  valid = attribute_present?(records) || argument_is_block? || terminating_function?
  raise ArgumentError.new("The attribute `#{@attribute}` is not present in the PStore.") unless valid
  valid
end