object-template 0.4 → 0.5
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 +56 -1
- data/test/test-match.rb +2 -0
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fa8ca2bf4796154808248e6699f83e7b563ab1f
|
4
|
+
data.tar.gz: 0981786792b58f0288f5e62ee6a06c4d04764129
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8778e3da7d5b23710b8e3fda65d67b821f5fcedf315377dafa57fbefeb3e127a2b7a99600912372d7520bf49e250e996591b492233cced0e091fdd464bee2985
|
7
|
+
data.tar.gz: df1183acc120dbd01e2d9054a5ff252a2c0dfb16ed5b76f42418929a575e36a7ffc83627e1c65f7205da90488e026641136b7518f9507dc3ac9def9f62564031
|
data/lib/object-template.rb
CHANGED
@@ -2,7 +2,7 @@ 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.5"
|
6
6
|
|
7
7
|
attr_reader :spec
|
8
8
|
|
@@ -138,6 +138,7 @@ class PortableObjectTemplate < ObjectTemplate
|
|
138
138
|
CLASS_FOR = {
|
139
139
|
"boolean" => BOOLEAN,
|
140
140
|
"number" => Numeric,
|
141
|
+
"integer" => Integer,
|
141
142
|
"string" => String,
|
142
143
|
"list" => Array,
|
143
144
|
"map" => Hash
|
@@ -146,4 +147,58 @@ class PortableObjectTemplate < ObjectTemplate
|
|
146
147
|
CLASS_FOR.default_proc = proc do |h,k|
|
147
148
|
raise ArgumentError, "no known class for matching type #{k.inspect}"
|
148
149
|
end
|
150
|
+
|
151
|
+
# Convert a ROT spec into a POT spec (without creating a ROT). Not completely
|
152
|
+
# general: some POTs cannot be represented in this way.
|
153
|
+
def self.spec_from rot_spec
|
154
|
+
case rot_spec
|
155
|
+
when Array
|
156
|
+
rot_spec.map do |col_rot_spec|
|
157
|
+
column_spec_from(col_rot_spec)
|
158
|
+
end
|
159
|
+
when Hash
|
160
|
+
h = {}
|
161
|
+
rot_spec.each do |k, col_rot_spec|
|
162
|
+
h[k] = column_spec_from(col_rot_spec)
|
163
|
+
end
|
164
|
+
h
|
165
|
+
else
|
166
|
+
raise ArgumentError
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def self.column_spec_from col_rot_spec
|
171
|
+
case col_rot_spec
|
172
|
+
when nil
|
173
|
+
nil
|
174
|
+
|
175
|
+
when Range
|
176
|
+
range = col_rot_spec
|
177
|
+
raise if range.exclude_end? ##
|
178
|
+
raise unless range.first.kind_of? Numeric ##
|
179
|
+
{type: "number", range: [range.first, range.last]}
|
180
|
+
|
181
|
+
when Module
|
182
|
+
mdl = col_rot_spec
|
183
|
+
class_name,_ = CLASS_FOR.find {|k,v| v == mdl}
|
184
|
+
raise unless class_name
|
185
|
+
{type: class_name}
|
186
|
+
|
187
|
+
when Regexp
|
188
|
+
rx = col_rot_spec
|
189
|
+
{regex: rx.source}
|
190
|
+
|
191
|
+
when MemberMatchingSet
|
192
|
+
set = col_rot_spec
|
193
|
+
if set == BOOLEAN
|
194
|
+
## awkward API: must reference PortableObjectTemplate::BOOLEAN
|
195
|
+
{type: "boolean"}
|
196
|
+
else
|
197
|
+
{set: set.to_a}
|
198
|
+
end
|
199
|
+
|
200
|
+
else # assume it is a value
|
201
|
+
{value: col_rot_spec}
|
202
|
+
end
|
203
|
+
end
|
149
204
|
end
|
data/test/test-match.rb
CHANGED
@@ -113,6 +113,7 @@ ne3 ["foo"], [/o{3,}/], [{regex: "o{3,}"}]
|
|
113
113
|
eq3 [true], [TrueClass], [{type: "boolean"}]
|
114
114
|
eq3 [false], [FalseClass], [{type: "boolean"}]
|
115
115
|
eq3 [42], [Numeric], [{type: "number"}]
|
116
|
+
eq3 [42], [Integer], [{type: "integer"}]
|
116
117
|
eq3 ["foo"], [String], [{type: "string"}]
|
117
118
|
eq3 [[1,2,3]], [Array], [{type: "list"}]
|
118
119
|
eq3 [{a:1, b:2}], [Hash], [{type: "map"}]
|
@@ -120,6 +121,7 @@ eq3 [{a:1, b:2}], [Hash], [{type: "map"}]
|
|
120
121
|
ne3 ["42"], [TrueClass], [{type: "boolean"}]
|
121
122
|
ne3 [0], [FalseClass], [{type: "boolean"}]
|
122
123
|
ne3 ["42"], [Numeric], [{type: "number"}]
|
124
|
+
ne3 [42.5], [Integer], [{type: "integer"}]
|
123
125
|
ne3 [123], [String], [{type: "string"}]
|
124
126
|
ne3 [{a:1, b:2}], [Array], [{type: "list"}]
|
125
127
|
ne3 [[1,2,3]], [Hash], [{type: "map"}]
|
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.5'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel VanderWerf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Templates for matching objects.
|
14
14
|
email: vjoel@users.sourceforge.net
|
@@ -18,17 +18,17 @@ extra_rdoc_files:
|
|
18
18
|
- README.md
|
19
19
|
- COPYING
|
20
20
|
files:
|
21
|
-
- README.md
|
22
21
|
- COPYING
|
22
|
+
- README.md
|
23
23
|
- Rakefile
|
24
|
-
- lib/object-template.rb
|
25
|
-
- bench/lib/bench.rb
|
26
24
|
- bench/bench-match.rb
|
25
|
+
- bench/lib/bench.rb
|
26
|
+
- lib/object-template.rb
|
27
27
|
- test/lib/assert-threequal.rb
|
28
28
|
- test/lib/eq3.rb
|
29
|
-
- test/test-match.rb
|
30
|
-
- test/test-key-conv.rb
|
31
29
|
- test/test-errors.rb
|
30
|
+
- test/test-key-conv.rb
|
31
|
+
- test/test-match.rb
|
32
32
|
homepage: https://github.com/vjoel/object-template
|
33
33
|
licenses:
|
34
34
|
- BSD
|
@@ -57,12 +57,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
57
|
version: '0'
|
58
58
|
requirements: []
|
59
59
|
rubyforge_project:
|
60
|
-
rubygems_version: 2.1
|
60
|
+
rubygems_version: 2.2.1
|
61
61
|
signing_key:
|
62
62
|
specification_version: 4
|
63
63
|
summary: Templates for matching objects
|
64
64
|
test_files:
|
65
|
-
- test/test-match.rb
|
66
65
|
- test/test-key-conv.rb
|
66
|
+
- test/test-match.rb
|
67
67
|
- test/test-errors.rb
|
68
68
|
has_rdoc:
|