Class: RedisTags::TagList
- Inherits:
-
Array
- Object
- Array
- RedisTags::TagList
- Defined in:
- lib/redis_tags/tag_list.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#owner_class ⇒ Object
readonly
Returns the value of attribute owner_class.
-
#owner_id ⇒ Object
readonly
Returns the value of attribute owner_id.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #<<(tag_name) ⇒ Object
- #append_multi(tags) ⇒ Object
- #delete(tag_name) ⇒ Object
- #delete_all ⇒ Object
- #engine ⇒ Object
-
#initialize(owner) ⇒ TagList
constructor
A new instance of TagList.
- #redis_key ⇒ Object
- #save ⇒ Object
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
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
4 5 6 |
# File 'lib/redis_tags/tag_list.rb', line 4 def owner @owner end |
#owner_class ⇒ Object (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_id ⇒ Object (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 |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
4 5 6 |
# File 'lib/redis_tags/tag_list.rb', line 4 def @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() if .is_a?(String) = .split(",").collect {|tag| tag.strip.downcase} end .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_all ⇒ Object
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 |
#engine ⇒ Object
70 71 72 |
# File 'lib/redis_tags/tag_list.rb', line 70 def engine self.owner.class. end |
#redis_key ⇒ Object
74 75 76 |
# File 'lib/redis_tags/tag_list.rb', line 74 def redis_key "#{self.owner_class}:#{self.owner_id}:tag_list" end |
#save ⇒ Object
64 65 66 67 68 |
# File 'lib/redis_tags/tag_list.rb', line 64 def save = self.dup delete_all self.append_multi() end |