modern_times 0.2.10 → 0.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.10
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
- def self.find(marshal_option)
31
- if marshal_option.nil?
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 marshal_option.kind_of? Symbol
34
- return case marshal_option
35
- when :ruby then Ruby
36
- when :string then String
37
- when :json then JSON
38
- when :bson then BSON
39
- when :yaml then YAML
40
- else raise "Invalid marshal strategy: #{marshal_option}"
41
- end
42
- elsif marshal_option.respond_to?(:marshal_type)
43
- return marshal_option
44
- else
45
- raise "Invalid marshal strategy: #{marshal_option}"
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
- @bson = Object.new
19
- @json = Object.new
20
- @ruby = Object.new
21
- @string = Object.new
22
- @bson.extend ModernTimes::MarshalStrategy::BSON
23
- @json.extend ModernTimes::MarshalStrategy::JSON
24
- @ruby.extend ModernTimes::MarshalStrategy::Ruby
25
- @string.extend ModernTimes::MarshalStrategy::String
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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: modern_times
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.10
5
+ version: 0.2.11
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brad Pardee