Class: RedisTags::TagList

Inherits:
Array
  • Object
show all
Defined in:
lib/redis_tags/tag_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ TagList

Returns a new instance of TagList.



6
7
8
9
10
11
12
# File 'lib/redis_tags/tag_list.rb', line 6

def initialize(owner)
  @owner = owner
  @owner_class = owner.class.to_s.downcase
  @owner_id = owner.id
  super(engine.smembers(self.redis_key))
  self
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



4
5
6
# File 'lib/redis_tags/tag_list.rb', line 4

def owner
  @owner
end

#owner_classObject (readonly)

Returns the value of attribute owner_class.



4
5
6
# File 'lib/redis_tags/tag_list.rb', line 4

def owner_class
  @owner_class
end

#owner_idObject (readonly)

Returns the value of attribute owner_id.



4
5
6
# File 'lib/redis_tags/tag_list.rb', line 4

def owner_id
  @owner_id
end

#tagsObject (readonly)

Returns the value of attribute tags.



4
5
6
# File 'lib/redis_tags/tag_list.rb', line 4

def tags
  @tags
end

Instance Method Details

#<<(tag_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redis_tags/tag_list.rb', line 14

def <<(tag_name)
  tag_name = tag_name.downcase.strip
  if !(self.owner_id.nil?)
    engine.multi do
      engine.sadd self.redis_key, tag_name
      engine.sadd "#{self.owner_class}:tagged_with:#{tag_name.gsub(" ", '-')}", self.owner_id
    end
    engine.multi do
      RedisTags::Tag.register_tag_for_autocomplete(engine, tag_name)
    end
  end
  super(tag_name)
end

#append_multi(tags) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/redis_tags/tag_list.rb', line 54

def append_multi(tags)
  if tags.is_a?(String)
    tags = tags.split(",").collect {|tag| tag.strip.downcase}
  end
  tags.each do |tag|
    self << tag
  end
  self
end

#delete(tag_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/redis_tags/tag_list.rb', line 28

def delete(tag_name)
  tag_name = tag_name.downcase.strip
  if !(self.owner_id.nil?)
    engine.multi do
      engine.srem self.redis_key, tag_name
      engine.srem "#{self.owner_class}:tagged_with:#{tag_name.gsub(" ", '-')}", self.owner_id
    end
  end
  super(tag_name)
end

#delete_allObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redis_tags/tag_list.rb', line 39

def delete_all
  if !(self.owner_id.nil?)
    engine.multi do
      engine.del self.redis_key
      self.each do |tag_name|
        engine.srem "#{self.owner_class}:tagged_with:#{tag_name.gsub(" ", '-')}", self.owner_id
      end
    end
  end
  self.each do |tag_name|
    self.delete(tag_name)
  end
  self
end

#engineObject



70
71
72
# File 'lib/redis_tags/tag_list.rb', line 70

def engine
  self.owner.class.redis_tags_engine
end

#redis_keyObject



74
75
76
# File 'lib/redis_tags/tag_list.rb', line 74

def redis_key
  "#{self.owner_class}:#{self.owner_id}:tag_list"
end

#saveObject



64
65
66
67
68
# File 'lib/redis_tags/tag_list.rb', line 64

def save
  my_tags = self.dup
  delete_all
  self.append_multi(my_tags)
end