polyrex-schema 0.3.0 → 0.3.1
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/polyrex-schema.rb +118 -117
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26291a8341252e4164caf31952ee7a45fbef8a8e
|
4
|
+
data.tar.gz: 5cb8179e93df122993ca1b42803efb082b610699
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0340e64343262982678285c90ccd4201d89bb9e31c712a0cdacc0d89a13e161656213117d1d1dd509437ff0d1dce769c21e797d8378b201e89094cd7e49a4b1b
|
7
|
+
data.tar.gz: 41e0a86fa1cce7c0eaa6caa678577701cf408466dbbd131df26341cb3da377628570cb0bc77d07a2fc25d2c97179f843d37991ab1ea9d24ae782418db1f9d648
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/polyrex-schema.rb
CHANGED
@@ -1,123 +1,124 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
# file: polyrex-schema.rb
|
4
|
-
|
5
|
-
require 'rexle'
|
6
|
-
|
7
|
-
class PolyrexSchema
|
8
|
-
|
9
|
-
def initialize(s)
|
10
|
-
|
11
|
-
s.prepend 'root/' if s[0] == '{'
|
12
|
-
|
13
|
-
r = add_node split(s)
|
14
|
-
r[3] << node('recordx_type', 'polyrex') << node('schema',s)
|
15
|
-
@doc = Rexle.new(r)
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_a() scan_to_a(@doc.root.xpath 'records/.') end
|
19
|
-
def to_h() scan_to_h(@doc.root.xpath 'records/.') end
|
20
|
-
def to_doc() @doc end
|
21
|
-
def to_s() @doc.to_s end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def node(name, val='', *children)
|
26
|
-
[name, val, {}, *children]
|
27
|
-
end
|
28
|
-
|
29
|
-
def add_node(a)
|
30
|
-
|
31
|
-
return if a.empty?
|
32
|
-
|
33
|
-
schema = a.join('/')
|
34
|
-
line = a.shift
|
35
|
-
raw_siblings = line[/\{.*/]
|
36
|
-
|
37
|
-
if raw_siblings then
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
field_rows
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
node
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
[r.name.to_sym, *fields
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
node
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
{name: r.name.to_sym, fields: fields, schema: schema
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# file: polyrex-schema.rb
|
4
|
+
|
5
|
+
require 'rexle'
|
6
|
+
|
7
|
+
class PolyrexSchema
|
8
|
+
|
9
|
+
def initialize(s)
|
10
|
+
|
11
|
+
s.prepend 'root/' if s[0] == '{'
|
12
|
+
|
13
|
+
r = add_node split(s)
|
14
|
+
r[3] << node('recordx_type', 'polyrex') << node('schema',s)
|
15
|
+
@doc = Rexle.new(r)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_a() scan_to_a(@doc.root.xpath 'records/.') end
|
19
|
+
def to_h() scan_to_h(@doc.root.xpath 'records/.') end
|
20
|
+
def to_doc() @doc end
|
21
|
+
def to_s() @doc.to_s end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def node(name, val='', *children)
|
26
|
+
[name, val, {}, *children]
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_node(a)
|
30
|
+
|
31
|
+
return if a.empty?
|
32
|
+
|
33
|
+
schema = a.join('/')
|
34
|
+
line = a.shift
|
35
|
+
raw_siblings = line[/\{.*/]
|
36
|
+
|
37
|
+
if raw_siblings then
|
38
|
+
return split(raw_siblings[1..-2],';').map {|x| add_node split(x) + a}
|
39
|
+
end
|
40
|
+
|
41
|
+
name, raw_fields = line.split('[',2)
|
42
|
+
|
43
|
+
rows = if raw_fields then
|
44
|
+
|
45
|
+
fields = raw_fields.chop.split(',')
|
46
|
+
field_rows = fields.map {|field| node(field.strip) }
|
47
|
+
field_rows << node('schema', schema)
|
48
|
+
field_rows << node('format_mask', fields.map{|x| "[!%s]" % x.strip}\
|
49
|
+
.join(' '))
|
50
|
+
end
|
51
|
+
|
52
|
+
children = add_node(a)
|
53
|
+
children = [children] if children and children[0].is_a? String
|
54
|
+
|
55
|
+
node(name, '',
|
56
|
+
node('summary', '', *rows), node('records', '', *children)
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
def scan_to_a(nodes)
|
61
|
+
|
62
|
+
nodes.map do |r|
|
63
|
+
|
64
|
+
a = r.xpath('summary/*/name()') # => ["entry", "format_mask"]
|
65
|
+
fields = (a - %w(schema format_mask)).map(&:to_sym)
|
66
|
+
node = r.xpath 'records/.'
|
67
|
+
|
68
|
+
if node.any? then
|
69
|
+
children = scan_to_a(node)
|
70
|
+
[r.name.to_sym, *fields, children]
|
71
|
+
else
|
72
|
+
[r.name.to_sym, *fields]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
def scan_to_h(nodes)
|
79
|
+
|
80
|
+
nodes.map do |r|
|
81
|
+
|
82
|
+
a = r.xpath('summary/*/name()') # => ["entry", "format_mask"]
|
83
|
+
schema = r.text('summary/schema')
|
84
|
+
fields = (a - %w(schema format_mask)).map(&:to_sym)
|
85
|
+
node = r.xpath 'records/.'
|
86
|
+
|
87
|
+
if node.any? then
|
88
|
+
children = scan_to_h(node)
|
89
|
+
{name: r.name.to_sym, fields: fields, schema: schema, children: children}
|
90
|
+
else
|
91
|
+
{name: r.name.to_sym, fields: fields, schema: schema}
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
# splits into levels identified by a slash (/) or a semicolon (;)
|
99
|
+
#
|
100
|
+
def split(s, separator='/')
|
101
|
+
|
101
102
|
brace_count = 0
|
102
|
-
|
103
|
-
s.each_char.inject(['']) do |r, c|
|
104
|
-
|
105
|
-
case c
|
106
|
-
when '{'
|
107
|
-
brace_count += 1
|
108
|
-
when '}'
|
109
|
-
brace_count -= 1
|
110
|
-
end
|
111
|
-
|
112
|
-
if c !=
|
103
|
+
|
104
|
+
s.each_char.inject(['']) do |r, c|
|
105
|
+
|
106
|
+
case c
|
107
|
+
when '{'
|
108
|
+
brace_count += 1
|
109
|
+
when '}'
|
110
|
+
brace_count -= 1
|
111
|
+
end
|
112
|
+
|
113
|
+
if c != separator or brace_count > 0 then
|
113
114
|
r.last << c
|
114
|
-
else
|
115
|
-
c = '' if c ==
|
115
|
+
else
|
116
|
+
c = '' if c == separator
|
116
117
|
r << c
|
117
118
|
end
|
118
119
|
r
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
123
124
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polyrex-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -31,7 +31,7 @@ cert_chain:
|
|
31
31
|
x3ppGgUf/zri8F8syPC4U/PhiV9ZBYGjFZDoQWr9TW6gJ1rEPuB1RYH9RPLaSYy4
|
32
32
|
SFJGImVxuM2M+Q==
|
33
33
|
-----END CERTIFICATE-----
|
34
|
-
date: 2014-
|
34
|
+
date: 2014-09-02 00:00:00.000000000 Z
|
35
35
|
dependencies:
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rexle
|
metadata.gz.sig
CHANGED
Binary file
|