Class: NiceCache::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/nice_cache/tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Tag

Returns a new instance of Tag.



15
16
17
# File 'lib/nice_cache/tag.rb', line 15

def initialize(name)
  @name = name.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/nice_cache/tag.rb', line 5

def name
  @name
end

Class Method Details

.find(*tag_names) ⇒ Object



7
8
9
# File 'lib/nice_cache/tag.rb', line 7

def self.find(*tag_names)
  Array(tag_names).flatten.map{ |tag_symbol| new(tag_symbol) }
end

.key(name) ⇒ Object



11
12
13
# File 'lib/nice_cache/tag.rb', line 11

def self.key(name)
  "Tag:#{name}"
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
45
46
# File 'lib/nice_cache/tag.rb', line 42

def ==(other)
  name == other.name
rescue
  false
end

#attach_on(fragment) ⇒ Object



19
20
21
22
23
# File 'lib/nice_cache/tag.rb', line 19

def attach_on(fragment)
  redis.sadd(tag_key, fragment.name)
  fragment.attach_with(self)
  self
end

#fragmentsObject



25
26
27
# File 'lib/nice_cache/tag.rb', line 25

def fragments
  fragment_keys.map{ |fragment_key| Fragment.find(fragment_key) }
end

#remove_from(fragment) ⇒ Object



33
34
35
# File 'lib/nice_cache/tag.rb', line 33

def remove_from(fragment)
  redis.srem(tag_key, fragment.name)
end

#sweepObject



37
38
39
40
# File 'lib/nice_cache/tag.rb', line 37

def sweep
  fragments.map(&:destroy)
  destroy
end

#tag_keyObject



29
30
31
# File 'lib/nice_cache/tag.rb', line 29

def tag_key
  self.class.key(@name)
end