Module: Flags

Defined in:
lib/city_watch/util/flags.rb

Instance Method Summary collapse

Instance Method Details

#clear_flag(name) ⇒ Object



10
11
12
13
14
15
# File 'lib/city_watch/util/flags.rb', line 10

def clear_flag(name)
  if get_flag(name)
    flag_flapped name, :off
    CityWatch.redis.setbit "#{CityWatch.config[:prefix]}::#{host}::#{self.name}::flags", flag_position(name), 0
  end
end

#flag_flapped(name, new_val) ⇒ Object



17
18
19
# File 'lib/city_watch/util/flags.rb', line 17

def flag_flapped(name,new_val)
  puts "Flag flipped: #{name} -> #{new_val}" if CityWatch.debug?
end

#flag_mapObject



40
41
42
# File 'lib/city_watch/util/flags.rb', line 40

def flag_map
  CityWatch.redis.lrange flag_map_key, 0, -1
end

#flag_map_keyObject



36
37
38
# File 'lib/city_watch/util/flags.rb', line 36

def flag_map_key
  "#{CityWatch.config[:prefix]}::#{self.name}::flag_map"
end

#flag_position(name) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/city_watch/util/flags.rb', line 44

def flag_position(name)
  if (map = flag_map) && map.include?(name.to_s)
    map.index(name.to_s)
  else
    new_flag(name)
  end
end

#get_flag(name, host = host) ⇒ Object



21
22
23
24
# File 'lib/city_watch/util/flags.rb', line 21

def get_flag(name,host=host)
  @host = host
  CityWatch.redis.getbit("#{CityWatch.config[:prefix]}::#{host}::#{self.name}::flags", flag_position(name)) == 1 ? true : false
end

#get_flags(host = host) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/city_watch/util/flags.rb', line 26

def get_flags(host=host)
  @host = host
  out = []
  map = flag_map
  map.each_index do |idx|
    out << map[idx] if get_flag(map[idx])
  end
  out
end

#new_flag(name) ⇒ Object



52
53
54
# File 'lib/city_watch/util/flags.rb', line 52

def new_flag(name)
  CityWatch.redis.rpush(flag_map_key, name) - 1
end

#set_flag(name) ⇒ Object



3
4
5
6
7
8
# File 'lib/city_watch/util/flags.rb', line 3

def set_flag(name)
  unless get_flag(name)
    flag_flapped name, :on
    CityWatch.redis.setbit "#{CityWatch.config[:prefix]}::#{host}::#{self.name}::flags", flag_position(name), 1
  end
end