Class: Sqreen::ConditionEvaluator
- Inherits:
-
Object
- Object
- Sqreen::ConditionEvaluator
- Defined in:
- lib/sqreen/condition_evaluator.rb
Overview
Evaluate a condition, resolving literals using BindingAccessor.
{ "%and" => ["true", "true"] } -> true
{ "%or" => ["true", "false"] } -> true
{ "%and" => ["false", "true"] } -> false
{ "%equal" => ["coucou", "#.args[0]"] } -> "coucou" == args[0]
{ "%hash_val_include" => ["toto is a small guy", "#.request_params"] } ->
true if one value of request params in included
in the sentence 'toto is a small guy'.
Combine expressions:
{ "%or" =>
[
{ "%hash_val_include" => ["AAA", "#.request_params"] },
{ "%hash_val_include" => ["BBB", "#.request_params"] },
]
}
will return true if one of the request_params include either AAA or BBB.
Class Method Summary collapse
-
.hash_key_include?(values, hash, min_value_size, rem = 10) ⇒ Boolean
Predicate: Is one of values deeply present in keys of hash.
-
.hash_val_include?(value, hash, min_value_size, rem = 20) ⇒ Boolean
Predicate: Is value deeply included in hash.
-
.str_include?(str, what) ⇒ Boolean
Test is a str contains what.
Instance Method Summary collapse
-
#evaluate(*args) ⇒ Object
Evaluate the condition.
-
#initialize(cond) ⇒ ConditionEvaluator
constructor
Initialize evaluator.
Constructor Details
#initialize(cond) ⇒ ConditionEvaluator
Initialize evaluator
115 116 117 118 119 120 121 122 123 |
# File 'lib/sqreen/condition_evaluator.rb', line 115 def initialize(cond) unless cond == true || cond == false unless cond.respond_to? :each raise(Sqreen::Exception, "cond should be a Hash (was #{cond.class})") end end @raw = cond @compiled = compile_expr(cond, 10) end |
Class Method Details
.hash_key_include?(values, hash, min_value_size, rem = 10) ⇒ Boolean
Predicate: Is one of values deeply present in keys of hash
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sqreen/condition_evaluator.rb', line 66 def self.hash_key_include?(values, hash, min_value_size, rem = 10) return true if rem <= 0 if hash.is_a?(Array) return hash.any? do |v| hash_key_include?(values, v, min_value_size, rem - 1) end end return false unless hash.is_a?(Hash) hash.any? do |hkey, hval| case hkey when NilClass false else if hkey.respond_to?(:empty?) && hkey.empty? false else key_incl = if values.is_a?(String) str_include?(values, hkey.to_s) else values.include?(hkey.to_s) end key_incl || hash_key_include?(values, hval, min_value_size, rem - 1) end end end end |
.hash_val_include?(value, hash, min_value_size, rem = 20) ⇒ Boolean
Predicate: Is value deeply included in hash
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sqreen/condition_evaluator.rb', line 35 def self.hash_val_include?(value, hash, min_value_size, rem = 20) return true if rem <= 0 vals = hash vals = hash.values if hash.is_a?(Hash) vals.any? do |hval| case hval when Hash, Array ConditionEvaluator.hash_val_include?(value, hval, min_value_size, rem - 1) when NilClass false else if hval.respond_to?(:empty?) && hval.empty? false else v = hval.to_s if v.size < min_value_size false else ConditionEvaluator.str_include?(value.to_s, v) end end end end end |
.str_include?(str, what) ⇒ Boolean
Test is a str contains what. Rencode if necessary
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sqreen/condition_evaluator.rb', line 97 def self.str_include?(str, what) str1 = if str.encoding != Encoding::UTF_8 str.encode(Encoding::UTF_8, :invalid => :replace, :undef => :replace) else str end str2 = if what.encoding != Encoding::UTF_8 what.encode(Encoding::UTF_8, :invalid => :replace, :undef => :replace) else what end str1.include?(str2) end |
Instance Method Details
#evaluate(*args) ⇒ Object
Evaluate the condition
127 128 129 |
# File 'lib/sqreen/condition_evaluator.rb', line 127 def evaluate(*args) evaluate_expr(@compiled, 10, *args) end |