scaffold_parser 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9ecbc5c73c858c25d5f97806fb148720065dcae
4
- data.tar.gz: ca3a1f63c84c8dc18c08f6d8099167eb884797fd
3
+ metadata.gz: bf1b6341574db5a6cee39b7973ab1b10455ecd01
4
+ data.tar.gz: 5c2a38270f517ff5c13e1cd84b803100c1b9b3f3
5
5
  SHA512:
6
- metadata.gz: a4490d4c87ef3d9512b6b69997705ca613b62f788ee77d95b7df721b97dab4e397f8995a7fced42aae3ffb58b2a02138df6e70591d70dd2b8e08f90b880fae9d
7
- data.tar.gz: efdd40de39de53466c90a607b2d301b6aa4c1d9345478091440140d57391fbee9c712855c0c2c74e3946d2fa6639f90fc91460df349d1b9899d0dd6c25958445
6
+ metadata.gz: a065da14a70f8d6ffb6ddc7074f35c9ccc5147af1bc7fa6591c9ba04e14f0623665490a76fb7a65417a3b95c329d646c34d916b5d8e783b4a36a033b9b0d3a1b
7
+ data.tar.gz: 522b3e5444ef11fb978309fd802c866bfe499f39f603e702c349f7425e7e6b418a3896da82b47c31d5631dbf90bd21db384b2f799e86318a93d1ebbb6ba6bc8c
@@ -60,16 +60,30 @@ module ScaffoldParser
60
60
  @raw = raw
61
61
  end
62
62
 
63
+ def attributes
64
+ raw.attributes
65
+ end
66
+
63
67
  private
64
68
 
65
69
  def at(locator)
66
70
  return nil if raw.nil?
67
71
 
68
- raw[locator]
72
+ element = raw.locate(locator.to_s).first
73
+
74
+ if element
75
+ text = WithAttributes.new(element.text)
76
+ text.attributes = element.attributes
77
+ text
78
+ end
79
+ end
80
+
81
+ def has?(locator)
82
+ raw.locate(locator).any?
69
83
  end
70
84
 
71
85
  def submodel_at(klass, locator)
72
- element_xml = at locator
86
+ element_xml = raw.locate(locator).first
73
87
 
74
88
  klass.new(element_xml) if element_xml
75
89
  end
@@ -77,27 +91,10 @@ module ScaffoldParser
77
91
  def array_of_at(klass, locator)
78
92
  return EMPTY_ARRAY if raw.nil?
79
93
 
80
- elements = raw.dig(*locator) || EMPTY_ARRAY
81
- if elements.is_a?(Hash) || elements.is_a?(String)
82
- elements = [elements]
83
- end
84
-
85
- elements.map do |raw|
86
- klass.new(raw)
87
- end
88
- end
89
-
90
- def all(locator)
91
- return EMPTY_ARRAY if raw.nil?
92
-
93
- result = raw[locator]
94
+ elements = raw.locate([*locator].join('/'))
94
95
 
95
- if result.is_a? Hash
96
- [result]
97
- elsif result.is_a? Array
98
- result
99
- else
100
- EMPTY_ARRAY
96
+ elements.map do |element|
97
+ klass.new(element)
101
98
  end
102
99
  end
103
100
  end
@@ -109,11 +106,11 @@ module ScaffoldParser
109
106
  <<~TEMPLATE
110
107
  module Builders
111
108
  module BaseBuilder
112
- attr_accessor :attributes, :element_name
109
+ attr_accessor :name, :data
113
110
 
114
- def initialize(attributes = {}, element_name)
115
- @element_name = element_name
116
- @attributes = attributes || {}
111
+ def initialize(name, data = {})
112
+ @name = name
113
+ @data = data || {}
117
114
  end
118
115
 
119
116
  def to_xml
@@ -122,6 +119,20 @@ module ScaffoldParser
122
119
 
123
120
  Ox.dump(doc, with_xml: true)
124
121
  end
122
+
123
+ def build_element(name, content)
124
+ element = Ox::Element.new(name)
125
+ if content.respond_to? :attributes
126
+ content.attributes.each { |k, v| element[k] = v }
127
+ end
128
+
129
+ if content.respond_to? :value
130
+ element << content.value if content.value
131
+ else
132
+ element << content if content
133
+ end
134
+ element
135
+ end
125
136
  end
126
137
  end
127
138
  TEMPLATE
@@ -33,36 +33,35 @@ module ScaffoldParser
33
33
  f.puts
34
34
 
35
35
  f.putsi " def builder"
36
- f.putsi " root = Ox::Element.new(element_name)"
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"
37
40
 
41
+ f.puts if node.value_nodes.any?
38
42
  node.value_nodes.each do |node|
39
- f.puts
40
- f.putsi " if attributes.key? :#{node.to_name.underscore}"
41
- f.putsi " element = Ox::Element.new('#{node.to_name}')"
42
- f.putsi " element << attributes[:#{node.to_name.underscore}] if attributes[:#{node.to_name.underscore}]"
43
- f.putsi " root << element"
44
- f.putsi " end"
43
+ f.putsi " root << build_element('#{node.to_name}', data[:#{node.to_name.underscore}]) if data.key? :#{node.to_name.underscore}"
45
44
  end
46
45
 
47
46
  node.submodel_nodes.each do |node|
48
47
  f.puts
49
- f.putsi " if attributes.key? :#{node.to_name.underscore}"
50
- f.putsi " root << #{node.to_class_name}.new(attributes[:#{node.to_name.underscore}], '#{node.to_name}').builder"
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"
51
50
  f.putsi " end"
52
51
  end
53
52
 
