Class: Readthis::Entity
- Inherits:
-
Object
- Object
- Readthis::Entity
- Defined in:
- lib/readthis/entity.rb
Constant Summary collapse
- DEFAULT_THRESHOLD =
8 * 1024
Instance Attribute Summary collapse
-
#compression ⇒ Object
readonly
Returns the value of attribute compression.
-
#marshal ⇒ Object
readonly
Returns the value of attribute marshal.
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
- #compress(value) ⇒ Object
- #decompress(value) ⇒ Object
- #dump(value) ⇒ Object
-
#initialize(marshal: Marshal, compress: false, threshold: DEFAULT_THRESHOLD) ⇒ Entity
constructor
A new instance of Entity.
- #load(value) ⇒ Object
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
#compression ⇒ Object (readonly)
Returns the value of attribute compression.
7 8 9 |
# File 'lib/readthis/entity.rb', line 7 def compression @compression end |
#marshal ⇒ Object (readonly)
Returns the value of attribute marshal.
7 8 9 |
# File 'lib/readthis/entity.rb', line 7 def marshal @marshal end |
#threshold ⇒ Object (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 |