Class: Smith::AgentCache
- Inherits:
-
Object
- Object
- Smith::AgentCache
- Includes:
- Enumerable
- Defined in:
- lib/smith/agent_cache.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #alive?(uuid) ⇒ Boolean
- #create(name) ⇒ Object
- #delete(uuid) ⇒ Object
- #each(&blk) ⇒ Object
- #entry(uuid) ⇒ Object (also: #[])
- #exist?(uuid) ⇒ Boolean
- #find_by_name(*names) ⇒ Object
-
#initialize(opts = {}) ⇒ AgentCache
constructor
A new instance of AgentCache.
-
#state(state) ⇒ Object
end.
Constructor Details
#initialize(opts = {}) ⇒ AgentCache
Returns a new instance of AgentCache.
19 20 21 |
# File 'lib/smith/agent_cache.rb', line 19 def initialize(opts={}) @db = GDBM.new(Smith.cache_directory.join('agent_state.gdbm').to_s, 0600, GDBM::WRCREAT | GDBM::SYNC) end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
17 18 19 |
# File 'lib/smith/agent_cache.rb', line 17 def path @path end |
Instance Method Details
#alive?(uuid) ⇒ Boolean
27 28 29 |
# File 'lib/smith/agent_cache.rb', line 27 def alive?(uuid) (@db.include?(uuid)) ? instantiate(@db[uuid]).alive? : false end |
#create(name) ⇒ Object
23 24 25 |
# File 'lib/smith/agent_cache.rb', line 23 def create(name) AgentProcess.new(@db, :name => name, :uuid => SecureRandom.uuid) end |
#delete(uuid) ⇒ Object
52 53 54 |
# File 'lib/smith/agent_cache.rb', line 52 def delete(uuid) @db.delete(uuid) end |
#each(&blk) ⇒ Object
62 63 64 |
# File 'lib/smith/agent_cache.rb', line 62 def each(&blk) @db.each {|k,v| blk.call(instantiate(v)) } end |
#entry(uuid) ⇒ Object Also known as: []
56 57 58 |
# File 'lib/smith/agent_cache.rb', line 56 def entry(uuid) (uuid) ? instantiate(@db[uuid]) : nil end |
#exist?(uuid) ⇒ Boolean
31 32 33 |
# File 'lib/smith/agent_cache.rb', line 31 def exist?(uuid) @db.include?(uuid) end |
#find_by_name(*names) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/smith/agent_cache.rb', line 35 def find_by_name(*names) inject([]) do |a, agent| a.tap do |acc| names.flatten.uniq.each do |name| acc << agent if name == agent.name end end end end |
#state(state) ⇒ Object
end
48 49 50 |
# File 'lib/smith/agent_cache.rb', line 48 def state(state) select {|a| a.state == state.to_s } end |