class Praxis::Extensions::AttributeFiltering::FilteringParams::Condition

Attributes

name[R]
op[R]
parent_group[RW]
values[R]

Public Class Methods

new(triad:, parent_group:) click to toggle source

For operands with a single or no values: Incoming data is a hash with name and op For Operands with multiple values: Incoming data is an array of hashes

First hash has the spec (i.e., name and op)
The rest of the hashes contain a value each (a hash with value: X each).
Example: [{:name=>"multi"@0, :op=>"="@5}, {:value=>"1"@6}, {:value=>"2"@8}]
# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 18
def initialize(triad:, parent_group:)
  @parent_group = parent_group
  if triad.is_a? Array # several values coming in
    spec, *values = triad
    @name = spec[:name].to_sym
    @op = spec[:op].to_s

    if values.empty?
      @values = ''
      @fuzzies = nil
    elsif values.size == 1
      @values, @fuzzies = _compute_fuzzy(values.first[:value].to_s)
    else
      @values = []
      @fuzzies = []
      values.each do |e|
        val, fuz = _compute_fuzzy(e[:value].to_s)
        @values.push val
        @fuzzies.push fuz
      end
    end
  else # No values for the operand
    @name = triad[:name].to_sym
    @op = triad[:op].to_s
    if ['!', '!!'].include?(@op)
      @values = nil
      @fuzzies = nil
    else
      # Value operand without value? => convert it to empty string
      raise "Interesting, didn't know this could happen. Oops!" if triad[:value].is_a?(Array) && !triad[:value].empty?

      if triad[:value] == []
        @values = ''
        @fuzzies = nil
      else
        @values, @fuzzies = _compute_fuzzy(triad[:value].to_s)
      end
    end
  end
end

Public Instance Methods

_compute_fuzzy(raw_val) click to toggle source

Takes a raw val, and spits out the output val (unescaped), and the fuzzy definition

# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 60
def _compute_fuzzy(raw_val)
  starting = raw_val[0] == '*'
  ending = raw_val[-1] == '*'
  newval, fuzzy = if starting && ending
                    [raw_val[1..-2], :start_end]
                  elsif starting
                    [raw_val[1..], :start]
                  elsif ending
                    [raw_val[0..-2], :end]
                  else
                    [raw_val, nil]
                  end
  newval = CGI.unescape(newval) if newval
  [newval, fuzzy]
end
_dump_value(val, fuzzy) click to toggle source

Dumps the value, marking where the fuzzy might be, and removing the * to differentiate from literals

# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 81
def _dump_value(val, fuzzy)
  case fuzzy
  when nil
    val
  when :start_end
    "{*}#{val}{*}"
  when :start
    "{*}#{val}"
  when :end
    "#{val}{*}"
  end
end
dump() click to toggle source
# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 94
def dump
  vals = if values.is_a? Array
           dumped = values.map.with_index { |val, i| _dump_value(val, @fuzzies[i]) }
           "[#{dumped.join(',')}]" # Purposedly enclose in brackets to make sure we differentiate
         else
           values == '' ? '""' : _dump_value(values, @fuzzies) # Dump the empty string explicitly with quotes if we've converted no value to empty string
         end
  "#{name}#{op}#{vals}"
end
flattened_conditions() click to toggle source
# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 76
def flattened_conditions
  [{ name: @name, op: @op, values: @values, fuzzies: @fuzzies, node_object: self }]
end