Skip to content

Commit 44ef255

Browse files
committed
Transforms: add docs
1 parent a1c9fde commit 44ef255

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+317
-49
lines changed

lib/moneta/transform.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class Transform
1111
autoload :Serializer, "moneta/transform/serializer"
1212

1313
# This helper can be used in subclasses to implement {#encode} and {#decode} by delegating to some other object. If
14-
# the object delegated to responds to encode (and optionally decode) or dump (and optionally load), these will be
15-
# detected automatically. Otherwise, a second argument can be supplied giving the names of the methods to use as a
16-
# pair of symbols (encode then optionally decode).
14+
# the object delegated to responds to +encode+ (and optionally +decode+) or +dump+ (and optionally +load+), these
15+
# will be detected automatically. Otherwise, a second argument can be supplied giving the names of the methods to
16+
# use as a pair of symbols (+encode+ then optionally +decode+).
1717
#
1818
# @example Delegate to stdlib JSON library
1919
# require 'json'
@@ -33,6 +33,17 @@ class Transform
3333
#
3434
# @param object [Module] The object to delegate to
3535
# @param methods [<Symbol,Symbol>] The methods on +object+ to delegate to
36+
#
37+
# @!macro [attach] transform_delegate_to
38+
# @!method encode(value)
39+
# Delegates to $1
40+
# @param value [Object]
41+
# @return [Object]
42+
#
43+
# @!method decode(value)
44+
# Delegates to $1
45+
# @param value [Object]
46+
# @return [Object]
3647
def self.delegate_to(object, methods = nil)
3748
extend Forwardable
3849

@@ -56,10 +67,19 @@ def self.delegate_to(object, methods = nil)
5667

5768
# Transforms can be initialized with arbitrary keyword args. The default
5869
# initializer does nothing, just swallows the arguments it receives.
59-
def initialize(**_)
60-
end
70+
def initialize(**_) end
71+
72+
# @!method encode(value)
73+
# @abstract All Subclasses should implement this method
74+
# @param value [Object] the thing to encode
75+
# @return [Object]
76+
#
77+
# @!method decode(value)
78+
# @abstract Subclasses where it is possible to decode again should implement this method
79+
# @param value [Object] the thing to decode
80+
# @return [Object]
6181

62-
# Returns true if the Tranform has a {#decode} method. Some transforms
82+
# Returns true if the transform has a {#decode} method. Some transforms
6383
# (e.g. MD5) are one-way.
6484
def decodable?
6585
respond_to? :decode

lib/moneta/transform/serializer.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ class Serializer < Transform
3939
#
4040
# @param object [Module] The object to delegate to
4141
# @param methods [<Symbol,Symbol>] The methods on +object+ to delegate to
42+
#
43+
# @!macro [attach] transform_serializer_delegate_to
44+
# @!method serialize(value)
45+
# Delegates to $1
46+
# @param value [Object]
47+
# @return [Object]
48+
#
49+
# @!method deserialize(value)
50+
# Delegates to $1
51+
# @param value [Object]
52+
# @return [Object]
4253
def self.delegate_to(object, methods = nil)
4354
extend Forwardable
4455

@@ -74,7 +85,7 @@ def initialize(serialize_unless_string: false, **_)
7485
# string.
7586
#
7687
# @param value [Object] object to encode
77-
# @return [String] The serialized string
88+
# @return [Object] The serialized object (usually string)
7889
def encode(value)
7990
if @serialize_unless_string && String === value
8091
value
@@ -85,7 +96,7 @@ def encode(value)
8596

8697
# Calls {#deserialize} if implemented and if +serialize_unless_string+ was disabled
8798
#
88-
# @param value [String] object to decode
99+
# @param value [Object] object to decode
89100
# @return [Object] the decoded object
90101
# @raise [NotImplementedError] if this serializer cannot do deserialization
91102
def decode(value)
@@ -99,6 +110,20 @@ def decode(value)
99110
def decodable?
100111
!@serialize_unless_string && respond_to?(:deserialize)
101112
end
113+
114+
# @!method serialize(value)
115+
# This method will be called by {#encode} except when the value being encoded is a string, and the object is in
116+
# +serialize_unless_string+ mode.
117+
# @abstract All Subclasses should implement this method
118+
# @param value [Object] the thing to encode
119+
# @return [Object]
120+
#
121+
# @!method decode(value)
122+
# This method will be called by {#decode} except when in +serialize_unless_string+ mode (in which case
123+
# deserialization is disabled).
124+
# @abstract Subclasses where it is possible to deserialize again should implement this method
125+
# @param value [Object] the thing to decode
126+
# @return [Object]
102127
end
103128
end
104129
end

