scaffold_parser 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (24) hide show
  1. checksums.yaml +4 -4
  2. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/all.rb +3 -3
  3. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/at_method.rb +7 -3
  4. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/base_method.rb +6 -1
  5. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/blank.rb +8 -7
  6. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/choice.rb +5 -12
  7. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/complex_type.rb +4 -4
  8. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/element_ref.rb +21 -0
  9. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/elements.rb +2 -39
  10. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/extension.rb +1 -1
  11. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb +28 -12
  12. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb +11 -5
  13. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb +54 -36
  14. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/module_include.rb +15 -6
  15. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/order_elements.rb +50 -0
  16. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb +6 -2
  17. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/requires.rb +2 -0
  18. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/sequence.rb +3 -19
  19. data/lib/scaffold_parser/scaffolders/xsd/parser/handlers/submodel_method.rb +18 -4
  20. data/lib/scaffold_parser/scaffolders/xsd/parser.rb +12 -23
  21. data/lib/scaffold_parser/scaffolders/xsd.rb +73 -178
  22. data/lib/scaffold_parser.rb +9 -10
  23. data/scaffold_parser.gemspec +1 -1
  24. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a86da6f57bcf43be3e59f2a47531005113ada9ff
4
- data.tar.gz: e29b1f9dbea6e921666a001bc51c9213db18e15d
3
+ metadata.gz: 5f042c0e917538cbf64c26da83731d871a9b024d
4
+ data.tar.gz: 74d9920de765dab0b8daa84774a740f8f9e0c4d7
5
5
  SHA512:
6
- metadata.gz: 664fc7cc63285c13ba2b620bc13224f9752a1887ed33c1ac6ee7e20984aecee83b0fdf7b6f21a948b00162e7b47e7931f935f841accb529ed4c8057dcb33480f
7
- data.tar.gz: 0d469c89f076255eefad50e3290ab8dc1b59cf11976f7ff25418e1957fda6a1306d3480e355fba259dab6a43b04e255a034d804af9a683b0e18b2376fb1fcba5
6
+ metadata.gz: 50d8898fce80d67adb3451646ac39e9ba46dcb669a36803e0d1550dc1f43a100443a290dfef9d8870cdb658bb190f60139b5559f7b2928cfd4b8914710662c0e
7
+ data.tar.gz: e753b2f92964c5efd0d566312f8599f24bb9a3475bef511267b9d45f99d3b0d0c131f0617dd9f7942fecf93dcdf9994b5a06c1120714a95b08908641e8399573
@@ -10,13 +10,13 @@ module ScaffoldParser
10
10
  @elements = [*elements]
11
11
  end
12
12
 
13
- def group(new_source)
14
- STACK.push Module.new("Groups::#{new_source.name.camelize}", elements)
13
+ def group(source)
14
+ STACK.push Module.new(source, elements)
15
15
  end
16
16
 
17
17
  def complex_type(source)
18
18
  if source.has_name?
19
- STACK.push Klass.new(source.name, elements)
19
+ STACK.push Klass.new(source, elements)
20
20
  end
21
21
 
22
22
  ComplexType.new elements
@@ -8,15 +8,19 @@ module ScaffoldParser
8
8
  include Utils
9
9
 
10
10
  def method_body
11
- "at '#{source.name}'"
11
+ "at '#{at}'"
12
+ end
13
+
14
+ def at
15
+ [source.xmlns_prefix, "#{source.name}"].compact.join(':')
12
16
  end
13
17
 
14
18
  def to_h_with_attrs_method
15
- "hash[:#{method_name}] = #{method_name} if has? '#{source.name}'"
19
+ "hash[:#{method_name}] = #{method_name} if has? '#{at}'"
16
20
  end
17
21
 
18
22
  def to_builder
19
- "root << build_element('#{source.name}', data[:#{source.name.underscore}]) if data.key? :#{source.name.underscore}"
23
+ "root << build_element('#{at}', data[:#{method_name}]) if data.key? :#{method_name}"
20
24
  end
21
25
 
22
26
  def sequence(_)
@@ -11,7 +11,12 @@ module ScaffoldParser
11
11
  end
12
12
 
13
13
  def method_name
14
- source.name.underscore
14
+ if source.name
15
+ source.name.underscore
16
+ elsif source.ref
17
+ prefix, name = source.ref.split(':')
18
+ name.underscore
19
+ end
15
20
  end
16
21
 
17
22
  def to_s
@@ -11,22 +11,24 @@ module ScaffoldParser
11
11
  def element(source)
12
12
  if source.multiple?
13
13
  if elements.any?
14
- new_class = STACK.push Klass.new(source.name, elements)
14
+ new_class = STACK.push Klass.new(source, elements)
15
15
 
16
16
  ListMethod.new(source) do |template|
17
17
  template.item_class = new_class.name.classify
18
18
  end
19
19
  else
20
20
  ListMethod.new(source) do |template|
21
- template.item_class = source.has_custom_type? ? source&.type&.classify : 'String'
21
+ template.item_class = source.has_custom_type? ? source.type.split(':').map(&:classify).join('::') : 'String'
22
22
  end
