Class: Readthis::Entity
- Inherits:
-
Object
- Object
- Readthis::Entity
- Defined in:
- lib/readthis/entity.rb
Overview
An instance of the Entity class is used to handle ‘load` and `dump` operations on cached values.
Constant Summary collapse
- DEFAULT_OPTIONS =
Unless they are overridden, these are the options used to load and unload every value.
{ compress: false, marshal: Marshal, threshold: 8 * 1024 }.freeze
- COMPRESSED_FLAG =
A hexidecimal compression flag. When it is present within the magic bit of an entity that entity is considered compressed.
0x8
Instance Method Summary collapse
-
#compose(value, marshal, compress) ⇒ String
Composes a single byte comprised of the chosen serializer and compression options.
-
#decompose(string) ⇒ Array<Module, Boolean, String>
Decompose an option embedded string into marshal, compression and value.
-
#dump(value, options = {}) ⇒ String
Output a value prepared for cache storage.
-
#initialize(options = {}) ⇒ Entity
constructor
Creates a Readthis::Entity with default options.
-
#load(string) ⇒ String
Parse a dumped value using the embedded options.
Constructor Details
#initialize(options = {}) ⇒ Entity
Creates a Readthis::Entity with default options. Each option can be overridden later when entities are being dumped.
Options are sticky, meaning that whatever is used when dumping will automatically be used again when loading, regardless of how current options are set.
35 36 37 |
# File 'lib/readthis/entity.rb', line 35 def initialize( = {}) @options = DEFAULT_OPTIONS.merge() end |
Instance Method Details
#compose(value, marshal, compress) ⇒ String
Composes a single byte comprised of the chosen serializer and compression options. The byte is formatted as:
| 0000 | 0 | 000 |
Where there are four unused bits, 1 compression bit, and 3 bits for the serializer. This allows up to 8 different serializers for marshaling.
104 105 106 107 108 109 |
# File 'lib/readthis/entity.rb', line 104 def compose(value, marshal, compress) flags = serializers.assoc(marshal) flags |= COMPRESSED_FLAG if compress value.prepend([flags].pack('C')) end |
#decompose(string) ⇒ Array<Module, Boolean, String>
Decompose an option embedded string into marshal, compression and value.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/readthis/entity.rb', line 117 def decompose(string) flags = string[0].unpack('C').first if flags < 16 marshal = serializers.rassoc(flags) compress = (flags & COMPRESSED_FLAG) != 0 [marshal, compress, string[1..-1]] else [@options[:marshal], @options[:compress], string] end end |
#dump(value, options = {}) ⇒ String
Output a value prepared for cache storage. Passed options will override whatever has been specified for the instance.
59 60 61 62 63 64 65 66 67 |
# File 'lib/readthis/entity.rb', line 59 def dump(value, = {}) compress = with_fallback(, :compress) marshal = with_fallback(, :marshal) threshold = with_fallback(, :threshold) dumped = deflate(marshal.dump(value), compress, threshold) compose(dumped, marshal, compress) end |
#load(string) ⇒ String
Parse a dumped value using the embedded options.
78 79 80 81 82 83 84 |
# File 'lib/readthis/entity.rb', line 78 def load(string) marshal, compress, value = decompose(string) marshal.load(inflate(value, compress)) rescue TypeError, NoMethodError string end |