Class: Sqreen::Rules::ArgumentFilter
- Inherits:
-
Object
- Object
- Sqreen::Rules::ArgumentFilter
- Defined in:
- lib/sqreen/rules/execjs_cb.rb
Constant Summary collapse
- MAX_DEPTH =
2
Class Method Summary collapse
Instance Method Summary collapse
- #build_arg_requirements(rule) ⇒ Object
- #filter(cbname, arguments) ⇒ Object
-
#initialize(rule) ⇒ ArgumentFilter
constructor
A new instance of ArgumentFilter.
Constructor Details
#initialize(rule) ⇒ ArgumentFilter
Returns a new instance of ArgumentFilter.
162 163 164 165 |
# File 'lib/sqreen/rules/execjs_cb.rb', line 162 def initialize(rule) @conditions = rule.fetch(Attrs::CONDITIONS, {}) build_arg_requirements rule end |
Class Method Details
.hash_val_included(needed, haystack, min_length = 8, max_depth = 20) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/sqreen/rules/execjs_cb.rb', line 217 def hash_val_included(needed, haystack, min_length = 8, max_depth = 20) new_obj = {} insert = [] to_do = haystack.map { |k, v| [new_obj, k, v, 0] } until to_do.empty? where, key, value, deepness = to_do.pop safe_key = key.is_a?(Integer) ? key : key.to_s if value.is_a?(Hash) && deepness < max_depth val = {} insert << [where, safe_key, val] to_do += value.map { |k, v| [val, k, v, deepness + 1] } elsif value.is_a?(Array) && deepness < max_depth val = [] insert << [where, safe_key, val] i = -1 to_do += value.map { |v| [val, i += 1, v, deepness + 1] } elsif deepness >= max_depth # if we are after max_depth don't try to filter insert << [where, safe_key, value] else v = value.to_s if v.size >= min_length && ConditionEvaluator.str_include?(needed.to_s, v) case where when Array where << value else where[safe_key] = value end end end end insert.reverse.each do |wh, ikey, ival| case wh when Array wh << ival unless ival.respond_to?(:empty?) && ival.empty? else wh[ikey] = ival unless ival.respond_to?(:empty?) && ival.empty? end end new_obj end |
Instance Method Details
#build_arg_requirements(rule) ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/sqreen/rules/execjs_cb.rb', line 190 def build_arg_requirements(rule) @ba_expressions = {} callbacks = rule[Attrs::CALLBACKS] callbacks.each do |name, args_or_func| next unless args_or_func.is_a?(Array) args_bas = args_or_func[0..-2] unless args_or_func.empty? @ba_expressions[name] = ExecJSCB.build_accessors(args_bas).map(&:expression) end end |
#filter(cbname, arguments) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/sqreen/rules/execjs_cb.rb', line 167 def filter(cbname, arguments) condition = @conditions[cbname] return arguments if condition.nil? || @ba_expressions[cbname].nil? each_hash_val_include(condition) do |needle, haystack, min_length| # We could actually run the binding accessor expression here. needed_idx = @ba_expressions[cbname].index(needle) next unless needed_idx haystack_idx = @ba_expressions[cbname].index(haystack) next unless haystack_idx arguments[haystack_idx] = ArgumentFilter.hash_val_included( arguments[needed_idx], arguments[haystack_idx], min_length.to_i, MAX_DEPTH ) end arguments end |