23
23
  end
24
24
  elsif source.has_custom_type?
25
25
  SubmodelMethod.new(source)
26
+ elsif source.has_ref?
27
+ ElementRef.new(source)
26
28
  else
27
29
  if elements.any?
28
- new_class = STACK.push Klass.new(source.name, elements)
29
- SubmodelMethod.new(source, new_class.name.camelize)
30
+ new_class = STACK.push Klass.new(source, elements)
31
+ SubmodelMethod.new(source, new_class.name_with_prefix)
30
32
  else
31
33
  AtMethod.new(source)
32
34
  end
@@ -35,7 +37,7 @@ module ScaffoldParser
35
37
 
36
38
  def complex_type(source)
37
39
  if source.has_name?
38
- STACK.push Klass.new(source.name)
40
+ STACK.push Klass.new(source)
39
41
  else
40
42
  ComplexType.new
41
43
  end
@@ -47,7 +49,6 @@ module ScaffoldParser
47
49
  else # basic xsd extension
48
50
  self
49
51
  end
50
-
51
52
  end
52
53
 
53
54
  def include(_)
@@ -63,7 +64,7 @@ module ScaffoldParser
63
64
  end
64
65
 
65
66
  def group(source)
66
- ModuleInclude.new(source.ref)
67
+ ModuleInclude.new(source)
67
68
  end
68
69
  end
69
70
  end
@@ -4,6 +4,8 @@ module ScaffoldParser
4
4
  class Parser
5
5
  module Handlers
6
6
  class Choice
7
+ include OrderElements
8
+
7
9
  attr_accessor :elements
8
10
 
9
11
  def initialize(elements = [])
@@ -12,23 +14,14 @@ module ScaffoldParser
12
14
 
13
15
  def complex_type(source)
14
16
  if source.has_name?
15
- STACK.push Klass.new(source.name, elements)
17
+ STACK.push Klass.new(source, elements)
16
18
  else
17
19
  ComplexType.new(elements)
18
20
  end
19
21
  end
20
22
 
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
23
+ def extension(source)
24
+ Extension.new elements, source.attributes
32
25
  end
33
26
  end
34
27
  end
@@ -17,22 +17,22 @@ module ScaffoldParser
17
17
  def element(source)
18
18
  if source.multiple?
19
19
  if elements.any?
20
- new_class = STACK.push Klass.new(source.name, elements)
20
+ new_class = STACK.push Klass.new(source, elements)
21
21
 
22
22
  ListMethod.new(source) do |template|
23
23
  template.item_class = new_class.name.camelize
24
24
  end
25
25
  else
26
26
  ListMethod.new(source) do |template|
27
- template.item_class = source.has_custom_type? ? source&.type&.classify : 'String'
27
+ template.item_class = source.has_custom_type? ? source.type.split(':').map(&:classify).join('::') : 'String'
28
28
  end
29
29
  end
30
30
  elsif source.has_custom_type?
31
31
  SubmodelMethod.new(source)
32
32
  else
33
33
  if elements.any?
34
- new_class = STACK.push Klass.new(source.name, elements)
35
- SubmodelMethod.new(source, new_class.name.camelize)
34
+ new_class = STACK.push Klass.new(source, elements)
35
+ SubmodelMethod.new(source, new_class.name_with_prefix)
36
36
  else
37
37
  AtMethod.new(source)
38
38
  end
@@ -0,0 +1,21 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ class ElementRef
7
+ def initialize(source)
8
+ @source = source
9
+ end
10
+
11
+ def to_submodel_method(ref_map)
12
+ name = ref_map[@source.ref].split(':').map(&:classify).join('::')
13
+
14
+ SubmodelMethod.new(@source, name)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,54 +4,17 @@ module ScaffoldParser
4
4
  class Parser
5
5
  module Handlers
6
6
  class Elements
7
+ include OrderElements
8
+
7
9
  attr_accessor :elements
8
10
 
9
11
  def initialize(elements = [])
10
12
  @elements = elements
11
13
  end
12
14
 
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
15
  def schema(_)
40
16
  STACK
41
17
  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
18
  end
56
19
  end
57
20
  end
@@ -13,7 +13,7 @@ module ScaffoldParser
13
13
 
14
14
  def complex_type(source)
15
15
  if source.has_name?
16
- template = Klass.new(source.name.camelize, elements) do |template|
16
+ template = Klass.new(source, elements) do |template|
17
17
  template.inherit_from = attributes['base'].camelize
18
18
  end
19
19
 
@@ -8,8 +8,9 @@ module ScaffoldParser
8
8
 
9
9
  attr_accessor :name, :namespace, :methods, :inherit_from, :includes
10
10
 
11
- def initialize(name = nil, elements = [])
12
- @name = name&.camelize
11
+ def initialize(source = nil, elements = [])
12
+ @name = source&.name&.camelize
13
+ @namespace = source.xmlns_prefix&.camelize
13
14
 
14
15
  includes, methods = [*elements].partition do |e|
15
16
  e.is_a? ModuleInclude
