xml_dsl 0.3.2 → 0.5.1

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: a06859b2ea2ebab29cd30ca0dd451005459ed911
4
- data.tar.gz: 476df9143b24ecc659d2751d06d4b315216394c5
3
+ metadata.gz: 8d3183fbdb92747ea50fb27908eb272551ebf4bd
4
+ data.tar.gz: 766e973b240d651b0d1fe373c5a0e5a22d02b12b
5
5
  SHA512:
6
- metadata.gz: 8803eeda3e875094fb8a52fed5bf224b5fd0b9f3eebf94f7c6f9632fa8d5be086871bc3e14a05872c0d19795ca6c804662cc98fde2f79fd75554c3db86384343
7
- data.tar.gz: 20d55686eebf557caefd68da858ffd8215c1105ae8d53d9231d0d48309402a6cdb469313d58010f047b13d6667a7c28802ab485844b33f26c228a66316667950
6
+ metadata.gz: 1520370bb9c152b497b8759d79c6b3c86114626e6a23cd3fabb5472fc2eb289ff9e3bc2262a4acffc1562c8a5580fc9626251e7bcb219c868ec1b01e4d68460c
7
+ data.tar.gz: e3de1ad0490194b3f3ed02bbfa208a4b1695b63dccf294cd0bbb8872439dbb220e9da2fd01304c1acb71203eac84d35b21765c803ee13f76e656bf7555cf1d40
data/README.md CHANGED
@@ -66,8 +66,13 @@ Then we can define our mapper as following
66
66
  self.external = external
67
67
  end
68
68
 
69
+ #
69
70
  # Here we definig parser, which will put attributes to Hash, and look for <offer> nodes inside <root>
70
- # any length of root path can be provided
71
+ # any length of root path can be provided, also attributes full path is also a choise
72
+ # define_xml_parser arbitrary_class, :root, "offer[type=uniq]"
73
+ # You can pass as first argument not only Hash, but any Ruby class capable to set attributes defined in fields
74
+ # define_xml_parser OpenStruct
75
+ # define_xml_parser NiftyActiveRecordModel
71
76
  define_xml_parser Hash, :root, :offer do
72
77
 
73
78
  # Here is basic validation blocks
@@ -130,6 +135,9 @@ Then you can use your newly defined parser as you wish:
130
135
 
131
136
  # or you can pass some accumulator for your instances to be put into
132
137
  parser.iterate acc: []
