Class: Babl::Nodes::Switch
- Inherits:
-
Object
- Object
- Babl::Nodes::Switch
- Defined in:
- lib/babl/nodes/switch.rb
Instance Method Summary collapse
- #dependencies ⇒ Object
-
#initialize(nodes) ⇒ Switch
constructor
A new instance of Switch.
- #optimize ⇒ Object
- #pinned_dependencies ⇒ Object
- #render(ctx) ⇒ Object
- #schema ⇒ Object
Constructor Details
#initialize(nodes) ⇒ Switch
Returns a new instance of Switch.
11 12 13 14 |
# File 'lib/babl/nodes/switch.rb', line 11 def initialize(nodes) raise Errors::InvalidTemplate, 'A least one switch() condition must be taken' if nodes.empty? super end |
Instance Method Details
#dependencies ⇒ Object
16 17 18 19 |
# File 'lib/babl/nodes/switch.rb', line 16 def dependencies nodes.flatten(1).map(&:dependencies) .reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) } end |
#optimize ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/babl/nodes/switch.rb', line 35 def optimize optimize_condition_and_values || optimize_falsy_conditions || optimize_truthy_conditions || optimize_always_same_outputs || optimize_same_conditions || optimize_continue_to_switch || self end |
#pinned_dependencies ⇒ Object
21 22 23 24 |
# File 'lib/babl/nodes/switch.rb', line 21 def pinned_dependencies nodes.flatten(1).map(&:pinned_dependencies) .reduce(Utils::Hash::EMPTY) { |a, b| Babl::Utils::Hash.deep_merge(a, b) } end |
#render(ctx) ⇒ Object
30 31 32 33 |
# File 'lib/babl/nodes/switch.rb', line 30 def render(ctx) nodes.each { |cond, value| return value.render(ctx) if cond.render(ctx) } raise Errors::RenderingError, 'A least one switch() condition must be taken' end |
#schema ⇒ Object
26 27 28 |
# File 'lib/babl/nodes/switch.rb', line 26 def schema Schema::AnyOf.canonicalized(nodes.map(&:last).map(&:schema)) end |