Class: Praxis::Extensions::AttributeFiltering::FilteringParams::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/extensions/attribute_filtering/filters_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(triad:, parent_group:) ⇒ Condition

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}]


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 10

def name
  @name
end

#opObject (readonly)

Returns the value of attribute op.



10
11
12
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 10

def op
  @op
end

#parent_groupObject

Returns the value of attribute parent_group.



11
12
13
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 11

def parent_group
  @parent_group
end

#valuesObject (readonly)

Returns the value of attribute values.



10
11
12
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 10

def values
  @values
end

Instance Method Details

#_compute_fuzzy(raw_val) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

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



81
82
83
84
85
86
87
88
89
90
91
92
# 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

#dumpObject



94
95
96
97
98
99
100
101
102
# 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_conditionsObject



76
77
78
# 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