Class: Flok::JSTermGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/flok/user_hook_generators/helpers.rb

Overview

This class helps construct javascript expressions like ((a == 3 || b == 4) && (a == 5 || c == 6)) Usually you have an outer-level JSTermGroup that represents all the things being anded togeather and then you have multiple inner groups that each represent ored terms. Search POS form on wikipedia e.g. #Convert ((a == 3 || b == 4) && (a == 5 || c == 6))

This will hold the entire result ands = JSTermGroup.new

Compute a small section of the result, the group (a == 3 || b == 4) and then add it as a group of the entire result in || form ors1 = JSTermGroup.new ors1 << “a == 3” ors1 << “b == 4” ands << ors1.to_or_js

Same thing but now we compute the second part which is (a == 5 || c == 5) ors2 = JSTermGroup.new ors2 << “a == 5” ors2 << “c == 6” ands << ors2.to_or_js

Finally get the result by &&ing all the ||s groups togeather result = ands.to_and_js

Instance Method Summary collapse

Constructor Details

#initializeJSTermGroup

Returns a new instance of JSTermGroup.



27
28
29
# File 'lib/flok/user_hook_generators/helpers.rb', line 27

def initialize
  @terms = []
end

Instance Method Details

#<<(expr) ⇒ Object

Add a javascript expression that evaluates to either true or false. May or may not contain parantheses



32
33
34
# File 'lib/flok/user_hook_generators/helpers.rb', line 32

def << expr
  @terms << "(#{expr})"
end

#to_and_jsObject

Join expressions via && statements and yield an expression that contains parantheses



42
43
44
# File 'lib/flok/user_hook_generators/helpers.rb', line 42

def to_and_js
  "(#{[*@terms, "true"].join(" && ")})"
end

#to_or_jsObject

Join expressions via || statements and yield an expression that contains parantheses



37
38
39
# File 'lib/flok/user_hook_generators/helpers.rb', line 37

def to_or_js
  "(#{[*@terms, "false"].join(" || ")})"
end