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)
keys = Ruote.flatten_keys(@rule['restore'] ? @hash['~~'] : @hash)
keys = keys.reject { |k| TILDE.match(k) } unless RTILDE.match(fl.source)
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
else
(fl.is_a?(Array) ? fl : fl.to_s.split(COMMA_SPLIT)).collect { |field|
[ field, Ruote.lookup(@hash, field), nil ]
}
end
end