Class: Smith::AgentCache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/smith/agent_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AgentCache

Returns a new instance of AgentCache.



12
13
14
# File 'lib/smith/agent_cache.rb', line 12

def initialize(opts={})
  @db = GDBM.new(Smith.cache_path.join('agent_state.gdbm').to_s, 0600, GDBM::WRCREAT | GDBM::SYNC)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/smith/agent_cache.rb', line 10

def path
  @path
end

Instance Method Details

#alive?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/smith/agent_cache.rb', line 20

def alive?(uuid)
  (@db.include?(uuid)) ? instantiate(@db[uuid]).alive? : false
end

#create(name) ⇒ Object



16
17
18
# File 'lib/smith/agent_cache.rb', line 16

def create(name)
  AgentProcess.new(@db, :name => name, :uuid => SecureRandom.uuid)
end

#delete(uuid) ⇒ Object



45
46
47
# File 'lib/smith/agent_cache.rb', line 45

def delete(uuid)
  @db.delete(uuid)
end

#each(&blk) ⇒ Object



55
56
57
# File 'lib/smith/agent_cache.rb', line 55

def each(&blk)
  @db.each {|k,v| blk.call(instantiate(v)) }
end

#entry(uuid) ⇒ Object Also known as: []



49
50
51
# File 'lib/smith/agent_cache.rb', line 49

def entry(uuid)
  (uuid) ? instantiate(@db[uuid]) : nil
end

#exist?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/smith/agent_cache.rb', line 24

def exist?(uuid)
  @db.include?(uuid)
end

#find_by_name(*names) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/smith/agent_cache.rb', line 28

def find_by_name(*names)
  inject([]) do |a, agent|
    a.tap do |acc|
      names.flatten.each do |name|
        acc << agent if name == agent.name
      end
    end
  end
end

#state(state) ⇒ Object

end



41
42
43
# File 'lib/smith/agent_cache.rb', line 41

def state(state)
  select {|a| a.state == state.to_s }
end