Class: Readthis::Entity

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

Constant Summary collapse

DEFAULT_THRESHOLD =
8 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(marshal: Marshal, compress: false, threshold: DEFAULT_THRESHOLD) ⇒ Entity

Returns a new instance of Entity.



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

def initialize(marshal: Marshal, compress: false, threshold: DEFAULT_THRESHOLD)
  @marshal     = marshal
  @compression = compress
  @threshold   = threshold
end

Instance Attribute Details

#compressionObject (readonly)

Returns the value of attribute compression.



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

def compression
  @compression
end

#marshalObject (readonly)

Returns the value of attribute marshal.



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

def marshal
  @marshal
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

Instance Method Details

#compress(value) ⇒ Object



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

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

#decompress(value) ⇒ Object



41
42
43
# File 'lib/readthis/entity.rb', line 41

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

#dump(value) ⇒ Object



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

def dump(value)
  return value if value.nil?

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

#load(value) ⇒ Object



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

def load(value)
  return value if value.nil?

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