cleaned up #sub_labels feature in Label object

This commit is contained in:
2011-03-09 11:56:42 +00:00
parent 482253f517
commit e3f23433d9
2 changed files with 28 additions and 19 deletions

View File

@@ -35,6 +35,18 @@ module Redistat
@saved ||= false
end
def parent
@parent ||= self.class.new(parent_group)
end
def parent_group
groups[1]
end
def group
@raw.split(GROUP_SEPARATOR).last
end
def groups
return @groups if @groups
@groups = []
@@ -49,23 +61,17 @@ module Redistat
@groups.reverse!
end
def parent
@parent ||= self.class.new(parent_group)
end
def parent_group
groups[1]
end
def sub_group
@raw.split(GROUP_SEPARATOR).last
def sub_labels
db.smembers("#{LABEL_INDEX}#{parent_group}").map { |member|
self.class.new("#{parent_group}#{GROUP_SEPARATOR}#{member}")
}
end
def update_index
groups.each do |group|
label = self.class.new(group)
break if label.parent_group.nil?
db.sadd("#{LABEL_INDEX}#{label.parent_group}", label.sub_group) == "OK" ? true : false
db.sadd("#{LABEL_INDEX}#{label.parent_group}", label.group) == "OK" ? true : false
end
end

View File

@@ -31,17 +31,22 @@ describe Redistat::Label do
@label = Redistat::Label.new(@name)
end
it "should know it's parent label group" do
@label.parent_group.should == 'message/public'
Redistat::Label.new('hello').parent_group.should be_nil
end
it "should separate label names into groups" do
@label.name.should == @name
@label.groups.should == [ "message/public/offensive",
"message/public",
"message" ]
"message/public",
"message" ]
@name = "/message/public/"
@label = Redistat::Label.new(@name)
@label.name.should == @name
@label.groups.should == [ "message/public",
"message" ]
"message" ]
@name = "message"
@label = Redistat::Label.new(@name)
@@ -49,17 +54,13 @@ describe Redistat::Label do
@label.groups.should == [ "message" ]
end
it "should know it's parent label group" do
@label.parent_group.should == 'message/public'
Redistat::Label.new('hello').parent_group.should be_nil
end
it "should update label index" do
db.smembers("#{Redistat::LABEL_INDEX}#{@label.parent_group}").should == []
@label.update_index
members = db.smembers("#{Redistat::LABEL_INDEX}#{@label.parent_group}") # checking 'message/public'
members.should have(1).item
members.should include('offensive')
members.should == @label.sub_labels.map { |l| l.group }
name = "message/public/nice"
label = Redistat::Label.new(name)
@@ -68,11 +69,13 @@ describe Redistat::Label do
members.should have(2).items
members.should include('offensive')
members.should include('nice')
members.should == label.sub_labels.map { |l| l.group }
label = @label.parent
members = db.smembers("#{Redistat::LABEL_INDEX}#{label.parent_group}") # checking 'message'
members.should have(1).item
members.should include('public')
members.should == label.sub_labels.map { |l| l.group }
end
end