Class: Workforce::Config
- Inherits:
-
Object
- Object
- Workforce::Config
- Includes:
- Singleton
- Defined in:
- lib/workforce/configuration.rb
Class Method Summary collapse
Instance Method Summary collapse
- #after(event, &block) ⇒ Object
- #before(event, &block) ⇒ Object
-
#initialize {|_self| ... } ⇒ Config
constructor
A new instance of Config.
- #load_file(file_path) ⇒ Object
- #schedule(worker, number = nil) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Config
Returns a new instance of Config.
23 24 25 26 27 28 |
# File 'lib/workforce/configuration.rb', line 23 def initialize yield self if block_given? @before_callbacks = Hash.new { |h,k| h[k] = [] } @after_callbacks = Hash.new { |h,k| h[k] = [] } @num_instances = Hash.new { |h,k| h[k] = 0 } end |
Class Method Details
.config_attr(attr_name) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/workforce/configuration.rb', line 7 def self.config_attr(attr_name) define_method(attr_name) do |*args| raise ArgumentError, "wrong number of arguments (#{args.size} for 1)" if args.size > 1 if instance_variable_get("@#{attr_name}").nil? || args[0] instance_variable_set("@#{attr_name}", args[0] || yield) end instance_variable_get("@#{attr_name}") end end |
.get(attr_name) ⇒ Object
19 20 21 |
# File 'lib/workforce/configuration.rb', line 19 def self.get(attr_name) self.instance.send(attr_name) end |
Instance Method Details
#after(event, &block) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/workforce/configuration.rb', line 42 def after(event, &block) if block_given? @after_callbacks[event.to_sym] << block else @after_callbacks[event.to_sym] end end |
#before(event, &block) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/workforce/configuration.rb', line 34 def before(event, &block) if block_given? @before_callbacks[event.to_sym] << block else @before_callbacks[event.to_sym] end end |
#load_file(file_path) ⇒ Object
30 31 32 |
# File 'lib/workforce/configuration.rb', line 30 def load_file(file_path) instance_eval(File.read(file_path), file_path) end |
#schedule(worker, number = nil) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/workforce/configuration.rb', line 50 def schedule(worker, number = nil) if number @num_instances[worker.to_sym] = Integer(number) else @num_instances[worker.to_sym] end end |