@@ -25,6 +26,10 @@ module ScaffoldParser
25
26
  yield self if block_given?
26
27
  end
27
28
 
29
+ def name_with_prefix
30
+ [namespace, name].compact.map(&:camelize).join('::')
31
+ end
32
+
28
33
  def schema(_)
29
34
  STACK
30
35
  end
@@ -40,18 +45,19 @@ module ScaffoldParser
40
45
  f = StringIO.new
41
46
 
42
47
  if inherit_from
43
- f.puts "class #{name} < #{inherit_from}"
48
+ i = inherit_from.split(':').compact.map(&:camelize).join('::')
49
+ f.puts "class #{name} < #{i}"
44
50
  else
45
51
  f.puts "class #{name}"
46
52
  end
47
- f.puts " include BaseParser"
48
- includes.each { |incl| f.puts " include Groups::#{incl.ref}" }
53
+ f.puts " include ParserCore::BaseParser"
54
+ includes.each { |incl| f.puts " include #{incl.full_ref}" }
49
55
  if methods.any? || includes.any?
50
56
  f.puts if methods.any?
51
57
  f.puts methods.map { |method| indent(method.to_s.lines).join }.join("\n\n")
52
58
  f.puts if methods.any?
53
59
  f.puts " def to_h_with_attrs"
54
- f.puts " hash = HashWithAttributes.new({}, attributes)"
60
+ f.puts " hash = ParserCore::HashWithAttributes.new({}, attributes)"
55
61
  f.puts
56
62
  methods.each { |method| f.puts " #{method.to_h_with_attrs_method}" }
57
63
  f.puts if methods.any?
@@ -70,7 +76,7 @@ module ScaffoldParser
70
76
 
71
77
  string = f.string.strip
72
78
 
73
- wrapped = wrap_in_namespace(string, 'Parsers')
79
+ wrapped = string
74
80
  wrapped = wrap_in_namespace(wrapped, namespace) if namespace
75
81
 
76
82
  wrapped
@@ -80,11 +86,13 @@ module ScaffoldParser
80
86
  f = StringIO.new
81
87
 
82
88
  if inherit_from
83
- f.puts "class #{name} < #{inherit_from}"
89
+ i = inherit_from.split(':').compact.map(&:camelize).join('::')
90
+ f.puts "class #{name} < #{i}"
84
91
  else
85
92
  f.puts "class #{name}"
86
93
  end
87
- f.puts " include BaseBuilder"
94
+ f.puts " include ParserCore::BaseBuilder"
95
+ includes.each { |incl| f.puts " include #{incl.full_ref}" }
88
96
  f.puts
89
97
  f.puts " def builder"
90
98
  f.puts " root = Ox::Element.new(name)"
@@ -99,8 +107,16 @@ module ScaffoldParser
99
107
  f.puts
100
108
  end
101
109
 
102
- f.puts methods.map { |method| indent(indent(method.to_builder.lines)).join }.join("\n")
103
- f.puts
110
+ if methods.any?
111
+ f.puts methods.map { |method| indent(indent(method.to_builder.lines)).join }.join("\n")
112
+ f.puts
113
+ end
114
+ if includes.any?
115
+ f.puts " mega.each do |r|"
116
+ f.puts " r.nodes.each { |n| root << n }"
117
+ f.puts " end"
118
+ f.puts
119
+ end
104
120
  f.puts " root"
105
121
  f.puts " end"
106
122
 
@@ -108,7 +124,7 @@ module ScaffoldParser
108
124
 
109
125
  string = f.string.strip
110
126
 
111
- wrapped = wrap_in_namespace(string, 'Builders')
127
+ wrapped = string
112
128
  wrapped = wrap_in_namespace(wrapped, namespace) if namespace
113
129
 
114
130
  wrapped
@@ -11,7 +11,7 @@ module ScaffoldParser
11
11
 
12
12
  def initialize(source)
13
13
  @source = source
14
- @at = [source.name]
14
+ @at = [[source.xmlns_prefix, "#{source.name}"].compact.join(':')]
15
15
 
16
16
  yield self if block_given?
17
17
  end
@@ -20,11 +20,15 @@ module ScaffoldParser
20
20
  "array_of_at(#{item_class}, #{single_quote(at)})"
21
21
  end
22
22
 
23
+ def name_with_prefix
24
+ [source.xmlns_prefix, "#{source.name}"].compact.join(':')
25
+ end
26
+
23
27
  def to_h_with_attrs_method
24
28
  if item_class == 'String'
25
- "hash[:#{method_name}] = #{method_name} if has? '#{source.name}'"
29
+ "hash[:#{method_name}] = #{method_name} if has? '#{name_with_prefix}'"
26
30
  else
27
- "hash[:#{method_name}] = #{method_name}.map(&:to_h_with_attrs) if has? '#{source.name}'"
31
+ "hash[:#{method_name}] = #{method_name}.map(&:to_h_with_attrs) if has? '#{name_with_prefix}'"
28
32
  end
29
33
  end
30
34
 
@@ -44,7 +48,9 @@ module ScaffoldParser
44
48
 
