lib/flapjack/data/entity.rb in flapjack-0.6.47 vs lib/flapjack/data/entity.rb in flapjack-0.6.48

- old
+ new

@@ -7,12 +7,14 @@ module Data class Entity - attr_accessor :name, :id, :tags + attr_accessor :name, :id + TAG_PREFIX = 'entity_tag' + def self.all(options = {}) raise "Redis connection not set" unless redis = options[:redis] redis.keys("entity_id:*").collect {|k| k =~ /^entity_id:(.+)$/; entity_name = $1 self.new(:name => entity_name, :id => redis.get("entity_id:#{entity_name}"), :redis => redis) @@ -105,22 +107,26 @@ checks = check_list return if checks.nil? checks.length end + def tags + @tags ||= load_tags + end + def add_tags(*enum) enum.each do |t| - Flapjack::Data::Tag.create("#{@tag_prefix}:#{t}", [@id], :redis => @redis) - @tags.add(t) + Flapjack::Data::Tag.create("#{TAG_PREFIX}:#{t}", [@id], :redis => @redis) + tags.add(t) end end def delete_tags(*enum) enum.each do |t| - tag = Flapjack::Data::Tag.find("#{@tag_prefix}:#{t}", :redis => @redis) + tag = Flapjack::Data::Tag.find("#{TAG_PREFIX}:#{t}", :redis => @redis) tag.delete(@id) - @tags.delete(t) + tags.delete(t) end end private @@ -129,19 +135,19 @@ def initialize(options = {}) raise "Redis connection not set" unless @redis = options[:redis] raise "Entity name not set" unless @name = options[:name] @id = options[:id] @logger = options[:logger] + end - @tag_prefix = 'entity_tag' - @tags = ::Set.new - tag_data = @redis.keys("#{@tag_prefix}:*").inject([]) do |memo, entity_tag| + def load_tags + entity_tags = ::Set.new + tag_data = @redis.keys("#{TAG_PREFIX}:*").inject([]) do |memo, entity_tag| tag = Flapjack::Data::Tag.find(entity_tag, :redis => @redis) - memo << entity_tag.sub(%r(^#{@tag_prefix}:), '') if tag.include?(@id.to_s) + memo << entity_tag.sub(/^#{TAG_PREFIX}:/, '') if tag.include?(@id.to_s) memo end - @tags.merge(tag_data) - + entity_tags.merge(tag_data) end end end