Class: Nodectl::Manager
- Inherits:
-
Object
- Object
- Nodectl::Manager
- Defined in:
- lib/nodectl/manager.rb
Constant Summary collapse
- MANAGER_METHODS =
[:all, :find, :find!, :push, :delete, :[], :[]=, :next_id, :onadd, :ondelete]
Instance Method Summary collapse
- #all ⇒ Object
- #delete(key, options = {}) ⇒ Object
- #find(key) ⇒ Object (also: #[])
- #find!(key) ⇒ Object
-
#initialize(target_class) ⇒ Manager
constructor
A new instance of Manager.
- #next_id ⇒ Object
- #onadd(&blk) ⇒ Object
- #onadd_call(object) ⇒ Object
- #ondelete(&blk) ⇒ Object
- #ondelete_call(object, options) ⇒ Object
- #push(key, object) ⇒ Object (also: #[]=)
Constructor Details
#initialize(target_class) ⇒ Manager
Returns a new instance of Manager.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/nodectl/manager.rb', line 4 def initialize(target_class) @objects = {} @target_class = target_class @onadd = [] @ondelete = [] MANAGER_METHODS.each do |method| @target_class.define_singleton_method method do |*args, &blk| @manager.send(method, *args, &blk) end end end |
Instance Method Details
#all ⇒ Object
35 36 37 |
# File 'lib/nodectl/manager.rb', line 35 def all @objects.values end |
#delete(key, options = {}) ⇒ Object
46 47 48 49 |
# File 'lib/nodectl/manager.rb', line 46 def delete(key, = {}) object = @objects.delete(key) ondelete_call(object, ) end |
#find(key) ⇒ Object Also known as: []
19 20 21 |
# File 'lib/nodectl/manager.rb', line 19 def find(key) @objects[key] end |
#find!(key) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/nodectl/manager.rb', line 25 def find!(key) object = find(key) unless object raise Nodectl::NotFound, "#{@target_class.name} with key '#{key}' not found" end object end |
#next_id ⇒ Object
51 52 53 54 |
# File 'lib/nodectl/manager.rb', line 51 def next_id @next_id ||= 0 @next_id += 1 end |
#onadd(&blk) ⇒ Object
56 57 58 |
# File 'lib/nodectl/manager.rb', line 56 def onadd(&blk) @onadd << blk end |
#onadd_call(object) ⇒ Object
64 65 66 |
# File 'lib/nodectl/manager.rb', line 64 def onadd_call(object) @onadd.each { |b| b.call(object) } end |
#ondelete(&blk) ⇒ Object
60 61 62 |
# File 'lib/nodectl/manager.rb', line 60 def ondelete(&blk) @ondelete << blk end |
#ondelete_call(object, options) ⇒ Object
68 69 70 |
# File 'lib/nodectl/manager.rb', line 68 def ondelete_call(object, ) @ondelete.each { |b| b.call(object, ) } end |
#push(key, object) ⇒ Object Also known as: []=
39 40 41 42 |
# File 'lib/nodectl/manager.rb', line 39 def push(key, object) @objects[key] = object onadd_call(object) end |