Sha256: 4b09a1ac7720d93ebc1a51fc4c2cef670f35b2b55a65b4b49e27aebab3ca9357
Contents?: true
Size: 1.21 KB
Versions: 2
Compression:
Stored size: 1.21 KB
Contents
module OrangeZest # Allows wrapping a boolean condition to monitor it for changes. module TriggerCondition ON = :on OFF = :off @@states = {} # Watches the given boolean condition, and returns: # # - {TriggerCondition::ON} if the condition was previously false, and becomes true # - {TriggerCondition::OFF} if the condition was previously true, and becomes false # - `nil` if the condition has not changed # # Conditions are assumed to be false on the first call. # # Each separate condition is identified by its source file and line number, which means you # **must not watch more than one condition on a single line**. # # @param [Boolean] condition The condition to watch. # @return The change in condition, if it has changed. def self.watch(condition) c = caller.first triggered = @@states[c] if (!condition && !triggered) || (condition && triggered) nil elsif condition && !triggered @@states[c] = true TriggerCondition::ON elsif !condition && triggered @@states[c] = false TriggerCondition::OFF else raise 'unexpected condition case' end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
orange_zest-0.2.0 | lib/orange_zest/trigger_condition.rb |
orange_zest-0.1.0 | lib/orange_zest/trigger_condition.rb |