Class: Nanite::LocalState

Inherits:
Hash
  • Object
show all
Defined in:
lib/nanite/local_state.rb

Instance Method Summary collapse

Constructor Details

#initialize(hsh = {}) ⇒ LocalState

Returns a new instance of LocalState.



3
4
5
6
7
# File 'lib/nanite/local_state.rb', line 3

def initialize(hsh={})
  hsh.each do |k,v|
    self[k] = v
  end
end

Instance Method Details

#nanites_for(request) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nanite/local_state.rb', line 9

def nanites_for(request)
  tags = request.tags
  service = request.type
  if service
    nanites = reject { |_, state| !state[:services].include?(service) }
  else
    nanites = self
  end

  if tags.nil? || tags.empty?
    service ? nanites : {}
  else
    nanites.reject { |_, state| (state[:tags] & tags).empty? }
  end
end

#update_status(name, status) ⇒ Object



25
26
27
# File 'lib/nanite/local_state.rb', line 25

def update_status(name, status)
  self[name].update(:status => status, :timestamp => Time.now.utc.to_i)
end

#update_tags(name, new_tags, obsolete_tags) ⇒ Object



29
30
31
32
33
# File 'lib/nanite/local_state.rb', line 29

def update_tags(name, new_tags, obsolete_tags)
  prev_tags = self[name] && self[name][:tags]
  updated_tags = (new_tags || []) + (prev_tags || []) - (obsolete_tags || [])
  self[name].update(:tags => updated_tags.uniq)
end