class Smith::AgentCache

Attributes

path[RW]

Public Class Methods

new(opts={}) click to toggle source
# File lib/smith/agent_cache.rb, line 18
def initialize(opts={})
  @db = GDBM.new(Smith.cache_directory.join('agent_state.gdbm').to_s, 0600, GDBM::WRCREAT | GDBM::SYNC)
end

Public Instance Methods

[](uuid)
Alias for: entry
alive?(uuid) click to toggle source
# File lib/smith/agent_cache.rb, line 26
def alive?(uuid)
  (@db.include?(uuid)) ? instantiate(@db[uuid]).alive? : false
end
create(name) click to toggle source
# File lib/smith/agent_cache.rb, line 22
def create(name)
  AgentProcess.new(@db, :name => name, :uuid => SecureRandom.uuid)
end
delete(uuid) click to toggle source
# File lib/smith/agent_cache.rb, line 51
def delete(uuid)
  @db.delete(uuid)
end
each(&blk) click to toggle source
# File lib/smith/agent_cache.rb, line 61
def each(&blk)
  @db.each {|k,v| blk.call(instantiate(v)) }
end
entry(uuid) click to toggle source
# File lib/smith/agent_cache.rb, line 55
def entry(uuid)
  (uuid) ? instantiate(@db[uuid]) : nil
end
Also aliased as: []
exist?(uuid) click to toggle source
# File lib/smith/agent_cache.rb, line 30
def exist?(uuid)
  @db.include?(uuid)
end
find_by_name(*names) click to toggle source
# File lib/smith/agent_cache.rb, line 34
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) click to toggle source

select {|a| a.name == name.to_s } end

# File lib/smith/agent_cache.rb, line 47
def state(state)
  select {|a| a.state == state.to_s }
end

Private Instance Methods

instantiate(state) click to toggle source
# File lib/smith/agent_cache.rb, line 67
def instantiate(state)
  (state) ? AgentProcess.new(@db, state) : nil
end