Class: CoderDecorator::Coders::Coder

Inherits:
Object
  • Object
show all
Defined in:
lib/coder_decorator/coders/coder.rb

Overview

The abstract class of coder, must implement #encode and #decode. It’s designed with decorator pattern, which makes it more flexible, and can be wrapped infinitely using Ruby instantiation.

To implement a custom coder decorator, inherit from Coders::Coder, and use coder.encode, coder.decode to get results from base coder:

class Reverse < Coders::Coder
   def encode(str); coder.encode(str).reverse; end
   def decode(str); coder.decode(str.reverse); end
end
coder = Reverse.new(Coder::Base64.new)

If you want to customize options, be sure to call super:

class MyCoder < Coders::Coder
  def initialize(gueset_coder, options = {})
    super(guest_coder)
    @options = options
  end
end

Direct Known Subclasses

Base64, Cipher, HMAC, Identity, JSON, Marshal, Rescue, Zip

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coder = nil) ⇒ Coder

Can optionally pass a base coder which is going to be decorated.



31
32
33
# File 'lib/coder_decorator/coders/coder.rb', line 31

def initialize(coder = nil)
  @coder = coder || Null.new
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder.



28
29
30
# File 'lib/coder_decorator/coders/coder.rb', line 28

def coder
  @coder
end

Instance Method Details

#decode(_obj) ⇒ Object

It decodes _obj, returning decoded data, or nil if it can’t decode.

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/coder_decorator/coders/coder.rb', line 41

def decode(_obj)
  raise NotImplementedError
end

#encode(_obj) ⇒ Object

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/coder_decorator/coders/coder.rb', line 35

def encode(_obj)
  raise NotImplementedError
end