Class: NiceCache::Fragment

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, tag_names = nil) ⇒ Fragment

Returns a new instance of Fragment.



19
20
21
# File 'lib/nice_cache/fragment.rb', line 19

def initialize(data = nil, tag_names = nil)
  create(data, tag_names) if data && tag_names
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.find(name) ⇒ Object



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

def self.find(name)
  new.find(name)
end

.key(name) ⇒ Object



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

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

.tags_for(name) ⇒ Object



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

def self.tags_for(name)
  "TagsFor:#{name}"
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
57
58
# File 'lib/nice_cache/fragment.rb', line 54

def ==(other)
  [name, data] == [other.name, other.data]
rescue
  false
end

#attach_with(tag) ⇒ Object



40
41
42
# File 'lib/nice_cache/fragment.rb', line 40

def attach_with(tag)
  redis.sadd(attached_tags, tag.name)
end

#create(data, tag_names) ⇒ Object



23
24
25
26
27
28
# File 'lib/nice_cache/fragment.rb', line 23

def create(data, tag_names)
  @name = stringify(tag_names)
  @data = data
  attach_tags(tag_names)
  save_to_redis
end

#destroyObject



44
45
46
47
48
49
50
51
52
# File 'lib/nice_cache/fragment.rb', line 44

def destroy
  tags.each do |tag|
    tag.remove_from(self)
  end

  redis.srem(NiceCache.all_fragments_key, @name)
  redis.del attached_tags
  redis.del fragment_key
end

#find(name) ⇒ Object



30
31
32
33
34
# File 'lib/nice_cache/fragment.rb', line 30

def find(name)
  @name = name
  @data = data_from_redis
  self
end

#tagsObject



36
37
38
# File 'lib/nice_cache/fragment.rb', line 36

def tags
  Tag.find(redis_tag_names)
end