45
49
  def to_proxy_list(source, path)
46
50
  ProxyListMethod.new(source) do |m|
47
- m.at = [path] + @at
51
+ prefixed_path = [source.xmlns_prefix, "#{source.name}"].compact.join(':')
52
+
53
+ m.at = [prefixed_path] + @at
48
54
  m.item_class = @item_class
49
55
  end
50
56
  end
@@ -63,7 +69,7 @@ module ScaffoldParser
63
69
 
64
70
  def complex_type(source)
65
71
  if source.has_name?
66
- STACK.push Klass.new(source.name, [self])
72
+ STACK.push Klass.new(source, [self])
67
73
  else
68
74
  self
69
75
  end
@@ -8,12 +8,14 @@ module ScaffoldParser
8
8
  class Module
9
9
  include Utils
10
10
 
11
- attr_accessor :name, :namespace, :methods, :includes
11
+ attr_accessor :name, :namespace, :methods, :includes, :inherit_from
12
12
 
13
- def initialize(name = nil, methods = [])
14
- @name = name&.camelize
13
+ def initialize(source = nil, methods = [])
14
+ @name = "Groups::#{source.name.camelize}"
15
15
  @methods = methods
16
16
 
17
+ @namespace = source.xmlns_prefix&.camelize
18
+
17
19
  yield self if block_given?
18
20
  end
19
21
 
@@ -21,43 +23,59 @@ module ScaffoldParser
21
23
  STACK
22
24
  end
23
25
 
26
+ def name_with_prefix
27
+ [namespace, name].compact.map(&:camelize).join('::')
28
+ end
29
+
24
30
  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
31
+ string =
32
+ ModuleTemplate.new(name.demodulize) do |template|
33
+ template.namespaces = ['Groups'].compact
34
+
35
+ methods.each { |method| template.methods << indent(method.to_s.lines).join }
36
+
37
+ meth = StringIO.new
38
+ meth.puts " def to_h_with_attrs"
39
+ meth.puts " hash = ParserCore::HashWithAttributes.new({}, attributes)"
40
+ meth.puts
41
+ methods.each { |method| meth.puts " #{method.to_h_with_attrs_method}" }
42
+ meth.puts
43
+ meth.puts " hash"
44
+ meth.puts " end"
45
+
46
+ template.methods << meth.string
47
+ end.to_s
48
+
49
+ wrapped = string
50
+ wrapped = wrap_in_namespace(wrapped, namespace) if namespace
51
+
52
+ wrapped
41
53
  end
42
54
 
43
55
  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
56
+ string =
57
+ ModuleTemplate.new(name.demodulize) do |template|
58
+ template.namespaces = ['Groups'].compact
59
+
60
+ meth = StringIO.new
61
+ meth.puts " def builder"
62
+ meth.puts " root = Ox::Element.new(name)"
63
+ meth.puts " if data.respond_to? :attributes"
64
+ meth.puts " data.attributes.each { |k, v| root[k] = v }"
65
+ meth.puts " end"
66
+ meth.puts
67
+ meth.puts methods.map { |method| indent(indent(method.to_builder.lines)).join }.join("\n")
68
+ meth.puts
69
+ meth.puts " root"
70
+ meth.puts " end"
71
+
72
+ template.methods = [meth.string]
73
+ end.to_s
74
+
75
+ wrapped = string
76
+ wrapped = wrap_in_namespace(wrapped, namespace) if namespace
77
+
78
+ wrapped
61
79
  end
62
80
  end
63
81
  end
@@ -4,10 +4,19 @@ module ScaffoldParser
4
4
  class Parser
5
5
  module Handlers
6
6
  class ModuleInclude
7
- attr_reader :ref
7
+ attr_reader :ref, :source
8
8
 
9
- def initialize(ref)
10
- @ref = ref&.camelize
9
+ def initialize(source)
10
+ @source = source
11
+ @ref = @source.ref&.camelize
12
+ end
13
+
14
+ def full_ref
15
+ if ref.include? ':'
16
+ [ref.split(':')[0], 'groups', ref.split(':')[1]].compact.map(&:camelize).join('::')
17
+ else
18
+ [source.xmlns_prefix, 'groups', ref].compact.map(&:camelize).join('::')
19
+ end
11
20
  end
12
21
 
13
22
  def sequence(_)
@@ -16,7 +25,7 @@ module ScaffoldParser
16
25
 
17
26
  def complex_type(new_source)
18
27
  if new_source.has_name?
19
- STACK.push Klass.new(new_source.name.camelize, self)
28
+ STACK.push Klass.new(new_source, self)
20
29
  else
21
30
  self
22
31
  end
@@ -24,9 +33,9 @@ module ScaffoldParser
24
33
 
25
34
  def element(new_source)
26
35
  if new_source.has_name?
27
- new_class = STACK.push Klass.new(new_source.name.camelize, self)
36
+ new_class = STACK.push Klass.new(new_source, self)
28
37
 
29
- SubmodelMethod.new(new_source, new_class.name.camelize)
38
+ SubmodelMethod.new(new_source, new_class.name_with_prefix)
30
39
  end
