Sha256: fc9baacbd2335f985a4485410a444414dfe0c75d94641c9d93459c88b317e3db

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Flags
	module ClassMethods
		
		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
		
		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
		
		def flag_flapped(name,new_val)
			puts "Flag flipped: #{name} -> #{new_val}" if CityWatch.debug?
		end
		
		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
		
		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
		
		def flag_map_key
			"#{CityWatch.config[:prefix]}::#{self.name}::flag_map"
		end
		
		def flag_map
			CityWatch.redis.lrange flag_map_key, 0, -1
		end
		
		def flag_position(name)
			if (map = flag_map) && map.include?(name.to_s)
				map.index(name.to_s)
			else
				new_flag(name)
			end
		end
		
		def new_flag(name)
			CityWatch.redis.rpush(flag_map_key, name) - 1
		end
		
	end
	
	def self.included(base)
		base.extend(ClassMethods)
	end
	
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
city-watch-0.5.4 lib/city_watch/util/flags.rb