scaffold_parser 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/lib/scaffold_parser.rb +29 -25
  4. data/lib/scaffold_parser/scaffolders/xsd.rb +140 -33
  5. data/lib/scaffold_parser/scaffolders/xsd/parser.rb +37 -106
  6. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/all.rb +29 -0
  7. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/at_method.rb +38 -0
  8. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/base_method.rb +36 -0
  9. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/blank.rb +73 -0
  10. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/choice.rb +38 -0
  11. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/class_inherit.rb +17 -0
  12. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/complex_type.rb +46 -0
  13. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/elements.rb +60 -0
  14. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/extension.rb +30 -0
  15. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/import.rb +15 -0
  16. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/include.rb +15 -0
  17. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb +121 -0
  18. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb +86 -0
  19. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb +67 -0
  20. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/module_include.rb +37 -0
  21. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb +53 -0
  22. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/requires.rb +19 -0
  23. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/sequence.rb +50 -0
  24. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/submodel_method.rb +59 -0
  25. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/utils.rb +39 -0
  26. data/lib/scaffold_parser/scaffolders/xsd/parser/module_template.rb +35 -0
  27. data/lib/scaffold_parser/scaffolders/xsd/parser/stack.rb +50 -0
  28. data/scaffold_parser.gemspec +2 -1
  29. metadata +39 -6
  30. data/lib/scaffold_parser/file_patches.rb +0 -13
  31. data/lib/scaffold_parser/nokogiri_patches.rb +0 -194
  32. data/lib/scaffold_parser/scaffolders/xsd/builder.rb +0 -105
@@ -0,0 +1,50 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ class Stack
6
+ include Singleton
7
+
8
+ def initialize
9
+ @stack = []
10
+ end
11
+
12
+ def push(value)
13
+ similar_classes = @stack.select { |klass| klass.name.start_with? value.name }
14
+
15
+ if similar_classes.any?
16
+ same_structure_class = similar_classes.find do |kl|
17
+ kl.namespace == value.namespace &&
18
+ kl.methods == value.methods &&
19
+ kl.inherit_from == value.inherit_from
20
+ end
21
+
22
+ if same_structure_class
23
+ same_structure_class
24
+ else
25
+ name_base = value.name
26
+ while @stack.find { |klass| klass.name == value.name }
27
+ counter ||= 1
28
+ value.name = "#{name_base}#{counter += 1}"
29
+ end
30
+ @stack.push value
31
+ value
32
+ end
33
+ else
34
+ @stack.push value
35
+ value
36
+ end
37
+ end
38
+
39
+ def clear
40
+ @stack.clear
41
+ end
42
+
43
+ def to_a
44
+ @stack
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'scaffold_parser'
6
- spec.version = '0.5.0'
6
+ spec.version = '0.6.0'
7
7
  spec.authors = ['Premysl Donat']
8
8
  spec.email = ['pdonat@seznam.cz']
9
9
 
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'rake', '~> 10.0'
24
24
  spec.add_development_dependency 'rspec', '~> 3.0'
25
25
  spec.add_development_dependency 'pry'
26
+ spec.add_development_dependency 'pry-byebug'
26
27
  spec.add_development_dependency 'nokogiri'
27
28
  spec.add_development_dependency 'activesupport'
28
29
  spec.add_development_dependency 'saharspec'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scaffold_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Premysl Donat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-26 00:00:00.000000000 Z
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: nokogiri
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -127,11 +141,30 @@ files:
127
141
  - bin/scaffold_parser
128
142
  - bin/setup
129
143
  - lib/scaffold_parser.rb
130
- - lib/scaffold_parser/file_patches.rb
131
- - lib/scaffold_parser/nokogiri_patches.rb
132
144
  - lib/scaffold_parser/scaffolders/xsd.rb
133
- - lib/scaffold_parser/scaffolders/xsd/builder.rb
134
145
  - lib/scaffold_parser/scaffolders/xsd/parser.rb
146
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/all.rb
147
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/at_method.rb
148
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/base_method.rb
149
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/blank.rb
150
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/choice.rb
151
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/class_inherit.rb
152
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/complex_type.rb
153
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/elements.rb
154
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/extension.rb
155
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/import.rb
156
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/include.rb
157
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb
158
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb
159
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb
160
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/module_include.rb
161
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb
162
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/requires.rb
163
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/sequence.rb
164
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/submodel_method.rb
165
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/utils.rb
166
+ - lib/scaffold_parser/scaffolders/xsd/parser/module_template.rb
167
+ - lib/scaffold_parser/scaffolders/xsd/parser/stack.rb
135
168
  - scaffold_parser.gemspec
136
169
  homepage: https://github.com/Masa331/scaffold_parser
137
170
  licenses:
@@ -153,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
186
  version: '0'
154
187
  requirements: []
155
188
  rubyforge_project:
156
- rubygems_version: 2.6.14
189
+ rubygems_version: 2.6.14.1
157
190
  signing_key:
158
191
  specification_version: 4
159
192
  summary: Tool for fast parser scaffolding
