Class: ConsoleAgent::SafetyGuards
- Inherits:
-
Object
- Object
- ConsoleAgent::SafetyGuards
- Defined in:
- lib/console_agent/safety_guards.rb
Instance Attribute Summary collapse
-
#guards ⇒ Object
readonly
Returns the value of attribute guards.
Instance Method Summary collapse
- #add(name, &block) ⇒ Object
- #allow(guard_name, key) ⇒ Object
- #allowed?(guard_name, key) ⇒ Boolean
- #allowlist ⇒ Object
- #disable! ⇒ Object
- #empty? ⇒ Boolean
- #enable! ⇒ Object
- #enabled? ⇒ Boolean
-
#initialize ⇒ SafetyGuards
constructor
A new instance of SafetyGuards.
- #names ⇒ Object
- #remove(name) ⇒ Object
-
#wrap(&block) ⇒ Object
Compose all guards around a block of code.
Constructor Details
#initialize ⇒ SafetyGuards
Returns a new instance of SafetyGuards.
18 19 20 21 22 |
# File 'lib/console_agent/safety_guards.rb', line 18 def initialize @guards = {} @enabled = true @allowlist = {} # { guard_name => [String or Regexp, ...] } end |
Instance Attribute Details
#guards ⇒ Object (readonly)
Returns the value of attribute guards.
16 17 18 |
# File 'lib/console_agent/safety_guards.rb', line 16 def guards @guards end |
Instance Method Details
#add(name, &block) ⇒ Object
24 25 26 |
# File 'lib/console_agent/safety_guards.rb', line 24 def add(name, &block) @guards[name.to_sym] = block end |
#allow(guard_name, key) ⇒ Object
52 53 54 55 56 |
# File 'lib/console_agent/safety_guards.rb', line 52 def allow(guard_name, key) guard_name = guard_name.to_sym @allowlist[guard_name] ||= [] @allowlist[guard_name] << key unless @allowlist[guard_name].include?(key) end |
#allowed?(guard_name, key) ⇒ Boolean
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/console_agent/safety_guards.rb', line 58 def allowed?(guard_name, key) entries = @allowlist[guard_name.to_sym] return false unless entries entries.any? do |entry| case entry when Regexp then key.match?(entry) else entry.to_s == key.to_s end end end |
#allowlist ⇒ Object
70 71 72 |
# File 'lib/console_agent/safety_guards.rb', line 70 def allowlist @allowlist end |
#disable! ⇒ Object
40 41 42 |
# File 'lib/console_agent/safety_guards.rb', line 40 def disable! @enabled = false end |
#empty? ⇒ Boolean
44 45 46 |
# File 'lib/console_agent/safety_guards.rb', line 44 def empty? @guards.empty? end |
#enable! ⇒ Object
36 37 38 |
# File 'lib/console_agent/safety_guards.rb', line 36 def enable! @enabled = true end |
#enabled? ⇒ Boolean
32 33 34 |
# File 'lib/console_agent/safety_guards.rb', line 32 def enabled? @enabled end |
#names ⇒ Object
48 49 50 |
# File 'lib/console_agent/safety_guards.rb', line 48 def names @guards.keys end |
#remove(name) ⇒ Object
28 29 30 |
# File 'lib/console_agent/safety_guards.rb', line 28 def remove(name) @guards.delete(name.to_sym) end |
#wrap(&block) ⇒ Object
Compose all guards around a block of code. Each guard is an around-block: guard.call { inner } Result: guard_1 { guard_2 { guard_3 { yield } } }
77 78 79 80 81 82 83 |
# File 'lib/console_agent/safety_guards.rb', line 77 def wrap(&block) return yield unless @enabled && !@guards.empty? @guards.values.reduce(block) { |inner, guard| -> { guard.call(&inner) } }.call end |