31
40
  end
32
41
  end
@@ -0,0 +1,50 @@
1
+ module ScaffoldParser
2
+ module Scaffolders
3
+ class XSD
4
+ class Parser
5
+ module Handlers
6
+ module OrderElements
7
+ def sequence(_)
8
+ flattened = elements.flat_map do |element|
9
+ case element
10
+ when Sequence, Choice, All
11
+ element.elements
12
+ else
13
+ element
14
+ end
15
+ end
16
+
17
+ Sequence.new flattened
18
+ end
19
+
20
+ def all(_)
21
+ flattened = elements.flat_map do |element|
22
+ case element
23
+ when Sequence, Choice, All
24
+ element.elements
25
+ else
26
+ element
27
+ end
28
+ end
29
+
30
+ All.new flattened
31
+ end
32
+
33
+ def choice(_)
34
+ flattened = elements.flat_map do |element|
35
+ case element
36
+ when Sequence, Choice, All
37
+ element.elements
38
+ else
39
+ element
40
+ end
41
+ end
42
+
43
+ Choice.new flattened
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -18,11 +18,15 @@ module ScaffoldParser
18
18
  "array_of_at(#{item_class}, #{single_quote(at)})"
19
19
  end
20
20
 
21
+ def name_with_prefix
22
+ [source.xmlns_prefix, "#{source.name}"].compact.join(':')
23
+ end
24
+
21
25
  def to_h_with_attrs_method
22
26
  if item_class == 'String'
23
- "hash[:#{method_name}] = #{method_name} if has? '#{source.name}'"
27
+ "hash[:#{method_name}] = #{method_name} if has? '#{name_with_prefix}'"
24
28
  else
25
- "hash[:#{method_name}] = #{method_name}.map(&:to_h_with_attrs) if has? '#{source.name}'"
29
+ "hash[:#{method_name}] = #{method_name}.map(&:to_h_with_attrs) if has? '#{name_with_prefix}'"
26
30
  end
27
31
  end
28
32
 
@@ -1,3 +1,4 @@
1
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/order_elements'
1
2
  require 'scaffold_parser/scaffolders/xsd/parser/handlers/blank'
2
3
  require 'scaffold_parser/scaffolders/xsd/parser/handlers/complex_type'
3
4
  require 'scaffold_parser/scaffolders/xsd/parser/handlers/elements'
@@ -16,4 +17,5 @@ require 'scaffold_parser/scaffolders/xsd/parser/handlers/list_method'
16
17
  require 'scaffold_parser/scaffolders/xsd/parser/handlers/module'
17
18
  require 'scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method'
18
19
  require 'scaffold_parser/scaffolders/xsd/parser/handlers/submodel_method'
20
+ require 'scaffold_parser/scaffolders/xsd/parser/handlers/element_ref'
19
21
  require 'scaffold_parser/scaffolders/xsd/parser/handlers/extension'
@@ -4,39 +4,23 @@ module ScaffoldParser
4
4
  class Parser
5
5
  module Handlers
6
6
  class Sequence
7
+ include OrderElements
7
8
  attr_accessor :elements
8
9
 
9
10
  def initialize(elements = [])
10
11
  @elements = [*elements]
11
12
  end
12
13
 
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
14
  def complex_type(source)
27
15
  if source.has_name?
28
- STACK.push Klass.new(source.name, elements)
16
+ STACK.push Klass.new(source, elements)
29
17
  end
30
18
 
31
19
  ComplexType.new elements
32
20
  end
33
21
 
34
22
  def group(source)
35
- STACK.push Module.new("Groups::#{source.name.camelize}", elements)
36
- end
37
-
38
- def choice(_)
39
- self
23
+ STACK.push Module.new(source, elements)
40
24
  end
41
25
 
42
26
  def extension(source)
@@ -11,22 +11,36 @@ module ScaffoldParser
11
11
 
12
12
  def initialize(source, submodel_class = nil)
13
13
  @source = source
14
- @submodel_class = submodel_class || source.type.camelize
14
+ @submodel_class =
15
+ submodel_class ||
16
+ source.type&.split(':')&.map(&:camelize)&.join('::')
17
+ end
18
+
19
+ def at
20
+ if source.name
21
+ [source.xmlns_prefix, "#{source.name}"].compact.join(':')
22
+ elsif source.ref
23
+ source.ref
24
+ end
15
25
  end
16
26
 
17
27
  def method_body
18
- "submodel_at(#{submodel_class}, '#{source.name}')"
28
+ "submodel_at(#{submodel_class}, '#{at}')"
29
+ end
30
+
31
+ def name_with_prefix
32
+ [source.xmlns_prefix, "#{method_name}"].compact.join(':')
19
33
  end
20
34
 
21
35
  def to_h_with_attrs_method
22
- "hash[:#{method_name}] = #{method_name}.to_h_with_attrs if has? '#{source.name}'"
36
+ "hash[:#{method_name}] = #{method_name}.to_h_with_attrs if has? '#{at}'"
23
37
  end
24
38
 
25
39
  def to_builder
26
40
  f = StringIO.new
