Class: Moneta::Transform Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/moneta/transform.rb,
lib/moneta/transform/serializer.rb

Overview

This class is abstract.

Subclasses should implement #encode and #decode or use Serializer.delegate_to to delegate to another object. They may also implement #encoded? if it is possible to efficiently test whether something was encoded (e.g. using a magic number).

Handles encoding/decoding of arbitrary objects into something that can be stored in a backend. Most transforms encode to string.

Defined Under Namespace

Classes: Serializer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**_) ⇒ Transform

Transforms can be initialized with arbitrary keyword args. The default initializer does nothing, just swallows the arguments it receives.



70
# File 'lib/moneta/transform.rb', line 70

def initialize(**_) end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/moneta/transform.rb', line 88

def method_missing(method, *args)
  case method
  when :encode
    raise NotImplementedError, "Encoder not defined"
  when :decode
    raise NotImplementedError, "Not decodable"
  when :encoded?
    nil
  else
    super
  end
end

Class Method Details

.delegate_to(object, methods = nil) ⇒ Object

This helper can be used in subclasses to implement #encode and #decode by delegating to some other object. If the object delegated to responds to encode (and optionally decode) or dump (and optionally load), these will be detected automatically. Otherwise, a second argument can be supplied giving the names of the methods to use as a pair of symbols (encode then optionally decode).

Examples:

Delegate to stdlib JSON library

require 'json'

class MyJsonTransform < Moneta::Transform
  delegate_to ::JSON
  # equvalent to
  delegate_to ::JSON, %[dump load]
end

Delegate to CGI’s special escaping methods

require 'json'

class MyEscapeTransform < Moneta::Transform
  delegate_to ::CGI, %[escapeURIComponent unescapeURIComponent]
end

Parameters:

  • object (Module)

    The object to delegate to

  • methods (<Symbol,Symbol>) (defaults to: nil)

    The methods on object to delegate to



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/moneta/transform.rb', line 47

def self.delegate_to(object, methods = nil)
  extend Forwardable

  encode, decode =
    if methods && methods.length >= 1
      methods
    elsif object.respond_to?(:encode)
      %i[encode decode]
    elsif object.respond_to?(:dump)
      %i[dump load]
    else
      raise "Could not determine what methods to use on #{object}"
    end

  def_delegator object, encode, :encode

  if decode && object.respond_to?(decode)
    def_delegator object, decode, :decode
  end
end

Instance Method Details

#decodable?Boolean

Returns true if the transform has a #decode method. Some transforms (e.g. MD5) are one-way.

Returns:

  • (Boolean)


84
85
86
# File 'lib/moneta/transform.rb', line 84

def decodable?
  respond_to? :decode
end

#decode(value) ⇒ Object

This method is abstract.

Subclasses where it is possible to decode again should implement this method

Parameters:

  • value (Object)

    the thing to decode

Returns:

  • (Object)


# File 'lib/moneta/transform.rb', line 72

#encode(value) ⇒ Object

This method is abstract.

All Subclasses should implement this method

Parameters:

  • value (Object)

    the thing to encode

Returns:

  • (Object)


# File 'lib/moneta/transform.rb', line 72

#respond_to_missing?(method, _) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/moneta/transform.rb', line 101

def respond_to_missing?(method, _)
  method == :encoded?
end