@@ -1,13 +0,0 @@
1
- module ScaffoldParser
2
- module FilePatches
3
- attr_accessor :indent
4
-
5
- def putsi(str)
6
- if indent
7
- puts str.prepend(' ')
8
- else
9
- puts str
10
- end
11
- end
12
- end
13
- end
@@ -1,194 +0,0 @@
1
- module ScaffoldParser
2
- module NokogiriPatches
3
- class DefinitionNotFound < StandardError; end
4
-
5
- module Element
6
- class BlankExtension
7
- def submodel_nodes
8
- []
9
- end
10
-
11
- def value_nodes
12
- []
13
- end
14
-
15
- def array_nodes
16
- []
17
- end
18
- end
19
-
20
- def submodel_nodes
21
- @submodel_nodes ||= attribute_elements.select(&:parent_type?) + extension.submodel_nodes
22
- end
23
-
24
- def value_nodes
25
- @value_nodes ||= attribute_elements.select(&:end_type?) + extension.value_nodes
26
- end
27
-
28
- def array_nodes
29
- @array_nodes ||= attribute_elements.select(&:list_type?) + extension.array_nodes
30
- end
31
-
32
- def end_type?
33
- (xs_type? && !list_type?) || (no_type? && complex_types.empty? && !complex_type? && !list_type?)
34
- end
35
-
36
- def parent_type?
37
- (complex_type? || complex_types.any?) && !list_type?
38
- end
39
-
40
- def max_occurs
41
- case self['maxOccurs']
42
- when 'unbounded'
43
- Float::INFINITY
44
- when String
45
- self['maxOccurs'].to_i
46
- else
47
- 1
48
- end
49
- end
50
-
51
- def list_type?
52
- simple_list? || named_list?
53
- end
54
-
55
- def simple_list?
56
- max_occurs > 1
57
- end
58
-
59
- def named_list?
60
- !simple_list? && definition.list_element.present? && definition.list_element.max_occurs > 1
61
- end
62
-
63
- def list_element
64
- if simple_list?
65
- self
66
- else
67
- xpath('xs:complexType/xs:sequence/xs:element').first
68
- end
69
- end
70
-
71
- def list_element_klass
72
- if simple_list?
73
- definition.to_name.camelize
74
- elsif named_list?
75
- list_element.definition.to_name.camelize
76
- end
77
- end
78
-
79
- def list_element_at
80
- if simple_list?
81
- [to_name]
82
- elsif named_list?
83
- [at_xpath('xs:complexType').to_name, list_element.to_name]
84
- end
85
- end
86
-
87
- def to_class_name
88
- definition.to_name.camelize
89
- end
90
-
91
- def definition
92
- @definition ||=
93
- if self['type'].present? && !self['type'].start_with?('xs:')
94
- find_type(self['type'])
95
- else
96
- self
97
- end
98
- end
99
-
100
- def extension
101
- complex_ext = at_xpath('xs:complexType/xs:complexContent/xs:extension')
102
- # Can be also simple extension but i don't have to do anything with these:
103
- # simple_ext = at_xpath('xs:complexType/xs:simpleContent/xs:extension')
104
-
105
- complex_ext ? find_type(complex_ext['base']) : BlankExtension.new
106
- end
107
-
108
- def extended_simple_type?
109
- simple_ext = at_xpath('xs:complexType/xs:simpleContent/xs:extension')
110
-
111
- simple_ext.present?
112
- end
113
-
114
- def xs_type?
115
- (definition['type'].present? && definition['type'].start_with?('xs:')) || extended_simple_type?
116
- end
117
-
118
- def complex_type?
119
- definition.name == 'complexType'
120
- end
121
-
122
- def complex_types
123
- definition.xpath('xs:complexType[not(xs:simpleContent)]')
124
- end
125
-
126
- def no_type?
127
- definition['type'].blank?
128
- end
129
-
130
- def to_name
131
- self['name'] || at_xpath('..')['name']
132
- end
133
-
134
- private
135
-
136
- def attribute_elements
137
- xpath('xs:sequence/xs:element',
138
- 'xs:sequence/xs:sequence/xs:element',
139
- 'xs:sequence/xs:choice/xs:sequence/xs:element',
140
- 'xs:sequence/xs:choice/xs:element',
141
- 'xs:complexType/xs:sequence/xs:element',
142
- 'xs:complexType/xs:choice/xs:element',
143
- 'xs:complexType/xs:complexContent/xs:extension/xs:sequence/xs:element',
144
- 'xs:complexContent/xs:extension/xs:sequence/xs:element'
145
- )
146
- end
147
-
148
- def find_type(name)
149
- if element = document.at_xpath("//*[@name='#{name}']")
150
- return element
151
- else
152
- definition_schema = collect_includes.find do |doc|
153
- doc.at_xpath("//*[@name='#{name}']").present?
154
- end
155
-
156
- if definition_schema.blank?
157
- fail DefinitionNotFound.new("Couldn't find type definiton for '#{name}' in any included schema.")
158
- end
159
-
160
- definition_schema.at_xpath("//*[@name='#{name}']")
161
- end
162
- end
163
-
164
- def collect_includes(doc = self, collection = [], names = [])
165
- new_names = doc.xpath('//xs:include').map { |incl| incl['schemaLocation'] }
166
-
167
- (new_names - names).each do |name|
168
- new_doc = Nokogiri::XML(File.open(name))
169
-
170
- collection << new_doc
171
- names << name
172
-
173
- collect_includes(new_doc, collection, names)
174
- end
175
-
176
- collection
177
- end
178
- end
179
-
180
- module Document
181
- def submodel_nodes
182
- xpath('xs:schema/xs:complexType|xs:schema/xs:element')
183
- end
184
-
185
- def array_nodes
186
- []
187
- end
188
-
189
- def definition
190
- self
191
- end
192
- end
193
- end
194
- end
@@ -1,105 +0,0 @@
1
- module ScaffoldParser
2
- module Scaffolders
3
- class XSD
4
- class Builder
5
- attr_reader :node
6
-
7
- def self.call(node, options)
8
- self.new(node, options).call
9
- end
10
-
11
- def initialize(node, options)
12
- @node = node
13
- @options = options
14
- end
15
-
16
- def call
17
- puts "Scaffolding builder for #{node.to_name}" if @options[:verbose]
18
-
19
- f = StringIO.new
20
- f.indent = true if @options[:namespace]
21
-
22
- f.puts "require '#{namespaced('builders/base_builder')}'"
23
- requires = node.submodel_nodes.map { |n| n.to_class_name.underscore }.uniq
24
- requires.each { |r| f.puts "require '#{namespaced(r.prepend('builders/'))}'" }
25
- requires = node.array_nodes.reject { |l| l.list_element.xs_type? }.uniq
26
- requires.each { |n| f.puts "require '#{namespaced(n.list_element.to_class_name.underscore.prepend('builders/'))}'" }
27
- f.puts
28
-
29
- f.puts "module #{@options[:namespace]}" if @options[:namespace]
30
- f.putsi "module Builders"
31
- f.putsi " class #{node.to_class_name}"
32
- f.putsi " include BaseBuilder"
33
- f.puts
34
-
35
- f.putsi " def builder"
36
- f.putsi " root = Ox::Element.new(name)"
37
- f.putsi " if data.respond_to? :attributes"
38
- f.putsi " data.attributes.each { |k, v| root[k] = v }"
39
- f.putsi " end"
40
-
41
- f.puts if node.value_nodes.any?
42
- node.value_nodes.each do |node|
43
- f.putsi " root << build_element('#{node.to_name}', data[:#{node.to_name.underscore}]) if data.key? :#{node.to_name.underscore}"
44
- end
45
-
46
- node.submodel_nodes.each do |node|
47
- f.puts
48
- f.putsi " if data.key? :#{node.to_name.underscore}"
49
- f.putsi " root << #{node.to_class_name}.new('#{node.to_name}', data[:#{node.to_name.underscore}]).builder"
50
- f.putsi " end"
51
- end
52
-
53
- node.array_nodes.reject { |l| l.list_element.xs_type? }.each do |node|
54
- if node.named_list?
55
- f.puts
56
- f.putsi " if data.key? :#{node.to_name.underscore}"
57
- f.putsi " element = Ox::Element.new('#{node.to_name}')"
58
- f.putsi " data[:#{node.to_name.underscore}].each { |i| element << #{node.list_element.to_class_name}.new('#{node.list_element.to_name}', i).builder }"
59
- f.putsi " root << element"
60
- f.putsi " end"
61
- else # simple_list
62
- f.puts
63
- f.putsi " if data.key? :#{node.to_name.underscore}"
64
- f.putsi " data[:#{node.to_name.underscore}].each { |i| root << #{node.list_element.to_class_name}.new('#{node.list_element.to_name}', i).builder }"
65
- f.putsi " end"
66
- end
67
- end
68
-
69
- node.array_nodes.select { |l| l.list_element.xs_type? }.each do |node|
70
- f.puts
71
-
72
- if node.named_list?
73
- f.putsi " if data.key? :#{node.to_name.underscore}"
74
- f.putsi " element = Ox::Element.new('#{node.to_name}')"
75
- f.putsi " data[:#{node.to_name.underscore}].map { |i| Ox::Element.new('#{node.list_element.to_name}') << i }.each { |i| element << i }"
76
- f.putsi " root << element"
77
- f.putsi " end"
78
- else #simple_list
79
- f.putsi " if data.key? :#{node.to_name.underscore}"
80
- f.putsi " data[:#{node.to_name.underscore}].map { |i| Ox::Element.new('#{node.to_name}') << i }.each { |i| root << i }"
81
- f.putsi " end"
82
- end
83
- end
84
-
85
- f.puts
86
- f.putsi " root"
87
- f.putsi " end"
88
-
89
-
90
- f.putsi " end"
91
- f.putsi "end"
92
- f.puts "end" if @options[:namespace]
93
-
94
- ["builders/#{node.to_class_name.underscore}.rb", f.string.strip]
95
- end
96
-
97
- private
98
-
99
- def namespaced(path)
100
- [@options[:namespace]&.underscore, path].compact.join('/')
101
- end
102
- end
103
- end
104
- end
105
- end