27
41
 
28
42
  f.puts "if data.key? :#{method_name}"
29
- f.puts " root << #{submodel_class}.new('#{source.name}', data[:#{source.name.underscore}]).builder"
43
+ f.puts " root << #{submodel_class}.new('#{at}', data[:#{method_name}]).builder"
30
44
  f.puts 'end'
31
45
 
32
46
  f.string.strip
@@ -21,33 +21,22 @@ module ScaffoldParser
21
21
  def call
22
22
  STACK.clear
23
23
 
24
- classes =
25
- xsds.map do |xsd|
26
- if @options[:verbose]
27
- puts "\n\nScaffolding schema which defines:\n#{xsd.children.map { |c| c.name }.compact}\n"
28
- end
29
-
30
- xsd.reverse_traverse do |element, children_result|
31
- handler =
32
- if children_result.empty?
33
- Handlers::Blank.new
34
- elsif children_result.one?
35
- children_result.first
36
- else
37
- Handlers::Elements.new(children_result)
38
- end
39
-
40
- if @options[:verbose]
41
- current_handler = handler.class.to_s.demodulize
42
- childrens = (handler.instance_variable_get('@elements') || []).map { |child| child.class.to_s.demodulize }
43
- puts "#{current_handler}#{childrens}##{element.element_name}"
24
+ xsds.each do |xsd|
25
+ xsd.reverse_traverse do |element, children_result|
26
+ handler =
27
+ if children_result.empty?
28
+ Handlers::Blank.new
29
+ elsif children_result.one?
30
+ children_result.first
31
+ else
32
+ Handlers::Elements.new(children_result)
44
33
  end
45
34
 
46
- handler.send(element.element_name, element)
47
- end
35
+ handler.send(element.element_name, element)
48
36
  end
37
+ end
49
38
 
50
- classes = STACK.to_a
39
+ STACK.to_a
51
40
  end
52
41
  end
53
42
  end
@@ -20,220 +20,115 @@ module ScaffoldParser
20
20
  all = [@doc.schema] + @doc.schema.collect_included_schemas(@parse_options) + @doc.schema.collect_imported_schemas(@parse_options)
21
21
 
22
22
  classes = Parser.call(all, @options)
23
+ top_level_elements = all.flat_map(&:elements)
24
+ ref_map = top_level_elements.map { |e| [e.name_with_prefix, e.type_with_prefix]}.to_h
23
25
 
24
- classes.each do |klass|
25
- klass.methods = klass.methods.map do |meth|
26
- if meth.is_a?(Parser::Handlers::SubmodelMethod) && !classes.map(&:name).include?(meth.submodel_class)
27
- meth.to_at_method
28
- else
29
- meth
30
- end
26
+ # reject dumb classes which are just extension proxies to simple types :D
27
+ classes = classes.reject do |klass|
28
+ inherit_from = classes.find do |cl|
29
+ cl.name_with_prefix == klass&.inherit_from&.split(':')&.map(&:camelize)&.join('::')
31
30
  end
32
- end
33
-
34
- classes.each do |klass|
35
- klass.namespace = @options[:namespace]
36
- end
37
31
 
38
- classes.flat_map do |class_template|
39
- [["parsers/#{class_template.name.underscore}.rb", class_template.to_s],
40
- ["builders/#{class_template.name.underscore}.rb", class_template.to_builder_s],
41
- ["parsers/base_parser.rb", wrap_in_namespace(base_parser_template, @options[:namespace])],
42
- ["builders/base_builder.rb", wrap_in_namespace(base_builder_template, @options[:namespace])],
43
- ["requires.rb", create_requires_template(classes)],
44
- ["hash_with_attrs.rb", wrap_in_namespace(hash_with_attrs_template, @options[:namespace])],
45
- ["mega.rb", wrap_in_namespace(mega_template, @options[:namespace])]
46
- ]
32
+ klass.methods.empty? && klass.includes.empty? && inherit_from.nil?
47
33
  end
48
- end
49
-
50
- private
51
-
52
- def base_parser_template
53
- <<~TEMPLATE
54
- module Parsers
55
- module BaseParser
56
- include Mega
57
- EMPTY_ARRAY = []
58
-
59
- attr_accessor :raw
60
-
61
- def initialize(raw)
62
- @raw = raw
63
- end
64
-
65
- def attributes
66
- raw.attributes
67
- end
68
-
69
- private
70
34
 
71
- def at(locator)
72
- return nil if raw.nil?
73
-
74
- element = raw.locate(locator.to_s).first
75
-
76
- if element
77
- StringWithAttributes.new(element.text, element.attributes)
78
- end
79
- end
80
-
81
- def has?(locator)
82
- raw.locate(locator).any?
83
- end
84
-
85
- def submodel_at(klass, locator)
86
- element_xml = raw.locate(locator).first
87
-
88
- klass.new(element_xml) if element_xml
89
- end
90
-
91
- def array_of_at(klass, locator)
92
- return EMPTY_ARRAY if raw.nil?
93
-
94
- elements = raw.locate([*locator].join('/'))
95
-
96
- elements.map do |element|
97
- klass.new(element)
98
- end
99
- end
100
-
101
- def to_h_with_attrs
102
- hash = HashWithAttributes.new({}, attributes)
103
-
104
- hash
105
- end
106
- end
35
+ # remove dumb classes inheritance
36
+ classes = classes.map do |klass|
37
+ inherit_from = classes.find do |cl|
38
+ cl.name_with_prefix == klass&.inherit_from&.split(':')&.map(&:camelize)&.join('::')
107
39
  end
