Class: Moneta::Transforms::Prefix

Inherits:
Moneta::Transform show all
Defined in:
lib/moneta/transforms/prefix.rb

Overview

Prefixes strings

Instance Method Summary collapse

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.

Parameters:

  • prefix (String)

    The prefix to add



6
7
8
9
10
11
# File 'lib/moneta/transforms/prefix.rb', line 6

def initialize(prefix:, **options)
  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

Parameters:

  • value (String)

    The prefixed string

Returns:

  • (String)

    The value with the prefix removed



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

Parameters:

  • value (String)

    The string to prefix

Returns:

  • (String)

    The value with the prefix prepended



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

Parameters:

  • value (String)

Returns:

  • (Boolean)


17
18
19
# File 'lib/moneta/transforms/prefix.rb', line 17

def encoded?(value)
  value.start_with?(@prefix)
end