Class: RailsConsoleAi::SafetyGuards

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_ai/safety_guards.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSafetyGuards

Returns a new instance of SafetyGuards.



29
30
31
32
33
# File 'lib/rails_console_ai/safety_guards.rb', line 29

def initialize
  @guards = {}
  @enabled = true
  @allowlist = {}  # { guard_name => [String or Regexp, ...] }
end

Instance Attribute Details

#guardsObject (readonly)

Returns the value of attribute guards.



27
28
29
# File 'lib/rails_console_ai/safety_guards.rb', line 27

def guards
  @guards
end

Instance Method Details

#add(name, &block) ⇒ Object



35
36
37
# File 'lib/rails_console_ai/safety_guards.rb', line 35

def add(name, &block)
  @guards[name.to_sym] = block
end

#allow(guard_name, key) ⇒ Object



63
64
65
66
67
# File 'lib/rails_console_ai/safety_guards.rb', line 63

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

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rails_console_ai/safety_guards.rb', line 69

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

#allowlistObject



81
82
83
# File 'lib/rails_console_ai/safety_guards.rb', line 81

def allowlist
  @allowlist
end

#disable!Object



51
52
53
# File 'lib/rails_console_ai/safety_guards.rb', line 51

def disable!
  @enabled = false
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rails_console_ai/safety_guards.rb', line 55

def empty?
  @guards.empty?
end

#enable!Object



47
48
49
# File 'lib/rails_console_ai/safety_guards.rb', line 47

def enable!
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rails_console_ai/safety_guards.rb', line 43

def enabled?
  @enabled
end

#namesObject



59
60
61
# File 'lib/rails_console_ai/safety_guards.rb', line 59

def names
  @guards.keys
end

#remove(name) ⇒ Object



39
40
41
# File 'lib/rails_console_ai/safety_guards.rb', line 39

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 } } }



88
89
90
91
92
93
94
# File 'lib/rails_console_ai/safety_guards.rb', line 88

def wrap(&block)
  return yield unless @enabled && !@guards.empty?

  @guards.values.reduce(block) { |inner, guard|
    -> { guard.call(&inner) }
  }.call
end