object-template 0.7 → 0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/object-template.rb +14 -7
- data/test/test-key-conv.rb +84 -35
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5bc6ab2eb7270d6d725ee9f0e37d6ff2ad31789d
|
4
|
+
data.tar.gz: d20466007e0d8895d6d97184cde46729b4c17d02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c40640688e71dabf0dcb8374d036ec9aa4c8f41c5c997c2f850f58d2437fce8eba4cf25c0e41dd6bbc26aa18cb6954efdd1e586fd2c773f4216167f4547884e9
|
7
|
+
data.tar.gz: e30e5141b82b1d2dbeb846efa765566bb331741f59f9bfc75f934ee9b4305ce54c426d2e70497cf68c66293fe517487c1fd22d63e3fbca70344c353eb04e7727
|
data/lib/object-template.rb
CHANGED
@@ -2,9 +2,9 @@ require 'set'
|
|
2
2
|
|
3
3
|
# Base class for classes of templates used to match somewhat arbitrary objects.
|
4
4
|
class ObjectTemplate
|
5
|
-
VERSION = "0.
|
5
|
+
VERSION = "0.8"
|
6
6
|
|
7
|
-
attr_reader :spec
|
7
|
+
attr_reader :spec, :key_converter
|
8
8
|
|
9
9
|
# A set implementation that treats the matching operator (===) as membership.
|
10
10
|
# Used internally by PortableObjectTemplate, but can also be used in
|
@@ -15,7 +15,7 @@ class ObjectTemplate
|
|
15
15
|
|
16
16
|
# The key_converter is for matching objects that, for example, have had symbol
|
17
17
|
# keys serialized to strings. Using a converter that round-trips through the
|
18
|
-
# same
|
18
|
+
# same serializer, symbols in keys will match strings. However, symbols in
|
19
19
|
# values will not.
|
20
20
|
def initialize spec, key_converter = nil
|
21
21
|
unless spec.respond_to? :size and spec.respond_to? :each
|
@@ -24,18 +24,19 @@ class ObjectTemplate
|
|
24
24
|
|
25
25
|
@spec = spec
|
26
26
|
@size = spec.size
|
27
|
+
@key_converter = key_converter
|
27
28
|
@matchers = []
|
28
29
|
|
29
30
|
if spec.respond_to? :to_hash # assume hash-like
|
30
31
|
@shibboleth = :to_hash
|
31
32
|
spec.each do |k, v|
|
32
|
-
|
33
|
-
|
34
|
-
# or other non-serializable object.
|
33
|
+
if key_converter
|
34
|
+
k = key_converter[k => nil].keys[0]
|
35
35
|
# Note: the [k=>nil].keys[0] is needed instead of just [k]
|
36
36
|
# because the key_converter might not convert values, only keys.
|
37
37
|
# We do not assume that k is a scalar.
|
38
|
-
|
38
|
+
end
|
39
|
+
fill_matchers k, v
|
39
40
|
end
|
40
41
|
|
41
42
|
else # assume array-like
|
@@ -93,6 +94,9 @@ end
|
|
93
94
|
# classes, regexes, ranges, and so on, in addition to single values.
|
94
95
|
class RubyObjectTemplate < ObjectTemplate
|
95
96
|
def fill_matchers k, v # :nodoc:
|
97
|
+
if key_converter
|
98
|
+
(v = key_converter[v]) rescue nil # error means templating object
|
99
|
+
end
|
96
100
|
@matchers << [k, v]
|
97
101
|
end
|
98
102
|
end
|
@@ -116,6 +120,9 @@ class PortableObjectTemplate < ObjectTemplate
|
|
116
120
|
v.each do |kk, vv|
|
117
121
|
case kk
|
118
122
|
when :value, "value"
|
123
|
+
if key_converter
|
124
|
+
(vv = key_converter[vv]) rescue nil # error means templating object
|
125
|
+
end
|
119
126
|
@matchers << [k, vv]
|
120
127
|
when :set, "set"
|
121
128
|
@matchers << [k, MemberMatchingSet.new(vv)]
|
data/test/test-key-conv.rb
CHANGED
@@ -4,41 +4,90 @@ require 'minitest/autorun'
|
|
4
4
|
require 'object-template'
|
5
5
|
require 'assert-threequal'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
7
|
+
begin
|
8
|
+
require 'msgpack'
|
9
|
+
rescue LoadError
|
10
|
+
$stderr.puts "skipping TestKeyConv: msgpack lib not available"
|
11
|
+
else
|
12
|
+
|
13
|
+
class TestKeyConv < Minitest::Test
|
14
|
+
include AssertThreequal
|
15
|
+
|
16
|
+
POT = PortableObjectTemplate
|
17
|
+
ROT = RubyObjectTemplate
|
18
|
+
|
19
|
+
CK_STR = proc {|x|
|
20
|
+
MessagePack.load(
|
21
|
+
MessagePack.dump(x))}
|
22
|
+
CK_SYM = proc {|x|
|
23
|
+
MessagePack.load(
|
24
|
+
MessagePack.dump(x), symbolize_keys: true)}
|
25
|
+
|
26
|
+
def test_match_converted_key
|
27
|
+
assert_threequal(
|
28
|
+
POT.new(
|
29
|
+
{foo: {value: {bar: 1}}}, CK_STR),
|
30
|
+
{"foo" => {"bar" => 1}}
|
31
|
+
)
|
32
|
+
|
33
|
+
assert_threequal(
|
34
|
+
POT.new(
|
35
|
+
{foo: {value: {"bar" => 1}}}, CK_STR),
|
36
|
+
{"foo" => {"bar" => 1}}
|
37
|
+
)
|
38
|
+
|
39
|
+
assert_threequal(
|
40
|
+
ROT.new(
|
41
|
+
{foo: {bar: 1}}, CK_STR),
|
42
|
+
{"foo" => {"bar" => 1}}
|
43
|
+
)
|
28
44
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
45
|
+
assert_threequal(
|
46
|
+
ROT.new(
|
47
|
+
{"foo" => {"bar" => 1}}, CK_STR),
|
48
|
+
{"foo" => {"bar" => 1}}
|
49
|
+
)
|
50
|
+
|
51
|
+
assert_threequal(
|
52
|
+
POT.new(
|
53
|
+
{foo: {value: {bar: 1}}}, CK_SYM),
|
54
|
+
{foo: {bar: 1}}
|
55
|
+
)
|
56
|
+
|
57
|
+
assert_threequal(
|
58
|
+
POT.new(
|
59
|
+
{foo: {value: {"bar" => 1}}}, CK_SYM),
|
60
|
+
{foo: {bar: 1}}
|
61
|
+
)
|
62
|
+
|
63
|
+
assert_threequal(
|
64
|
+
ROT.new(
|
65
|
+
{foo: {bar: 1}}, CK_SYM),
|
66
|
+
{foo: {bar: 1}}
|
67
|
+
)
|
68
|
+
|
69
|
+
assert_threequal(
|
70
|
+
ROT.new(
|
71
|
+
{"foo" => {"bar" => 1}}, CK_SYM),
|
72
|
+
{foo: {bar: 1}}
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_not_match_unconverted_key
|
77
|
+
assert_not_threequal(
|
78
|
+
POT.new(
|
79
|
+
{foo: {value: 1}},
|
80
|
+
proc {|x| x}),
|
81
|
+
{"foo" => 1}
|
82
|
+
)
|
83
|
+
|
84
|
+
assert_not_threequal(
|
85
|
+
ROT.new(
|
86
|
+
{foo: 1},
|
87
|
+
proc {|x| x}),
|
88
|
+
{"foo" => 1}
|
89
|
+
)
|
90
|
+
end
|
43
91
|
end
|
92
|
+
|
44
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object-template
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.8'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel VanderWerf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Templates for matching objects.
|
14
14
|
email: vjoel@users.sourceforge.net
|