108
- TEMPLATE
109
- end
110
-
111
- def base_builder_template
112
- <<~TEMPLATE
113
- module Builders
114
- module BaseBuilder
115
- attr_accessor :name, :data, :options
116
-
117
- def initialize(name, data = {}, options = {})
118
- @name = name
119
- @data = data || {}
120
- @options = options || {}
121
- end
122
40
 
123
- def to_xml
124
- encoding = options[:encoding]
125
-
126
- doc_options = { version: '1.0' }
127
- doc_options[:encoding] = encoding if encoding
128
- doc = Ox::Document.new(doc_options)
129
- doc << builder
130
-
131
- dump_options = { with_xml: true }
132
- dump_options[:encoding] = encoding if encoding
133
- Ox.dump(doc, dump_options)
134
- end
135
-
136
- def build_element(name, content)
137
- element = Ox::Element.new(name)
138
- if content.respond_to? :attributes
139
- content.attributes.each { |k, v| element[k] = v }
140
- end
141
-
142
- if content.respond_to? :value
143
- element << content.value if content.value
144
- else
145
- element << content if content
146
- end
147
- element
148
- end
149
- end
41
+ if inherit_from.nil?
42
+ klass.inherit_from = nil
150
43
  end
151
- TEMPLATE
152
- end
44
+ klass
45
+ end
153
46
 
154
- def hash_with_attrs_template
155
- <<~TEMPLATE
156
- class HashWithAttributes
157
- def initialize(hash, attributes = nil)
158
- @hash = hash
159
- @attributes = attributes if attributes
160
- end
47
+ # remove dumb classes includes
48
+ classes = classes.map do |klass|
49
+ existing_includes = (klass.includes || []).select do |incl|
50
+ classes.map(&:name_with_prefix).include? incl.full_ref
51
+ end
161
52
 
162
- def value
163
- @hash
164
- end
53
+ klass.includes = existing_includes
165
54
 
166
- def attributes
167
- @attributes ||= {}
168
- end
55
+ klass
56
+ end
169
57
 
170
- def attributes=(attributes)
171
- @attributes = attributes
172
- end
58
+ # remove proxy lists through named complex types
59
+ # #TODO: could i somehow remove proxy complex types so they are not outputted into class files?
60
+ # ... they are not used anyhow.. probably not used..? Can't they be inherited from or something?
61
+ classes = classes.map do |klass|
62
+ klass.methods = klass.methods.map do |meth|
63
+ if meth.is_a?(Parser::Handlers::SubmodelMethod)
64
+ submodel_class = classes.find { |cl| cl.name_with_prefix == meth.submodel_class }
173
65
 
174
- def ==(other)
175
- if other.respond_to?(:value) && other.respond_to?(:attributes)
176
- value == other.value && other.attributes == attributes
66
+ if (submodel_class.methods.size == 1) && submodel_class.methods.first.is_a?(Parser::Handlers::ListMethod) && submodel_class.inherit_from.nil? && submodel_class.includes.empty?
67
+ submodel_class.methods.first.to_proxy_list(meth.source, meth.at)
177
68
  else
178
- value == other
69
+ meth
179
70
  end
71
+ else
72
+ meth
180
73
  end
74
+ end
181
75
 
182
- def merge(other)
183
- merged_hash = value.merge other.value
184
- merged_attrs = attributes.merge other.attributes
76
+ klass
77
+ end
185
78
 
186
- self.class.new(merged_hash, merged_attrs)
79
+ classes.each do |klass|
80
+ klass.methods = klass.methods.map do |meth|
81
+ if meth.is_a?(Parser::Handlers::SubmodelMethod) && !classes.map(&:name_with_prefix).include?(meth.submodel_class)
82
+ meth.to_at_method
83
+ elsif meth.is_a?(Parser::Handlers::ElementRef)
84
+ meth.to_submodel_method(ref_map)
85
+ else
86
+ meth
187
87
  end
88
+ end
89
+ end
188
90
 
189
- def key?(key)
190
- value.key? key
191
- end
91
+ requires = create_requires_template(classes)
92
+ parsers = classes.map do |klass|
93
+ path = ["parsers", klass.namespace&.underscore, "#{klass.name.underscore}.rb"].compact.join('/')
94
+ string = wrap_in_namespace(klass.to_s, 'Parsers')
192
95
 
193
- def [](key)
194
- value[key]
195
- end
96
+ [path, string]
97
+ end
98
+ builders = classes.map do |klass|
99
+ path = ["builders", klass.namespace&.underscore, "#{klass.name.underscore}.rb"].compact.join('/')
100
+ string = wrap_in_namespace(klass.to_builder_s, 'Builders')
196
101
 
