Class: Ruote::RuleSession

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/util/filter.rb

Overview

:nodoc:

The class used to run a rule (a line of a filter).

Constant Summary collapse

SKIP =
%w[ and or fields field f ]
BOOLEANS =
%w[ and or ]
NUMBER_CLASSES =
[ Fixnum, Float ]
BOOLEAN_CLASSES =
[ TrueClass, FalseClass ]
TILDE =
/^~/
RTILDE =
/^\^~/
COMMA_SPLIT =
/ *, */
PIPE_SPLIT =
/ *\| */

Instance Method Summary collapse

Constructor Details

#initialize(hash, rule) ⇒ RuleSession

Returns a new instance of RuleSession

Raises:

  • (ArgumentError)

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ruote/util/filter.rb', line 137

def initialize(hash, rule)

  @hash = hash
  @rule = rule

  fl = @rule['fields'] || @rule['field'] || @rule['f']

  raise ArgumentError.new(
    "filter is missing a 'fields', 'field' or 'f' arg at #{@rule.inspect}"
  ) unless fl

  if fl.is_a?(String)
    fl = fl.gsub(/!/, '\.') if REGEX_IN_STRING.match(fl)
    fl = Ruote.regex_or_s(fl)
  end

  @fields = if fl.is_a?(Regexp)

    # when restoring, you look at the old keys, not the current ones

    keys = Ruote.flatten_keys(@rule['restore'] ? @hash['~~'] : @hash)
    keys = keys.reject { |k| TILDE.match(k) } unless RTILDE.match(fl.source)

    # now only keep the keys that match our regexp

    keys.each_with_object([]) { |k, a|
      m = fl.match(k)
      a << [ k, Ruote.lookup(@hash, k), m[1..-1] ] if m
    }

  elsif fl.is_a?(String) and PIPE_SPLIT.match(fl)

    fields = fl.split(PIPE_SPLIT).collect { |field|
      val = Ruote.lookup(@hash, field)
      val.nil? ? nil : [ field, val, nil ]
    }.compact

    fields.empty? ? [ [ fl, nil, nil ] ] : fields
      # if no fields where found, place fake fl field to force failure

  else

    (fl.is_a?(Array) ? fl : fl.to_s.split(COMMA_SPLIT)).collect { |field|
      [ field,  Ruote.lookup(@hash, field), nil ]
    }
  end
end

Instance Method Details

#runObject


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ruote/util/filter.rb', line 185

def run

  keys = @rule.keys - SKIP
  validation = (@rule.keys & BOOLEANS).empty?

  if validation and @fields.empty? and keys.empty?
    fl = @rule['fields'] || @rule['field'] || @rule['f']
    return [ [ @rule, fl, nil ] ] # validation break
  end

  @fields.collect { |field, value, matches|

    valid = nil

    if keys.empty?

      valid = (value != nil)

    else

      keys.each do |k|

        v = @rule[k]

        m = "_#{k}"
        next unless self.respond_to?(m)

        r = self.send(m, field, value, matches, k, v)

        valid = false if r == false
      end
    end

    raise_or_and(valid, field, value)

  }.compact
end