Class: Spritely::Cache

Inherits:
Struct
  • Object
show all
Defined in:
lib/spritely/cache.rb

Constant Summary collapse

PNG_SIGNATURE_LENGTH =
8
PNG_INFO_LENGTH =
8
PNG_CRC_LENGTH =

Cyclic Redundancy Check (CRC) byte-length; www.w3.org/TR/PNG/#5Chunk-layout

4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



4
5
6
# File 'lib/spritely/cache.rb', line 4

def filename
  @filename
end

Class Method Details

.busted?(filename, expected_cache_key) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/spritely/cache.rb', line 13

def self.busted?(filename, expected_cache_key)
  new(filename).key != expected_cache_key
end

.generate(*objects) ⇒ Object



9
10
11
# File 'lib/spritely/cache.rb', line 9

def self.generate(*objects)
  Digest::MD5.hexdigest(objects.collect(&:cache_key).join)
end

Instance Method Details

#keyObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spritely/cache.rb', line 17

def key
  return @key if @key

  File.open(filename) do |file|
    file.read(PNG_SIGNATURE_LENGTH) # we first have to read the signature to fast-forward the IO#pos
    until file.eof?
      each_chunk(file) do |keyword, value|
        if keyword == 'cache_key'
          return @key = value
          break
        end
      end
    end
  end
end