197
- def []=(key, key_value)
198
- value[key] = key_value
199
- end
102
+ [path, string]
103
+ end
200
104
 
201
- def dig(*attrs)
202
- value.dig(*attrs)
203
- end
204
- end
205
- TEMPLATE
206
- end
105
+ all = parsers + builders
106
+ result = all.map do |path, string|
107
+ [path, wrap_in_namespace(string, @options[:namespace])]
108
+ end
207
109
 
208
- def mega_template
209
- <<~TEMPLATE
210
- module Mega
211
- def mega
212
- called_from = caller_locations[0].label
213
- included_modules = (self.class.included_modules - Class.included_modules - [Mega])
214
- included_modules.map { |m| m.instance_method(called_from).bind(self).call }
215
- end
216
- end
217
- TEMPLATE
110
+ result + [['requires.rb', requires]]
218
111
  end
219
112
 
113
+ private
114
+
220
115
  def create_requires_template(classes)
221
116
  modules = classes.select { |cl| cl.is_a? Parser::Handlers::Module }
222
117
  classes = classes.select { |cl| cl.is_a? Parser::Handlers::Klass }
223
118
  with_inheritance, others = classes.partition { |klass| klass.inherit_from }
224
119
 
225
- requires = ['parsers/base_parser', 'builders/base_builder']
120
+ requires = []
226
121
  modules.each do |klass|
227
- requires << "parsers/#{klass.name.underscore}"
228
- requires << "builders/#{klass.name.underscore}"
122
+ requires << ["parsers", klass.namespace&.underscore, klass.name.underscore].compact.join('/')
123
+ requires << ["builders", klass.namespace&.underscore, klass.name.underscore].compact.join('/')
229
124
  end
230
125
  others.each do |klass|
231
- requires << "parsers/#{klass.name.underscore}"
232
- requires << "builders/#{klass.name.underscore}"
126
+ requires << ["parsers", klass.namespace&.underscore, klass.name.underscore].compact.join('/')
127
+ requires << ["builders", klass.namespace&.underscore, klass.name.underscore].compact.join('/')
233
128
  end
234
129
  with_inheritance.each do |klass|
235
- requires << "parsers/#{klass.name.underscore}"
236
- requires << "builders/#{klass.name.underscore}"
130
+ requires << ["parsers", klass.namespace&.underscore, klass.name.underscore].compact.join('/')
131
+ requires << ["builders", klass.namespace&.underscore, klass.name.underscore].compact.join('/')
237
132
  end
238
133
 
239
134
  if @options[:namespace]
@@ -1,17 +1,13 @@
1
1
  require 'xsd_model'
2
+ require 'fileutils'
2
3
  require 'active_support/all'
3
4
  require 'scaffold_parser/scaffolders/xsd'
4
5
 
5
6
  module ScaffoldParser
6
7
  def self.scaffold(path, options = {})
7
- ensure_dir_exists('./tmp/')
8
- ensure_dir_exists('./tmp/builders')
9
- ensure_dir_exists('./tmp/builders/groups')
10
- ensure_dir_exists('./tmp/parsers')
11
- ensure_dir_exists('./tmp/parsers/groups')
12
-
13
8
  scaffold_to_string(File.read(path), options).each do |path, content|
14
9
  complete_path = path.prepend('./tmp/')
10
+ ensure_dir_exists(complete_path, options)
15
11
 
16
12
  puts "Writing out #{complete_path}" if options[:verbose]
17
13
 
@@ -37,10 +33,13 @@ module ScaffoldParser
37
33
 
38
34
  private
39
35
 
40
- def self.ensure_dir_exists(path)
41
- unless Dir.exists?(path)
42
- Dir.mkdir(path)
43
- puts "#{path} directory created"
36
+ def self.ensure_dir_exists(path, options)
37
+ dir = path.split('/')[0..-2].join('/')
38
+
39
+ unless Dir.exists?(dir)
40
+ FileUtils.mkdir_p(dir)
41
+
42
+ puts "#{dir} directory created" if options[:verbose]
44
43
  end
45
44
  end
46
45
  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.6.0'
6
+ spec.version = '0.7.0'
7
7
  spec.authors = ['Premysl Donat']
8
8
  spec.email = ['pdonat@seznam.cz']
9
9
 
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.6.0
4
+ version: 0.7.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-05-04 00:00:00.000000000 Z
11
+ date: 2018-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,6 +150,7 @@ files:
150
150
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/choice.rb
151
151
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/class_inherit.rb
152
152
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/complex_type.rb
153
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/element_ref.rb
153
154
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/elements.rb
154
155
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/extension.rb
155
156
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/import.rb
@@ -158,6 +159,7 @@ files:
158
159
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb
159
160
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb
160
161
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/module_include.rb
162
+ - lib/scaffold_parser/scaffolders/xsd/parser/handlers/order_elements.rb
161
163
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb
162
164
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/requires.rb
163
165
  - lib/scaffold_parser/scaffolders/xsd/parser/handlers/sequence.rb