138
+
139
+ # or event call a method on instances without getting them actually
140
+ parser.iterate after_method: :save
133
141
  ```
134
142
 
135
143
  ## Contributing
@@ -7,6 +7,7 @@ module XmlDsl
7
7
  @block = block if block_given?
8
8
  end
9
9
 
10
+ # order of params to be called can differ, so naming is ambiguous
10
11
  def call(a, b = nil, c = nil)
11
12
  if block
12
13
  self.send method, *[a, b, c].compact + args, &block
@@ -17,11 +18,12 @@ module XmlDsl
17
18
 
18
19
  def field(instance, node, parser, target, source = nil, getter: :text, matcher: :to_s, null: false, &block)
19
20
  raise ArgumentError, 'Wrong target' unless target.is_a? Symbol
21
+ wrapped_instance = XmlDsl::InstanceWrapper.new instance
20
22
  if block_given?
21
- instance[target] = parser.instance_exec instance, node, &block
23
+ wrapped_instance.set target, parser.instance_exec(instance, node, &block)
22
24
  else
23
25
  raise ArgumentError, 'No source specified' if source.nil?
24
- instance[target] = node.search(convert_source(source)).send(getter).send(matcher)
26
+ wrapped_instance.set target, node.search(convert_source(source)).send(getter).send(matcher)
25
27
  end
26
28
  if null
27
29
  raise XmlDsl::ParseError, "#{target} is empty. Node: #{node}" if instance[target].nil? || instance[target] == ""
@@ -52,5 +54,21 @@ module XmlDsl
52
54
  source.to_s
53
55
  end
54
56
  end
57
+
58
+ end
59
+ class InstanceWrapper
60
+ attr_accessor :instance
61
+
62
+ def initialize(instance)
63
+ self.instance = instance
64
+ end
65
+
66
+ def set(key, value)
67
+ if instance.is_a? Hash
68
+ instance[key] = value
69
+ else
70
+ instance.send "#{key}=", value
71
+ end
72
+ end
55
73
  end
56
74
  end
@@ -18,7 +18,7 @@ module XmlDsl
18
18
  end
19
19
 
20
20
  module InstanceMethods
21
- def iterate(xml_obj = nil, acc: nil, &block)
21
+ def iterate(xml_obj = nil, acc: nil, after_method: nil, &block)
22
22
  xml_obj ||= xml
23
23
  raise ArgumentError, "If there is no @xml in parser, pass it to iterate" if xml_obj.nil?
24
24
  xml_obj.search(_xml_root_path).each do |node|
@@ -33,6 +33,9 @@ module XmlDsl
33
33
  bm.call instance, node, self
34
34
  end
35
35
  yield instance if block_given?
36
+ if after_method && after_method.is_a?(Symbol)
37
+ instance.send after_method
38
+ end
36
39
  acc << instance if acc
37
40
  rescue XmlDsl::ParseError => e
38
41
  _xml_parse_callbacks[:error_handlers].each do |bm|
@@ -1,3 +1,3 @@
1
1
  module XmlDsl
2
- VERSION = "0.3.2"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -58,7 +58,9 @@ module XmlDsl
58
58
  end
59
59
 
60
60
  def root_path
61
- @root_path.join('/').prepend('//')
61
+ rp = @root_path.join('/')
62
+ rp.prepend('//') if @root_path.map {|part| part.is_a? Symbol}.reduce(true, :&)
63
+ rp
62
64
  end
63
65
  end
64
66
  end
@@ -18,7 +18,7 @@
18
18
  <area type="kitchen">10</area>
19
19
  </areas>
20
20
  </offer>
21
- <offer>
21
+ <offer type="uniq">
22
22
  <id>703102</id>
23
23
  <distance>25</distance>
24
24
  <house>7</house>
@@ -133,4 +133,57 @@ describe 'Example Xml Mapper Spec' do
133
133
  expect(@parser.iterate(acc:[]).length).to eql(1)
134
134
  end
135
135
  end
136
+
137
+ context 'Output parser and arbitrary class' do
138
+ before(:each) do
139
+ arbitrary_class = Class.new
140
+ arbitrary_class.class_eval do
141
+ attr_accessor :id, :minutes, :magick
142
+
143
+ def very_important_method
144
+ puts self.id
145
+ end
146
+ end
147
+ clazz = Class.new
148
+ @external_obj = double
149
+ clazz.class_eval do
150
+ attr_accessor :xml, :external
151
+ def initialize(xml, external)
152
+ self.xml = xml
153
+ self.external = external
154
+ end
155
+
156
+ define_xml_parser arbitrary_class, :root, "offer[type=uniq]" do
157
+ field :id, :id, matcher: :to_i
158
+ field :minutes, :distance, matcher: :to_i
159
+ end
160
+ end
161
+ @parser = clazz.new some_xml, @external_obj
162
+ end
163
+
164
+ it 'calls on iterate' do
165
+ d = double
166
+ expect(d).to receive(:fire).once
167
+ @parser.iterate do |i|
168
+ d.fire i
169
+ end
170
+ end
171
+
172
+ it 'appends instances to a list provided' do
173
+ list = []
174
+ @parser.iterate acc: list
175
+ expect(list.length).to eql(1)
176
+ end
177
+
178
+ it 'instances are having attributes set' do
179
+ list = []
180
+ @parser.iterate acc: list
181
+ expect(list.last.id).to eql(703102)
182
+ end
183
+
184
+ it 'calls after_method if provided' do
185
+ expect_any_instance_of(@parser.instance_variable_get(:@_xml_target_class)).to receive(:very_important_method).at_least(:once)
186
+ @parser.iterate after_method: :very_important_method
187
+ end
188
+ end
136
189
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladislav Bogomolov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-20 00:00:00.000000000 Z
11
+ date: 2014-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri