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,29 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class All
7
+ attr_accessor :elements
8
+
9
+ def initialize(elements = [])
10
+ @elements = [*elements]
11
+ end
12
+
13
+ def group(new_source)
14
+ STACK.push Module.new("Groups::#{new_source.name.camelize}", elements)
15
+ end
16
+
17
+ def complex_type(source)
18
+ if source.has_name?
19
+ STACK.push Klass.new(source.name, elements)
20
+ end
21
+
22
+ ComplexType.new elements
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class AtMethod
7
+ include BaseMethod
8
+ include Utils
9
+
10
+ def method_body
11
+ "at '#{source.name}'"
12
+ end
13
+
14
+ def to_h_with_attrs_method
15
+ "hash[:#{method_name}] = #{method_name} if has? '#{source.name}'"
16
+ end
17
+
18
+ def to_builder
19
+ "root << build_element('#{source.name}', data[:#{source.name.underscore}]) if data.key? :#{source.name.underscore}"
20
+ end
21
+
22
+ def sequence(_)
23
+ Sequence.new self
24
+ end
25
+
26
+ def choice(_)
27
+ Choice.new self
28
+ end
29
+
30
+ def all(_)
31
+ All.new self
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ module BaseMethod
7
+ attr_accessor :source
8
+
9
+ def initialize(source)
10
+ @source = source
11
+ end
12
+
13
+ def method_name
14
+ source.name.underscore
15
+ end
16
+
17
+ def to_s
18
+ f = StringIO.new
19
+
20
+ f.puts "def #{method_name}"
21
+ f.puts indent(method_body.lines).join
22
+ f.puts "end"
23
+
24
+ f.string.strip
25
+ end
26
+
27
+ def ==(other)
28
+ method_name == other.method_name &&
29
+ method_body == other.method_body
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,73 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Blank
7
+ def elements
8
+ []
9
+ end
10
+
11
+ def element(source)
12
+ if source.multiple?
13
+ if elements.any?
14
+ new_class = STACK.push Klass.new(source.name, elements)
15
+
16
+ ListMethod.new(source) do |template|
17
+ template.item_class = new_class.name.classify
18
+ end
19
+ else
20
+ ListMethod.new(source) do |template|
21
+ template.item_class = source.has_custom_type? ? source&.type&.classify : 'String'
22
+ end
23
+ end
24
+ elsif source.has_custom_type?
25
+ SubmodelMethod.new(source)
26
+ else
27
+ if elements.any?
28
+ new_class = STACK.push Klass.new(source.name, elements)
29
+ SubmodelMethod.new(source, new_class.name.camelize)
30
+ else
31
+ AtMethod.new(source)
32
+ end
33
+ end
34
+ end
35
+
36
+ def complex_type(source)
37
+ if source.has_name?
38
+ STACK.push Klass.new(source.name)
39
+ else
40
+ ComplexType.new
41
+ end
42
+ end
43
+
44
+ def extension(source)
45
+ if source.custom_extension?
46
+ Extension.new(elements, source.attributes)
47
+ else # basic xsd extension
48
+ self
49
+ end
50
+
51
+ end
52
+
53
+ def include(_)
54
+ Include.new
55
+ end
56
+
57
+ def import(_)
58
+ Import.new
59
+ end
60
+
61
+ def schema(_)
62
+ STACK
63
+ end
64
+
65
+ def group(source)
66
+ ModuleInclude.new(source.ref)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,38 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Choice
7
+ attr_accessor :elements
8
+
9
+ def initialize(elements = [])
10
+ @elements = [*elements]
11
+ end
12
+
13
+ def complex_type(source)
14
+ if source.has_name?
15
+ STACK.push Klass.new(source.name, elements)
16
+ else
17
+ ComplexType.new(elements)
18
+ end
19
+ end
20
+
21
+ def sequence(_)
22
+ flattened = elements.flat_map do |element|
23
+ case element
24
+ when Sequence, Choice, All
25
+ then element.elements
26
+ else
27
+ element
28
+ end
29
+ end
30
+
31
+ Sequence.new flattened
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class ClassInherit
7
+ attr_reader :base
8
+
9
+ def initialize(base)
10
+ @base = base&.camelize
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,46 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class ComplexType
7
+ attr_accessor :elements
8
+
9
+ def initialize(elements = [])
10
+ @elements = elements
11
+ end
12
+
13
+ def schema(_)
14
+ STACK
15
+ end
16
+
17
+ def element(source)
18
+ if source.multiple?
19
+ if elements.any?
20
+ new_class = STACK.push Klass.new(source.name, elements)
21
+
22
+ ListMethod.new(source) do |template|
23
+ template.item_class = new_class.name.camelize
24
+ end
25
+ else
26
+ ListMethod.new(source) do |template|
27
+ template.item_class = source.has_custom_type? ? source&.type&.classify : 'String'
28
+ end
29
+ end
30
+ elsif source.has_custom_type?
31
+ SubmodelMethod.new(source)
32
+ else
33
+ if elements.any?
34
+ new_class = STACK.push Klass.new(source.name, elements)
35
+ SubmodelMethod.new(source, new_class.name.camelize)
36
+ else
37
+ AtMethod.new(source)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,60 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Elements
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 all(_)
27
+ flattened = elements.flat_map do |element|
28
+ case element
29
+ when Sequence, Choice, All
30
+ then element.elements
31
+ else
32
+ element
33
+ end
34
+ end
35
+
36
+ All.new flattened
37
+ end
38
+
39
+ def schema(_)
40
+ STACK
41
+ end
42
+
43
+ def choice(_)
44
+ flattened = elements.flat_map do |element|
45
+ case element
46
+ when Sequence, Choice, All
47
+ then element.elements
48
+ else
49
+ element
50
+ end
51
+ end
52
+
53
+ Choice.new flattened
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,30 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Extension
7
+ attr_accessor :elements, :attributes
8
+
9
+ def initialize(elements = [], attributes)
10
+ @elements = [*elements]
11
+ @attributes = attributes
12
+ end
13
+
14
+ def complex_type(source)
15
+ if source.has_name?
16
+ template = Klass.new(source.name.camelize, elements) do |template|
17
+ template.inherit_from = attributes['base'].camelize
18
+ end
19
+
20
+ STACK.push template
21
+ else
22
+ ComplexType.new elements + [ClassInherit.new(attributes['base'])]
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Import
7
+ def schema(_)
8
+ STACK
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Include
7
+ def schema(_)
8
+ STACK
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,121 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class Klass
7
+ include Utils
8
+
9
+ attr_accessor :name, :namespace, :methods, :inherit_from, :includes
10
+
11
+ def initialize(name = nil, elements = [])
12
+ @name = name&.camelize
13
+
14
+ includes, methods = [*elements].partition do |e|
15
+ e.is_a? ModuleInclude
16
+ end
17
+ inherits, methods = methods.partition do |e|
18
+ e.is_a? ClassInherit
19
+ end
20
+
21
+ @methods = methods
22
+ @includes = includes
23
+ @inherit_from = inherits.first.base if inherits.any?
24
+
25
+ yield self if block_given?
26
+ end
27
+
28
+ def schema(_)
29
+ STACK
30
+ end
31
+
32
+ def ==(other)
33
+ name == other.name &&
34
+ namespace == other.namespace &&
35
+ methods == other.methods &&
36
+ inherit_from == other.inherit_from
37
+ end
38
+
39
+ def to_s
40
+ f = StringIO.new
41
+
42
+ if inherit_from
43
+ f.puts "class #{name} < #{inherit_from}"
44
+ else
45
+ f.puts "class #{name}"
46
+ end
47
+ f.puts " include BaseParser"
48
+ includes.each { |incl| f.puts " include Groups::#{incl.ref}" }
49
+ if methods.any? || includes.any?
50
+ f.puts if methods.any?
51
+ f.puts methods.map { |method| indent(method.to_s.lines).join }.join("\n\n")
52
+ f.puts if methods.any?
53
+ f.puts " def to_h_with_attrs"
54
+ f.puts " hash = HashWithAttributes.new({}, attributes)"
55
+ f.puts
56
+ methods.each { |method| f.puts " #{method.to_h_with_attrs_method}" }
57
+ f.puts if methods.any?
58
+ if includes.any?
59
+ f.puts " mega.inject(hash) { |memo, r| memo.merge r }"
60
+ else
61
+ f.puts " hash"
62
+ end
63
+ if inherit_from
64
+ f.puts " super.merge(hash)"
65
+ end
66
+ f.puts " end"
67
+ end
68
+
69
+ f.puts "end"
70
+
71
+ string = f.string.strip
72
+
73
+ wrapped = wrap_in_namespace(string, 'Parsers')
74
+ wrapped = wrap_in_namespace(wrapped, namespace) if namespace
75
+
76
+ wrapped
77
+ end
78
+
79
+ def to_builder_s
80
+ f = StringIO.new
81
+
82
+ if inherit_from
83
+ f.puts "class #{name} < #{inherit_from}"
84
+ else
85
+ f.puts "class #{name}"
86
+ end
87
+ f.puts " include BaseBuilder"
88
+ f.puts
89
+ f.puts " def builder"
90
+ f.puts " root = Ox::Element.new(name)"
91
+ f.puts " if data.respond_to? :attributes"
92
+ f.puts " data.attributes.each { |k, v| root[k] = v }"
93
+ f.puts " end"
94
+ f.puts
95
+ if inherit_from
96
+ f.puts " super.nodes.each do |n|"
97
+ f.puts " root << n"
98
+ f.puts " end"
99
+ f.puts
100
+ end
101
+
102
+ f.puts methods.map { |method| indent(indent(method.to_builder.lines)).join }.join("\n")
103
+ f.puts
104
+ f.puts " root"
105
+ f.puts " end"
106
+
107
+ f.puts "end"
108
+
109
+ string = f.string.strip
110
+
111
+ wrapped = wrap_in_namespace(string, 'Builders')
112
+ wrapped = wrap_in_namespace(wrapped, namespace) if namespace
113
+
114
+ wrapped
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end