Class: HtmlSlicer::CachedStuff

Inherits:
Object
  • Object
show all
Defined in:
lib/html_slicer/cached_stuff.rb

Overview

Object to be serialized and dumped as cache storage. Include resizing and slicing objects, and original text’s hexdigest hash value to provide authenticity. During the runtime object is used as an maps accessor too.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil) ⇒ CachedStuff

Returns a new instance of CachedStuff.



12
13
14
15
16
# File 'lib/html_slicer/cached_stuff.rb', line 12

def initialize(text = nil)
  @version = HtmlSlicer::VERSION
  @changed = false
  self.hexdigest_for = text if text
end

Instance Attribute Details

#changedObject

Returns the value of attribute changed.



10
11
12
# File 'lib/html_slicer/cached_stuff.rb', line 10

def changed
  @changed
end

#hexdigestObject (readonly)

Returns the value of attribute hexdigest.



9
10
11
# File 'lib/html_slicer/cached_stuff.rb', line 9

def hexdigest
  @hexdigest
end

#resizingObject

Returns the value of attribute resizing.



9
10
11
# File 'lib/html_slicer/cached_stuff.rb', line 9

def resizing
  @resizing
end

#slicingObject

Returns the value of attribute slicing.



9
10
11
# File 'lib/html_slicer/cached_stuff.rb', line 9

def slicing
  @slicing
end

#timeObject

Returns the value of attribute time.



10
11
12
# File 'lib/html_slicer/cached_stuff.rb', line 10

def time
  @time
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/html_slicer/cached_stuff.rb', line 9

def version
  @version
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/html_slicer/cached_stuff.rb', line 59

def changed?
  @changed
end

#hexdigest_for=(text) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/html_slicer/cached_stuff.rb', line 18

def hexdigest_for=(text)
  hex = Digest::SHA1.hexdigest(text)
  unless hex == @hexdigest
    @changed = true
    @hexdigest = hex
  end
  hex
end

#to_dumpObject

Serialize self, using Marshal and Base64 encoding



64
65
66
67
# File 'lib/html_slicer/cached_stuff.rb', line 64

def to_dump
  @time = Time.now
  Base64.encode64(Marshal.dump(self))
end

#valid_resizing_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/html_slicer/cached_stuff.rb', line 51

def valid_resizing_options?(options)
  options.try(:hexdigest) == @resizing.try(:options).try(:hexdigest)
end

#valid_slicing_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/html_slicer/cached_stuff.rb', line 55

def valid_slicing_options?(options)
  options.try(:hexdigest) == @slicing.try(:options).try(:hexdigest)
end

#valid_text?(text) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/html_slicer/cached_stuff.rb', line 47

def valid_text?(text)
  Digest::SHA1.hexdigest(text) == @hexdigest
end