Class: DRbQS::Server::Hook
- Inherits:
-
Object
- Object
- DRbQS::Server::Hook
- Defined in:
- lib/drbqs/server/server_hook.rb
Instance Method Summary collapse
- #add(key, opts = {}, &block) ⇒ Object
- #delete(key, name = nil) ⇒ Object
- #exec(key, *args, &cond) ⇒ Object
- #hook_names(key) ⇒ Object
-
#initialize ⇒ Hook
constructor
A new instance of Hook.
- #number_of_hook(key) ⇒ Object
- #set_finish_exit(&block) ⇒ Object
- #set_shutdown_unused_nodes(&block) ⇒ Object
Constructor Details
#initialize ⇒ Hook
Returns a new instance of Hook.
4 5 6 7 8 9 10 11 12 |
# File 'lib/drbqs/server/server_hook.rb', line 4 def initialize @hook = Hash.new { |h, k| h[k] = Array.new } @argument_number = {} @finish_exit = nil set_argument_number(:empty_queue, 1) set_argument_number(:process_data, 2) set_argument_number(:finish, 1) set_argument_number(:task_assigned, 1) end |
Instance Method Details
#add(key, opts = {}, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/drbqs/server/server_hook.rb', line 29 def add(key, opts = {}, &block) unless block_given? raise ArgumentError, "The main part of hook must be specified as a block." end if (n = @argument_number[key]) && (block.arity != n) raise ArgumentError, "Invalid argument number of hook of #{key.inspect}." end name = opts[:name] || create_proc_name(key) @hook[key] << [name, block, opts[:repeat]] name end |
#delete(key, name = nil) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/drbqs/server/server_hook.rb', line 41 def delete(key, name = nil) if name @hook[key].delete_if { |ary| ary[0] == name } else @hook[key].clear end end |
#exec(key, *args, &cond) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/drbqs/server/server_hook.rb', line 80 def exec(key, *args, &cond) delete_unused_hook @hook[key].each do |ary| name, proc, repeat = ary if !cond || cond.call(name) if !repeat || repeat > 0 proc.call(*args) ary[2] -= 1 if repeat end else return nil end end specific_proc(key, &cond) end |
#hook_names(key) ⇒ Object
67 68 69 |
# File 'lib/drbqs/server/server_hook.rb', line 67 def hook_names(key) @hook[key].map { |a| a[0] } end |
#number_of_hook(key) ⇒ Object
104 105 106 |
# File 'lib/drbqs/server/server_hook.rb', line 104 def number_of_hook(key) @hook.has_key?(key) ? @hook[key].size : 0 end |
#set_finish_exit(&block) ⇒ Object
96 97 98 |
# File 'lib/drbqs/server/server_hook.rb', line 96 def set_finish_exit(&block) @finish_exit = block end |
#set_shutdown_unused_nodes(&block) ⇒ Object
100 101 102 |
# File 'lib/drbqs/server/server_hook.rb', line 100 def set_shutdown_unused_nodes(&block) @shutdown_unused_nodes = block end |