54
53
  node.array_nodes.reject { |l| l.list_element.xs_type? }.each do |node|
55
54
  if node.named_list?
56
55
  f.puts
57
- f.putsi " if attributes.key? :#{node.to_name.underscore}"
56
+ f.putsi " if data.key? :#{node.to_name.underscore}"
58
57
  f.putsi " element = Ox::Element.new('#{node.to_name}')"
59
- f.putsi " attributes[:#{node.to_name.underscore}].each { |i| element << #{node.list_element.to_class_name}.new(i, '#{node.list_element.to_name}').builder }"
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 }"
60
59
  f.putsi " root << element"
61
60
  f.putsi " end"
62
61
  else # simple_list
63
62
  f.puts
64
- f.putsi " if attributes.key? :#{node.to_name.underscore}"
65
- f.putsi " attributes[:#{node.to_name.underscore}].each { |i| root << #{node.list_element.to_class_name}.new(i, '#{node.list_element.to_name}').builder }"
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 }"
66
65
  f.putsi " end"
67
66
  end
68
67
  end
@@ -71,14 +70,14 @@ module ScaffoldParser
71
70
  f.puts
72
71
 
73
72
  if node.named_list?
74
- f.putsi " if attributes.key? :#{node.to_name.underscore}"
73
+ f.putsi " if data.key? :#{node.to_name.underscore}"
75
74
  f.putsi " element = Ox::Element.new('#{node.to_name}')"
76
- f.putsi " attributes[:#{node.to_name.underscore}].map { |i| Ox::Element.new('#{node.list_element.to_name}') << i }.each { |i| element << i }"
75
+ f.putsi " data[:#{node.to_name.underscore}].map { |i| Ox::Element.new('#{node.list_element.to_name}') << i }.each { |i| element << i }"
77
76
  f.putsi " root << element"
78
77
  f.putsi " end"
79
78
  else #simple_list
80
- f.putsi " if attributes.key? :#{node.to_name.underscore}"
81
- f.putsi " attributes[:#{node.to_name.underscore}].map { |i| Ox::Element.new('#{node.to_name}') << i }.each { |i| root << i }"
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 }"
82
81
  f.putsi " end"
83
82
  end
84
83
  end
@@ -36,7 +36,7 @@ module ScaffoldParser
36
36
  at = method.to_name
37
37
 
38
38
  f.putsi " def #{method_name}"
39
- f.putsi " at :#{at}"
39
+ f.putsi " at '#{at}'"
40
40
  f.putsi " end"
41
41
  end
42
42
 
@@ -48,7 +48,7 @@ module ScaffoldParser
48
48
  at = method.to_name
49
49
 
50
50
  f.putsi " def #{method_name}"
51
- f.putsi " submodel_at(#{klass}, :#{at})"
51
+ f.putsi " submodel_at(#{klass}, '#{at}')"
52
52
  f.putsi " end"
53
53
  end
54
54
 
@@ -57,7 +57,7 @@ module ScaffoldParser
57
57
 
58
58
  list_element_klass = method.list_element_klass
59
59
  method_name = method.to_name.underscore
60
- list_element_at = method.list_element_at.map { |e| ":#{e}" }.join(', ')
60
+ list_element_at = method.list_element_at.map { |e| "'#{e}'" }.join(', ')
61
61
 
62
62
  f.putsi " def #{method_name}"
63
63
  f.putsi " array_of_at(#{list_element_klass}, [#{list_element_at}])"
@@ -69,7 +69,7 @@ module ScaffoldParser
69
69
 
70
70
  list_element_klass = method.list_element_klass
71
71
  method_name = method.to_name.underscore
72
- list_element_at = method.list_element_at.map { |e| ":#{e}" }.join(', ')
72
+ list_element_at = method.list_element_at.map { |e| "'#{e}'" }.join(', ')
73
73
 
74
74
  f.putsi " def #{method_name}"
75
75
  f.putsi " array_of_at(String, [#{list_element_at}])"
@@ -79,25 +79,24 @@ module ScaffoldParser
79
79
  ### to_h method
80
80
  lines = []
81
81
  node.value_nodes.each do |node|
82
- lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore} if raw.key? :#{node.to_name}"
82
+ lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore} if has? '#{node.to_name}'"
83
83
  end
84
84
 
85
85
  node.submodel_nodes.each do |node|
86
- lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore}.to_h if raw.key? :#{node.to_name}"
86
+ lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore}.to_h if has? '#{node.to_name}'"
87
87
  end
88
88
  node.array_nodes.reject { |l| l.list_element.xs_type? }.each do |node|
89
- lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore}.map(&:to_h) if raw.key? :#{node.to_name}"
89
+ lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore}.map(&:to_h) if has? '#{node.to_name}'"
90
90
  end
91
91
  node.array_nodes.select { |l| l.list_element.xs_type? }.each do |node|
92
- lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore} if raw.key? :#{node.to_name}"
92
+ lines << "hash[:#{node.to_name.underscore}] = #{node.to_name.underscore} if has? '#{node.to_name}'"
93
93
  end
94
94
  if lines.any?
95
95
  f.puts
96
- # lines.last.chop!
97
- # first_line = lines.shift
98
96
 
99
97
  f.putsi " def to_h"
100
- f.putsi " hash = {}"
98
+ f.putsi " hash = WithAttributes.new({})"
99
+ f.putsi " hash.attributes = attributes"
101
100
  f.puts
102
101
 
103
102
  lines.each do |line|
@@ -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.3.0'
6
+ spec.version = '0.4.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.3.0
4
+ version: 0.4.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-24 00:00:00.000000000 Z
11
+ date: 2018-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler