Class: CSDL::Processor
- Inherits:
-
AST::Processor
- Object
- AST::Processor
- CSDL::Processor
- Defined in:
- lib/csdl/processor.rb
Overview
Direct Known Subclasses
Instance Method Summary collapse
-
#on_and(node) ⇒ String
AND two or more child nodes together.
-
#on_argument(node) ⇒ String
Process the first child node as the “argument” in a condition node tree (target + operator + argument).
-
#on_condition(node) ⇒ String
Process :condition node and it’s expected children :target, :operator, and :argument nodes.
-
#on_logical_group(node) ⇒ String
Wrap all processed child nodes in parentheses.
-
#on_not(node) ⇒ String
Process :not node as a :condition node, prepending the logical operator NOT to the processed :condition node.
-
#on_operator(node) ⇒ String
Process :operator nodes, ensuring the the given terminator value is a valid operator.
-
#on_or(node) ⇒ String
OR two or more child nodes together.
-
#on_raw(node) ⇒ String
Process :raw nodes.
-
#on_root(node) ⇒ Object
Process all child nodes.
-
#on_string(node) ⇒ String
Wrap the stringified terminal value in quotes.
-
#on_target(node) ⇒ String
Process :target nodes, ensuring the the given terminator value is a valid operator.
-
#validate_target!(target_key) ⇒ void
Raises an UnknownTargetError if the target isn’t a valid CSDL target.
Instance Method Details
#on_and(node) ⇒ String
AND two or more child nodes together.
39 40 41 |
# File 'lib/csdl/processor.rb', line 39 def on_and(node) logically_join_nodes("AND", node.children) end |
#on_argument(node) ⇒ String
Raise if the node doesn’t have any children.
Process the first child node as the “argument” in a condition node tree (target + operator + argument).
56 57 58 |
# File 'lib/csdl/processor.rb', line 56 def on_argument(node) process(node.children.first) end |
#on_condition(node) ⇒ String
Raise when we don’t have a target node.
Raise when we don’t have a operator node.
Raise when we don’t have a argument node, assuming the operator is binary.
Raise if the argument node’s child is not of a valid node type for the given operator.
Process :condition node and it’s expected children :target, :operator, and :argument nodes.
79 80 81 82 83 84 |
# File 'lib/csdl/processor.rb', line 79 def on_condition(node) target = node.children.find { |child| child.type == :target } operator = node.children.find { |child| child.type == :operator } argument = node.children.find { |child| child.type == :argument } process_all([ target, operator, argument ].compact).join(" ") end |
#on_logical_group(node) ⇒ String
Wrap all processed child nodes in parentheses.
100 101 102 |
# File 'lib/csdl/processor.rb', line 100 def on_logical_group(node) "(" + process_all(node.children).join(" ") + ")" end |
#on_not(node) ⇒ String
Raise when we don’t have a target node.
Raise when we don’t have a operator node.
Raise when we don’t have a argument node, assuming the operator is binary.
Raise if the argument node’s child is not of a valid node type for the given operator.
Support negating logical groupings.
Process :not node as a :condition node, prepending the logical operator NOT to the processed :condition node.
124 125 126 |
# File 'lib/csdl/processor.rb', line 124 def on_not(node) "NOT " + process(node.updated(:condition)) end |
#on_operator(node) ⇒ String
Process :operator nodes, ensuring the the given terminator value is a valid operator.
140 141 142 143 144 145 146 |
# File 'lib/csdl/processor.rb', line 140 def on_operator(node) operator = node.children.first.to_s unless ::CSDL.operator?(operator) fail ::CSDL::UnknownOperatorError, "Operator #{operator} is unknown" end operator end |
#on_or(node) ⇒ String
OR two or more child nodes together.
163 164 165 |
# File 'lib/csdl/processor.rb', line 163 def on_or(node) logically_join_nodes("OR", node.children) end |
#on_raw(node) ⇒ String
Raise if the node doesn’t have any children.
Process :raw nodes.
179 180 181 |
# File 'lib/csdl/processor.rb', line 179 def on_raw(node) node.children.first.to_s end |
#on_root(node) ⇒ Object
Process all child nodes. Useful for grouping child nodes without any syntax introduction.
189 190 191 |
# File 'lib/csdl/processor.rb', line 189 def on_root(node) process_all(node.children).join(" ") end |
#on_string(node) ⇒ String
Raise if the node doesn’t have any children.
Wrap the stringified terminal value in quotes.
205 206 207 |
# File 'lib/csdl/processor.rb', line 205 def on_string(node) '"' + node.children.first.to_s.gsub(/"/, '\"') + '"' end |
#on_target(node) ⇒ String
Process :target nodes, ensuring the the given terminator value is a valid operator.
223 224 225 226 227 |
# File 'lib/csdl/processor.rb', line 223 def on_target(node) target = node.children.first.to_s validate_target!(target) target end |
#validate_target!(target_key) ⇒ void
This method returns an undefined value.
Raises an UnknownTargetError if the target isn’t a valid CSDL target. Useful for implenting a child processor that can ensure the target is known and valid for a given use-case (e.g. Interaction Filters vs Query Filters). Generally not useful to be called directly, use CSDL.target? instead.
243 244 245 246 247 |
# File 'lib/csdl/processor.rb', line 243 def validate_target!(target_key) unless ::CSDL.target?(target_key) fail ::CSDL::UnknownTargetError, "Target '#{target_key}' is not a known target type." end end |