class Sanity::Groq::Filter

Constants

COMPARISON_OPERATORS
END_PAREN
LOGICAL_OPERATORS
RESERVED
START_PAREN

Attributes

args[R]
filter_value[R]

Public Class Methods

call(**args) click to toggle source
# File lib/sanity/groq/filter.rb, line 10
def call(**args)
  new(**args).call
end
new(**args) click to toggle source
# File lib/sanity/groq/filter.rb, line 37
def initialize(**args)
  @args = args.except(*Sanity::Groqify::RESERVED - RESERVED)
  @filter_value = +""
end

Public Instance Methods

call() click to toggle source
# File lib/sanity/groq/filter.rb, line 42
def call
  iterate
  filter_value.strip
end

Private Instance Methods

cast_value(val) click to toggle source
# File lib/sanity/groq/filter.rb, line 49
def cast_value(val)
  val.is_a?(Integer) ? val : "'#{val}'"
end
default_multi_filter() click to toggle source
# File lib/sanity/groq/filter.rb, line 53
def default_multi_filter
  filter_value.length.positive? ? " #{LOGICAL_OPERATORS[:and]}" : ""
end
equal() click to toggle source
# File lib/sanity/groq/filter.rb, line 57
def equal
  COMPARISON_OPERATORS[:is]
end
filter(key: nil) click to toggle source
# File lib/sanity/groq/filter.rb, line 61
def filter(key: nil)
  key ? " #{multi_filter(key)}" : default_multi_filter.to_s
end
iterate(arg = args, nested_key: nil) click to toggle source
# File lib/sanity/groq/filter.rb, line 65
def iterate(arg = args, nested_key: nil)
  arg.each do |key, val|
    if val.is_a?(String) || val.is_a?(Integer)
      filter_value << "#{filter(key: nested_key)} #{key} #{equal} #{cast_value(val)}"
    elsif val.is_a?(Array) && !val[0].is_a?(Hash)
      filter_value << "#{key} in #{val.map(&:to_s)}"
    elsif LOGICAL_OPERATORS.key?(key)
      if val.is_a?(Array)
        val.each { |hsh| iterate(hsh, nested_key: key) }
      elsif LOGICAL_OPERATORS.key?(val.keys[0])
        filter_value << " #{LOGICAL_OPERATORS[key]} #{START_PAREN}"

        val.values[0].each_with_index do |(vkey, vval), idx|
          operator = logical_operator(val.keys[0], index: idx)
          filter_value << "#{operator} " unless operator.empty?

          if vkey.is_a?(Hash)
            vkey.each do |vvkey, vvval|
              filter_value << "#{vvkey} #{equal} #{cast_value(vvval)}"
            end
          else
            filter_value << "#{vkey} #{equal} #{cast_value(vval)}"
          end
        end

        filter_value << END_PAREN
      else
        iterate(val, nested_key: key)
      end
    elsif COMPARISON_OPERATORS.key?(val.keys[0])
      val.each do |vkey, vval|
        filter_value << "#{filter(key: nested_key)} #{key} #{COMPARISON_OPERATORS[vkey]} #{cast_value(vval)}"
      end
    end
  end
end
logical_operator(key, index: 0) click to toggle source
# File lib/sanity/groq/filter.rb, line 102
def logical_operator(key, index: 0)
  index.positive? ? " #{LOGICAL_OPERATORS[key]}" : ""
end
multi_filter(key) click to toggle source
# File lib/sanity/groq/filter.rb, line 106
def multi_filter(key)
  filter_value.length.positive? ? LOGICAL_OPERATORS[key] : ""
end