Class: Redwood::HookManager
- Includes:
- Singleton
- Defined in:
- lib/sup/hook.rb
Defined Under Namespace
Classes: HookContext
Class Attribute Summary collapse
-
.descs ⇒ Object
readonly
Returns the value of attribute descs.
Instance Attribute Summary collapse
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
- #clear_one(k) ⇒ Object
- #enabled?(name) ⇒ Boolean
-
#initialize(dir) ⇒ HookManager
constructor
A new instance of HookManager.
- #print_hooks(pattern = "", f = $stdout) ⇒ Object
- #run(name, locals = {}) ⇒ Object
Methods included from Singleton
Constructor Details
#initialize(dir) ⇒ HookManager
Returns a new instance of HookManager.
80 81 82 83 84 85 86 87 |
# File 'lib/sup/hook.rb', line 80 def initialize dir @dir = dir @hooks = {} @contexts = {} = {} Dir.mkdir dir unless File.exist? dir end |
Class Attribute Details
.descs ⇒ Object (readonly)
Returns the value of attribute descs.
77 78 79 |
# File 'lib/sup/hook.rb', line 77 def descs @descs end |
Instance Attribute Details
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
89 90 91 |
# File 'lib/sup/hook.rb', line 89 def end |
Class Method Details
.register(name, desc) ⇒ Object
108 109 110 |
# File 'lib/sup/hook.rb', line 108 def self.register name, desc @descs[name] = desc end |
Instance Method Details
#clear ⇒ Object
130 |
# File 'lib/sup/hook.rb', line 130 def clear; @hooks.clear; BufferManager.flash "Hooks cleared" end |
#clear_one(k) ⇒ Object
131 |
# File 'lib/sup/hook.rb', line 131 def clear_one k; @hooks.delete k; end |
#enabled?(name) ⇒ Boolean
128 |
# File 'lib/sup/hook.rb', line 128 def enabled? name; !hook_for(name).nil? end |
#print_hooks(pattern = "", f = $stdout) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sup/hook.rb', line 112 def print_hooks pattern="", f=$stdout matching_hooks = HookManager.descs.sort.keep_if {|name, desc| pattern.empty? or name.match(pattern)}.map do |name, desc| "\#{name}\n\#{\"-\" * name.length}\nFile: \#{fn_for name}\n\#{desc}\n" end showing_str = matching_hooks.size == HookManager.descs.size ? "" : " (showing #{matching_hooks.size})" f.puts "Have #{HookManager.descs.size} registered hooks#{showing_str}:" f.puts matching_hooks.each { |text| f.puts text } end |
#run(name, locals = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sup/hook.rb', line 91 def run name, locals={} hook = hook_for(name) or return context = @contexts[hook] ||= HookContext.new(name) result = nil fn = fn_for name begin result = context.__run hook, fn, locals rescue Exception => e log "error running #{fn}: #{e.message}" log e.backtrace.join("\n") @hooks[name] = nil # disable it BufferManager.flash "Error running hook: #{e.message}" if BufferManager.instantiated? end result end |