Class: Readthis::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/readthis/entity.rb

Constant Summary collapse

DEFAULT_THRESHOLD =
8 * 1024
MAGIC_BYTES =
[120, 156].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Entity

Returns a new instance of Entity.



10
11
12
13
14
# File 'lib/readthis/entity.rb', line 10

def initialize(options = {})
  @marshal     = options.fetch(:marshal, Marshal)
  @compression = options.fetch(:compress, false)
  @threshold   = options.fetch(:threshold, DEFAULT_THRESHOLD)
end

Instance Attribute Details

#compressionObject (readonly)

Returns the value of attribute compression.



8
9
10
# File 'lib/readthis/entity.rb', line 8

def compression
  @compression
end

#marshalObject (readonly)

Returns the value of attribute marshal.



8
9
10
# File 'lib/readthis/entity.rb', line 8

def marshal
  @marshal
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



8
9
10
# File 'lib/readthis/entity.rb', line 8

def threshold
  @threshold
end

Instance Method Details

#compress(value) ⇒ Object



34
35
36
# File 'lib/readthis/entity.rb', line 34

def compress(value)
  Zlib::Deflate.deflate(marshal.dump(value))
end

#decompress(value) ⇒ Object



38
39
40
# File 'lib/readthis/entity.rb', line 38

def decompress(value)
  marshal.load(Zlib::Inflate.inflate(value))
end

#dump(value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/readthis/entity.rb', line 16

def dump(value)
  if compress?(value)
    compress(value)
  else
    marshal.dump(value)
  end
end

#load(value) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/readthis/entity.rb', line 24

def load(value)
  if compressed?(value)
    decompress(value)
  else
    marshal.load(value)
  end
rescue TypeError, Zlib::Error
  value
end