Class: Readthis::Entity
- Inherits:
-
Object
- Object
- Readthis::Entity
- Defined in:
- lib/readthis/entity.rb
Constant Summary collapse
- DEFAULT_THRESHOLD =
8 * 1024
- MAGIC_BYTES =
[120, 156].freeze
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(options = {}) ⇒ Entity
constructor
A new instance of Entity.
- #load(value) ⇒ Object
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( = {}) @marshal = .fetch(:marshal, Marshal) @compression = .fetch(:compress, false) @threshold = .fetch(:threshold, DEFAULT_THRESHOLD) end |
Instance Attribute Details
#compression ⇒ Object (readonly)
Returns the value of attribute compression.
8 9 10 |
# File 'lib/readthis/entity.rb', line 8 def compression @compression end |
#marshal ⇒ Object (readonly)
Returns the value of attribute marshal.
8 9 10 |
# File 'lib/readthis/entity.rb', line 8 def marshal @marshal end |
#threshold ⇒ Object (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 |