class Safeword::Blocker
Decides whether blocks of code are executed or not.
Public Class Methods
new(enabled: true)
click to toggle source
Instantiates an enabled blocker.
@param [Boolean] enabled Whether the blocker is enabled or not.
# File lib/safeword/blocker.rb, line 8 def initialize(enabled: true) @enabled = enabled end
Public Instance Methods
disable()
click to toggle source
Disables the blocker, allowing blocks of code to be executed.
@example Disabling a blocker
blocker.disable blocker.use { puts 'I will be executed' } #=> I will be executed
@return self
# File lib/safeword/blocker.rb, line 44 def disable @enabled = false self end
enable()
click to toggle source
Enables the blocker, preventing blocks of code from being executed.
@example Enabling a blocker
blocker.enable blocker.use { puts 'I wont be executed' } #=> nothing happens
@return self
# File lib/safeword/blocker.rb, line 32 def enable @enabled = true self end
enabled?()
click to toggle source
Whether the blocker is enabled or not. Enabled blockers prevent blocks of code from being executed.
@example Verifying if a blocker is enabled
blocker.enable blocker.enabled? #=> true blocker.disable blocker.enabled? #=> false
@return true
if the blocker is enabled, false
otherwise.
# File lib/safeword/blocker.rb, line 21 def enabled? @enabled end
use() { || ... }
click to toggle source
Ignores the provided block of code if enabled. Executes it otherwise.
@example Ignoring a block of code when enabled
blocker.enable blocker.use { puts 'I wont be executed' } #=> nothing happens
@example Executing a block of code when disabled
blocker.disable blocker.use { puts 'I will be executed' } #=> I will be executed
@return self
# File lib/safeword/blocker.rb, line 60 def use yield unless enabled? self end