modern_times 0.2.10 → 0.2.11
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/modern_times/marshal_strategy.rb +34 -15
- data/test/marshal_strategy_test.rb +28 -8
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.11
|
@@ -27,23 +27,42 @@ require 'modern_times/marshal_strategy/yaml'
|
|
27
27
|
|
28
28
|
module ModernTimes
|
29
29
|
module MarshalStrategy
|
30
|
-
|
31
|
-
|
30
|
+
@options = {
|
31
|
+
:ruby => Ruby,
|
32
|
+
:string => String,
|
33
|
+
:json => JSON,
|
34
|
+
:bson => BSON,
|
35
|
+
:yaml => YAML,
|
36
|
+
}
|
37
|
+
|
38
|
+
def self.find(marshaler)
|
39
|
+
if marshaler.nil?
|
32
40
|
return Ruby
|
33
|
-
elsif
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
raise "Invalid marshal strategy: #{
|
41
|
+
elsif marshaler.kind_of? Symbol
|
42
|
+
val = @options[marshaler]
|
43
|
+
return val if val
|
44
|
+
elsif valid?(marshaler)
|
45
|
+
return marshaler
|
46
|
+
end
|
47
|
+
raise "Invalid marshal strategy: #{marshaler}"
|
48
|
+
end
|
49
|
+
|
50
|
+
# Allow user-defined marshal strategies
|
51
|
+
def self.register(hash)
|
52
|
+
hash.each do |key, marshaler|
|
53
|
+
raise "Invalid marshal strategy: #{marshaler}" unless valid?(marshaler)
|
54
|
+
@options[key] = marshaler
|
46
55
|
end
|
47
56
|
end
|
57
|
+
|
58
|
+
def self.unregister(sym)
|
59
|
+
@options.delete(sym)
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.valid?(marshaler)
|
63
|
+
return marshaler.respond_to?(:marshal_type) &&
|
64
|
+
marshaler.respond_to?(:marshal) &&
|
65
|
+
marshaler.respond_to?(:unmarshal)
|
66
|
+
end
|
48
67
|
end
|
49
68
|
end
|
@@ -12,28 +12,48 @@ class Klass
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
module SpockMarshalStrategy
|
16
|
+
def self.marshal_type
|
17
|
+
:text
|
18
|
+
end
|
19
|
+
|
20
|
+
# Change days to hours
|
21
|
+
def self.marshal(i)
|
22
|
+
(i.to_i * 24).to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
# Change days to hours
|
26
|
+
def self.unmarshal(str)
|
27
|
+
str.to_i / 24
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
15
31
|
class MarshalStrategyTest < Test::Unit::TestCase
|
16
32
|
context '' do
|
17
33
|
setup do
|
18
|
-
|
19
|
-
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
25
|
-
@
|
34
|
+
ModernTimes::MarshalStrategy.register(:spock => SpockMarshalStrategy)
|
35
|
+
|
36
|
+
@bson = ModernTimes::MarshalStrategy.find(:bson)
|
37
|
+
@json = ModernTimes::MarshalStrategy.find(:json)
|
38
|
+
@ruby = ModernTimes::MarshalStrategy.find(:ruby)
|
39
|
+
@string = ModernTimes::MarshalStrategy.find(:string)
|
40
|
+
@yaml = ModernTimes::MarshalStrategy.find(:string)
|
41
|
+
@spock = ModernTimes::MarshalStrategy.find(:spock)
|
42
|
+
@spock2 = ModernTimes::MarshalStrategy.find(SpockMarshalStrategy)
|
26
43
|
end
|
27
44
|
|
28
45
|
should 'marshal and unmarshal correctly' do
|
29
46
|
hash = {'foo' => 42, 'bar' => 'zulu'}
|
30
47
|
str = 'abcdef1234'
|
31
48
|
obj = Klass.new('hello')
|
49
|
+
i = 6
|
32
50
|
assert_equal hash, @bson.unmarshal(@bson.marshal(hash))
|
33
51
|
assert_equal hash, @json.unmarshal(@json.marshal(hash))
|
34
52
|
assert_equal hash, @ruby.unmarshal(@ruby.marshal(hash))
|
35
53
|
assert_equal str, @string.unmarshal(@string.marshal(str))
|
36
54
|
assert_equal obj.hello, @ruby.unmarshal(@ruby.marshal(obj)).hello
|
55
|
+
assert_equal i, @spock.unmarshal(@spock.marshal(i))
|
56
|
+
assert_equal i, @spock2.unmarshal(@spock2.marshal(i))
|
37
57
|
end
|
38
58
|
end
|
39
59
|
end
|