Class: CoderDecorator::Coders::Coder
- Inherits:
-
Object
- Object
- CoderDecorator::Coders::Coder
- 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, = {})
super(guest_coder)
@options =
end
end
Instance Attribute Summary collapse
-
#coder ⇒ Object
readonly
Returns the value of attribute coder.
Instance Method Summary collapse
-
#decode(_obj) ⇒ Object
It decodes
_obj
, returning decoded data, ornil
if it can’t decode. - #encode(_obj) ⇒ Object
-
#initialize(coder = nil) ⇒ Coder
constructor
Can optionally pass a base coder which is going to be decorated.
Constructor Details
Instance Attribute Details
#coder ⇒ Object (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.
41 42 43 |
# File 'lib/coder_decorator/coders/coder.rb', line 41 def decode(_obj) raise NotImplementedError end |
#encode(_obj) ⇒ Object
35 36 37 |
# File 'lib/coder_decorator/coders/coder.rb', line 35 def encode(_obj) raise NotImplementedError end |