Class: Moneta::Transforms::Prefix
- Inherits:
-
Moneta::Transform
- Object
- Moneta::Transform
- Moneta::Transforms::Prefix
- Defined in:
- lib/moneta/transforms/prefix.rb
Overview
Prefixes strings
Instance Method Summary collapse
-
#decode(value) ⇒ String
Removes the prefix.
-
#encode(value) ⇒ String
Prepends the prefix.
-
#encoded?(value) ⇒ Boolean
Checks that the string starts with the prefix.
-
#initialize(prefix:, **options) ⇒ Prefix
constructor
A new instance of Prefix.
Methods inherited from Moneta::Transform
#decodable?, delegate_to, #method_missing, #respond_to_missing?
Constructor Details
#initialize(prefix:, **options) ⇒ Prefix
Returns a new instance of Prefix.
6 7 8 9 10 11 |
# File 'lib/moneta/transforms/prefix.rb', line 6 def initialize(prefix:, **) super raise "prefix must be a string" unless prefix.is_a? String @prefix = prefix end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Moneta::Transform
Instance Method Details
#decode(value) ⇒ String
Removes the prefix
33 34 35 36 |
# File 'lib/moneta/transforms/prefix.rb', line 33 def decode(value) raise "value is not prefixed with #{@prefix}" unless encoded? value value[@prefix.length..-1] end |
#encode(value) ⇒ String
Prepends the prefix
25 26 27 |
# File 'lib/moneta/transforms/prefix.rb', line 25 def encode(value) @prefix + value end |
#encoded?(value) ⇒ Boolean
Checks that the string starts with the prefix
17 18 19 |
# File 'lib/moneta/transforms/prefix.rb', line 17 def encoded?(value) value.start_with?(@prefix) end |