one || two #=> [one, two]
one && two #=> [[one, two]]
one && two || three #=> [[one, two], three]
one && two || three; four => [[one, two], [[three, four]]]
# Helps with executing, and short-circut evaluation. | |
class ShortCircuitIterator | |
def initialize(tree) | |
@tree = tree | |
end | |
# Executes all the OR logic breaking on the first `true`-ish value | |
def each(&block) | |
@tree.each do |and_operation| | |
break if and_block(and_operation, &block) | |
end | |
end | |
protected | |
# Executes all the && logic breaking on the first `false`-ish value | |
def and_block(and_operation, &block) | |
and_operation.inject(true) do |memo, commands| | |
break(false) unless execute_commands(commands, &block) | |
true | |
end | |
end | |
# Executes commands or series of commands (separated by semicolons) | |
def execute_commands(commands, &block) | |
Array([commands]).flatten.inject(true) do |memo, command| | |
block.call(command) | |
end | |
end | |
end |