Class: Redwood::HookManager

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/sup/hook.rb

Defined Under Namespace

Classes: HookContext

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Singleton

included

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 = {}
  @tags = {}

  Dir.mkdir dir unless File.exist? dir
end

Class Attribute Details

.descsObject (readonly)

Returns the value of attribute descs.



77
78
79
# File 'lib/sup/hook.rb', line 77

def descs
  @descs
end

Instance Attribute Details

#tagsObject (readonly)

Returns the value of attribute tags.



89
90
91
# File 'lib/sup/hook.rb', line 89

def tags
  @tags
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

#clearObject



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

Returns:

  • (Boolean)


128
# File 'lib/sup/hook.rb', line 128

def enabled? name; !hook_for(name).nil? end


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|
    <<EOS
#{name}
#{"-" * name.length}
File: #{fn_for name}
#{desc}
EOS
  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