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,86 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class ListMethod
7
+ include BaseMethod
8
+ include Utils
9
+
10
+ attr_accessor :at, :item_class
11
+
12
+ def initialize(source)
13
+ @source = source
14
+ @at = [source.name]
15
+
16
+ yield self if block_given?
17
+ end
18
+
19
+ def method_body
20
+ "array_of_at(#{item_class}, #{single_quote(at)})"
21
+ end
22
+
23
+ def to_h_with_attrs_method
24
+ if item_class == 'String'
25
+ "hash[:#{method_name}] = #{method_name} if has? '#{source.name}'"
26
+ else
27
+ "hash[:#{method_name}] = #{method_name}.map(&:to_h_with_attrs) if has? '#{source.name}'"
28
+ end
29
+ end
30
+
31
+ def to_builder
32
+ f = StringIO.new
33
+
34
+ f.puts "if data.key? :#{method_name}"
35
+ if item_class == 'String'
36
+ f.puts " data[:#{method_name}].map { |i| Ox::Element.new('#{at.first}') << i }.each { |i| root << i }"
37
+ else
38
+ f.puts " data[:#{method_name}].each { |i| root << #{item_class}.new('#{at.first}', i).builder }"
39
+ end
40
+ f.puts 'end'
41
+
42
+ f.string.strip
43
+ end
44
+
45
+ def to_proxy_list(source, path)
46
+ ProxyListMethod.new(source) do |m|
47
+ m.at = [path] + @at
48
+ m.item_class = @item_class
49
+ end
50
+ end
51
+
52
+ def sequence(_)
53
+ self
54
+ end
55
+
56
+ def choice(_)
57
+ self
58
+ end
59
+
60
+ def all(_)
61
+ self
62
+ end
63
+
64
+ def complex_type(source)
65
+ if source.has_name?
66
+ STACK.push Klass.new(source.name, [self])
67
+ else
68
+ self
69
+ end
70
+ end
71
+
72
+ def element(source)
73
+ if source.has_name?
74
+ to_proxy_list(source, source.name)
75
+ end
76
+ end
77
+
78
+ def extension(source)
79
+ Extension.new(self, source.attributes)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,67 @@
1
+ require 'scaffold_parser/scaffolders/xsd/parser/module_template'
2
+
3
+ module ScaffoldParser
4
+ module Scaffolders
5
+ class XSD
6
+ class Parser
7
+ module Handlers
8
+ class Module
9
+ include Utils
10
+
11
+ attr_accessor :name, :namespace, :methods, :includes
12
+
13
+ def initialize(name = nil, methods = [])
14
+ @name = name&.camelize
15
+ @methods = methods
16
+
17
+ yield self if block_given?
18
+ end
19
+
20
+ def schema(_)
21
+ STACK
22
+ end
23
+
24
+ def to_s
25
+ ModuleTemplate.new(name.demodulize) do |template|
26
+ template.namespaces = ['Groups', 'Parsers', namespace].compact
27
+
28
+ methods.each { |method| template.methods << indent(method.to_s.lines).join }
29
+
30
+ meth = StringIO.new
31
+ meth.puts " def to_h_with_attrs"
32
+ meth.puts " hash = HashWithAttributes.new({}, attributes)"
33
+ meth.puts
34
+ methods.each { |method| meth.puts " #{method.to_h_with_attrs_method}" }
35
+ meth.puts
36
+ meth.puts " hash"
37
+ meth.puts " end"
38
+
39
+ template.methods << meth.string
40
+ end.to_s
41
+ end
42
+
43
+ def to_builder_s
44
+ ModuleTemplate.new(name.demodulize) do |template|
45
+ template.namespaces = ['Groups', 'Builders', namespace].compact
46
+
47
+ meth = StringIO.new
48
+ meth.puts " def builder"
49
+ meth.puts " root = Ox::Element.new(name)"
50
+ meth.puts " if data.respond_to? :attributes"
51
+ meth.puts " data.attributes.each { |k, v| root[k] = v }"
52
+ meth.puts " end"
53
+ meth.puts
54
+ meth.puts methods.map { |method| indent(indent(method.to_builder.lines)).join }.join("\n")
55
+ meth.puts
56
+ meth.puts " root"
57
+ meth.puts " end"
58
+
59
+ template.methods = [meth.string]
60
+ end.to_s
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,37 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class ModuleInclude
7
+ attr_reader :ref
8
+
9
+ def initialize(ref)
10
+ @ref = ref&.camelize
11
+ end
12
+
13
+ def sequence(_)
14
+ Sequence.new self
15
+ end
16
+
17
+ def complex_type(new_source)
18
+ if new_source.has_name?
19
+ STACK.push Klass.new(new_source.name.camelize, self)
20
+ else
21
+ self
22
+ end
23
+ end
24
+
25
+ def element(new_source)
26
+ if new_source.has_name?
27
+ new_class = STACK.push Klass.new(new_source.name.camelize, self)
28
+
29
+ SubmodelMethod.new(new_source, new_class.name.camelize)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,53 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class ProxyListMethod
7
+ include BaseMethod
8
+ include Utils
9
+
10
+ attr_accessor :at, :item_class
11
+
12
+ def initialize(source)
13
+ @source = source
14
+ yield self if block_given?
15
+ end
16
+
17
+ def method_body
18
+ "array_of_at(#{item_class}, #{single_quote(at)})"
19
+ end
20
+
21
+ def to_h_with_attrs_method
22
+ if item_class == 'String'
23
+ "hash[:#{method_name}] = #{method_name} if has? '#{source.name}'"
24
+ else
25
+ "hash[:#{method_name}] = #{method_name}.map(&:to_h_with_attrs) if has? '#{source.name}'"
26
+ end
27
+ end
28
+
29
+ def to_builder
30
+ f = StringIO.new
31
+
32
+ f.puts "if data.key? :#{method_name}"
33
+ f.puts " element = Ox::Element.new('#{at.first}')"
34
+ if item_class == 'String'
35
+ f.puts " data[:#{method_name}].map { |i| Ox::Element.new('#{at.last}') << i }.each { |i| element << i }"
36
+ else
37
+ f.puts " data[:#{method_name}].each { |i| element << #{item_class}.new('#{at.last}', i).builder }"
38
+ end
39
+ f.puts ' root << element'
40
+ f.puts 'end'
41
+
42
+ f.string.strip
43
+ end
44
+
45
+ def sequence(_)
46
+ Sequence.new self
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/blank'
2
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/complex_type'
3
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/elements'
4
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/sequence'
5
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/all'
6
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/choice'
7
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/include'
8
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/import'
9
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/utils'
10
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/base_method'
11
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/module_include'
12
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/class_inherit'
13
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/at_method'
14
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/klass'
15
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/list_method'
16
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/module'
17
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method'
18
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/submodel_method'
19
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/extension'
@@ -0,0 +1,50 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Sequence
7
+ attr_accessor :elements
8
+
9
+ def initialize(elements = [])
10
+ @elements = [*elements]
11
+ end
12
+
13
+ def sequence(_)
14
+ flattened = elements.flat_map do |element|
15
+ case element
16
+ when Sequence, Choice, All
17
+ then element.elements
18
+ else
19
+ element
20
+ end
21
+ end
22
+
23
+ Sequence.new flattened
24
+ end
25
+
26
+ def complex_type(source)
27
+ if source.has_name?
28
+ STACK.push Klass.new(source.name, elements)
29
+ end
30
+
31
+ ComplexType.new elements
32
+ end
33
+
34
+ def group(source)
35
+ STACK.push Module.new("Groups::#{source.name.camelize}", elements)
36
+ end
37
+
38
+ def choice(_)
39
+ self
40
+ end
41
+
42
+ def extension(source)
43
+ Extension.new elements, source.attributes
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,59 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class SubmodelMethod
7
+ include BaseMethod
8
+ include Utils
9
+
10
+ attr_accessor :submodel_class
11
+
12
+ def initialize(source, submodel_class = nil)
13
+ @source = source
14
+ @submodel_class = submodel_class || source.type.camelize
15
+ end
16
+
17
+ def method_body
18
+ "submodel_at(#{submodel_class}, '#{source.name}')"
19
+ end
20
+
21
+ def to_h_with_attrs_method
22
+ "hash[:#{method_name}] = #{method_name}.to_h_with_attrs if has? '#{source.name}'"
23
+ end
24
+
25
+ def to_builder
26
+ f = StringIO.new
27
+
28
+ f.puts "if data.key? :#{method_name}"
29
+ f.puts " root << #{submodel_class}.new('#{source.name}', data[:#{source.name.underscore}]).builder"
30
+ f.puts 'end'
31
+
32
+ f.string.strip
33
+ end
34
+
35
+ def sequence(_)
36
+ Sequence.new self
37
+ end
38
+
39
+ def choice(_)
40
+ Choice.new self
41
+ end
42
+
43
+ def all(_)
44
+ All.new self
45
+ end
46
+
47
+ def schema(_)
48
+ STACK
49
+ end
50
+
51
+ def to_at_method
52
+ AtMethod.new(source)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,39 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ module Utils
7
+ def indent(lines_or_string)
8
+ if lines_or_string.is_a? Array
9
+ lines_or_string.map { |line| indent_string(line) }
10
+ else
11
+ indent_string(lines_or_string)
12
+ end
13
+ end
14
+
15
+ def indent_string(string)
16
+ string == "\n" ? string : string.prepend(' ')
17
+ end
18
+
19
+ def single_quote(string)
20
+ string.to_s.gsub('"', '\'')
21
+ end
22
+
23
+ def wrap_in_namespace(klass, namespace)
24
+ return klass unless namespace
25
+
26
+ lines = klass.lines
27
+ indented = indent(lines)
28
+
29
+ indented.unshift "module #{namespace}\n"
30
+ indented << "\nend"
31
+
32
+ indented.join
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,35 @@
1
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/utils'
2
+
3
+ module ScaffoldParser
4
+ module Scaffolders
5
+ class XSD
6
+ class Parser
7
+ class ModuleTemplate
8
+ include Handlers::Utils
9
+
10
+ attr_accessor :name, :methods, :namespaces
11
+
12
+ def initialize(name = nil)
13
+ @name = name
14
+ @methods = []
15
+ @namespaces = []
16
+
17
+ yield self if block_given?
18
+ end
19
+
20
+ def to_s
21
+ f = StringIO.new
22
+
23
+ f.puts "module #{name}"
24
+ f.puts methods.join("\n\n")
25
+
26
+ f.puts "end"
27
+ string = f.string.strip
28
+
29
+ namespaces.inject(string) { |string, n| wrap_in_namespace(string, n) }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end