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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af7dd1f76a6cfe98730cb33f12fb1da876849e8e
4
- data.tar.gz: 36199fb4a8b5baf5208dc738e6a9c1e2a9fb12f6
3
+ metadata.gz: 26291a8341252e4164caf31952ee7a45fbef8a8e
4
+ data.tar.gz: 5cb8179e93df122993ca1b42803efb082b610699
5
5
  SHA512:
6
- metadata.gz: 5dc8b546b014973131fb295df13b6c858e651dadd0a47f0b0c62f6c71f8f0506a8224e5599986f732be5fd3097999caac9f33f9493760b95349b1a7182f46b26
7
- data.tar.gz: b3377913c78b968c486fd30202c9af1220a1c04f75b57bff282d08157440e0c4d9ceac798f0b91786bd5004a39979a9748fcd2a0a75358b5fe043e97a3f1a5fd
6
+ metadata.gz: 0340e64343262982678285c90ccd4201d89bb9e31c712a0cdacc0d89a13e161656213117d1d1dd509437ff0d1dce769c21e797d8378b201e89094cd7e49a4b1b
7
+ data.tar.gz: 41e0a86fa1cce7c0eaa6caa678577701cf408466dbbd131df26341cb3da377628570cb0bc77d07a2fc25d2c97179f843d37991ab1ea9d24ae782418db1f9d648
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -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
- return raw_siblings[1..-2].split(/\s*\s*;/,2)\
40
- .map {|x| add_node split(x) + a}
41
- end
42
-
43
- name, raw_fields = line.split('[',2)
44
-
45
- rows = if raw_fields then
46
-
47
- fields = raw_fields.chop.split(',')
48
- field_rows = fields.map {|field| node(field.strip) }
49
- field_rows << node('schema', schema)
50
- field_rows << node('format_mask', fields.map{|x| "[!%s]" % x.strip}\
51
- .join(' '))
52
- end
53
-
54
- children = add_node(a)
55
- children = [children] if children and children[0].is_a? String
56
-
57
- node(name, '',
58
- node('summary', '', *rows), node('records', '', *children)
59
- )
60
- end
61
-
62
- def scan_to_a(nodes)
63
-
64
- nodes.map do |r|
65
-
66
- a = r.xpath('summary/*/name()') # => ["entry", "format_mask"]
67
- fields = (a - %w(schema format_mask)).map(&:to_sym)
68
- node = r.xpath 'records/.'
69
-
70
- if node.any? then
71
- children = scan_to_a(node)
72
- [r.name.to_sym, *fields, children]
73
- else
74
- [r.name.to_sym, *fields]
75
- end
76
- end
77
-
78
- end
79
-
80
- def scan_to_h(nodes)
81
-
82
- nodes.map do |r|
83
-
84
- a = r.xpath('summary/*/name()') # => ["entry", "format_mask"]
85
- schema = r.text('summary/schema')
86
- fields = (a - %w(schema format_mask)).map(&:to_sym)
87
- node = r.xpath 'records/.'
88
-
89
- if node.any? then
90
- children = scan_to_h(node)
91
- {name: r.name.to_sym, fields: fields, schema: schema, children: children}
92
- else
93
- {name: r.name.to_sym, fields: fields, schema: schema}
94
- end
95
- end
96
-
97
- end
98
-
99
- def split(s)
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 != '/' or brace_count > 0 then
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.0
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-08-26 00:00:00.000000000 Z
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