Module: Babl::Operators::Switch::DSL
- Included in:
- Template
- Defined in:
- lib/babl/operators/switch.rb
Instance Method Summary collapse
-
#continue ⇒ Object
Return a special placeholder that can be used as a switch(…) value.
-
#default ⇒ Object
To be used as a switch(…) condition.
-
#switch(conds = {}) ⇒ Object
Conditional switching.
Instance Method Details
#continue ⇒ Object
Return a special placeholder that can be used as a switch(…) value. It tells BABL to continue the evaluation of the original chain after switch().
13 14 15 16 17 18 19 |
# File 'lib/babl/operators/switch.rb', line 13 def continue construct_terminal { |context| node = context[:continue] raise Babl::InvalidTemplateError, 'continue() cannot be used outside switch()' unless node node } end |
#default ⇒ Object
To be used as a switch(…) condition. It is strictly equivalent to write ‘true’ instead, but convey more meaning.
7 8 9 |
# File 'lib/babl/operators/switch.rb', line 7 def default unscoped.static(true) end |
#switch(conds = {}) ⇒ Object
Conditional switching
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/babl/operators/switch.rb', line 22 def switch(conds = {}) construct_node(continue: nil) { |node, context| nodes = conds.map { |cond, value| cond_node = unscoped.call(cond).builder .precompile(Rendering::InternalValueNode.instance, context.merge(continue: nil)) value_node = unscoped.call(value).builder .precompile(Rendering::TerminalValueNode.instance, context.merge(continue: node)) [cond_node, value_node] }.to_h SwitchNode.new(nodes) } end |