Class: Moneta::Transforms::Truncate
- Inherits:
-
Moneta::Transform
- Object
- Moneta::Transform
- Moneta::Transforms::Truncate
- Defined in:
- lib/moneta/transforms/truncate.rb
Overview
Transforms strings by truncating them to a certain fixed length (32 bytes or greater). Strings that are longer than the specified length will be MD5 hashed, and their last 32 bytes will be replaced by the hash.
Instance Method Summary collapse
- #encode(value) ⇒ String
-
#initialize(maxlen: 32, **_) ⇒ Truncate
constructor
A new instance of Truncate.
Methods inherited from Moneta::Transform
#decodable?, #decode, delegate_to, #method_missing, #respond_to_missing?
Constructor Details
#initialize(maxlen: 32, **_) ⇒ Truncate
Returns a new instance of Truncate.
20 21 22 23 24 |
# File 'lib/moneta/transforms/truncate.rb', line 20 def initialize(maxlen: 32, **_) super raise ":maxlen must be at least 32" if maxlen < 32 @maxlen = maxlen end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Moneta::Transform
Instance Method Details
#encode(value) ⇒ String
28 29 30 31 32 33 34 35 |
# File 'lib/moneta/transforms/truncate.rb', line 28 def encode(value) if value.size >= @maxlen digest = ::Digest::MD5.hexdigest(value) value[0, @maxlen - digest.size] << digest else value end end |