lib/moneta/transforms.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Moneta
2+
# This module is used to namespace Moneta's built-in transform classes, as used by {Moneta::Transformer}.
23
module Transforms
34
TRANSFORMS = %i[
45
BEncode
@@ -45,13 +46,18 @@ module Transforms
4546
autoload transform, "moneta/transforms/#{transform.to_s.downcase}"
4647
end
4748

49+
# Used by {Moneta::Transformer} to initialise instances of {Moneta::Transform} to do key/value encoding and
50+
# decoding.
51+
#
52+
# @param name [String,Symbol] The name of the transform
53+
# @return [Module] A class that implements the {Moneta::Transform} interface when initialized
4854
def self.module_for(name)
4955
transform_sym =
5056
case name
5157
when :msgpack
5258
:MessagePack
5359
else
54-
name_str = name.to_s.gsub('_', '')
60+
name_str = name.to_s.gsub("_", "")
5561
TRANSFORMS.find do |transform|
5662
transform == name ||
5763
transform.to_s.downcase == name_str

lib/moneta/transforms/base64.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Moneta
44
module Transforms
5+
# Encodes text as Base64 using the stdlib +base64+ library
56
class Base64 < Transform
67
def initialize(url_safe: false, strict: false, **options)
78
super

lib/moneta/transforms/bencode.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
require 'bencode'
1+
require "bencode"
22

33
module Moneta
44
module Transforms
5+
# Serializes objects using the {https://github.com/dasch/ruby-bencode bencode gem}
56
class BEncode < Transform::Serializer
7+
# (see Transform::Serializer#serialize)
68
def serialize(value)
79
::BEncode.dump(value)
810
end
911

12+
# (see Transform::Serializer#deserialize)
1013
def deserialize(value)
1114
# BEncode needs a mutable string
1215
::BEncode.load(value.dup).tap do |deserialized_value|

lib/moneta/transforms/bert.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
require 'bert'
1+
require "bert"
22

33
module Moneta
44
module Transforms
5+
# Encodes text using the {https://rubygems.org/gems/bert bert gem}.
56
class BERT < Transform::Serializer
67
delegate_to ::BERT
78
end

lib/moneta/transforms/bson.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
require 'bson'
1+
require "bson"
22

33
module Moneta
44
module Transforms
5+
# Serializes objects to binary strings using the {https://rubygems.org/gems/bson bson gem}
56
class BSON < Transforms::Serializer
7+
# Serialize to BSON
8+
#
9+
# @param value [Object]
10+
# @return [String]
611
def serialize(value)
7-
::BSON::Document['v' => value].to_bson.to_s
12+
::BSON::Document["v" => value].to_bson.to_s
813
end
914

15+
# Deserialize from BSON
16+
#
17+
# @param value [String]
18+
# @return [Object]
1019
def deserialize(value)
11-
::BSON::Document.from_bson(::BSON::ByteBuffer.new(value))['v']
20+
::BSON::Document.from_bson(::BSON::ByteBuffer.new(value))["v"]
1221
end
1322
end
1423
end

lib/moneta/transforms/bzip2.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
require 'rbzip2'
1+
require "rbzip2"
2+
require "stringio"
23

34
module Moneta
45
module Transforms
6+
# Compresses strings using the {https://rubygems.org/gems/rbzip2 rbzip2 gem}
57
class Bzip2 < Transform
8+
# Compresses using BZip2
9+
#
10+
# @param value [String]
11+
# @return [String]
612
def encode(value)
713
io = ::StringIO.new
814
bz = ::RBzip2.default_adapter::Compressor.new(io)
@@ -11,6 +17,18 @@ def encode(value)
1117
io.string
1218
end
1319

20+
# Returns true if the string starts with the right magic number ("BZh")
21+
#
22+
# @param value [Object]
23+
# @return [Boolean]
24+
def encoded?(value)
25+
String === value && value.byteslice(0, 3) == "BZh"
26+
end
27+
28+
# Decompresses BZip2-compressed data
29+
#
30+
# @param value [String]
31+
# @return [String]
1432
def decode(value)
1533
::RBzip2.default_adapter::Decompressor.new(::StringIO.new(value)).read
1634
end

lib/moneta/transforms/city128.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
require 'cityhash'
1+
require "cityhash"
22

33
module Moneta
44
module Transforms
5+
# Hashes strings using the {https://rubygems.org/gems/cityhash cityhash gem} - 128 bit version
56
class City128 < Transform
7+
# Hashes using the +CityHash128+ algorithm
8+
#
9+
# @param [String]
10+
# @return [String]
611
def encode(value)
712
::CityHash.hash128(value).to_s(16)
813
end

lib/moneta/transforms/city32.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
require 'cityhash'
1+
require "cityhash"
22

33
module Moneta
44
module Transforms
5+
# Hashes strings using the {https://rubygems.org/gems/cityhash cityhash gem} - 32 bit version
56
class City32 < Transform
7+
# Hashes using the +CityHash32+ algorithm
8+
#
9+
# @param [String]
10+
# @return [String]
611
def encode(value)
712
::CityHash.hash32(value).to_s(16)
813
end

0 commit comments

Comments
 (0)