sax-machine 1.1.1 → 1.2.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: 7e1a19d1fe1c96e358244af5eddf73e658c43041
4
- data.tar.gz: ab4ab21c950788a7ca9a05e9daa678f929bd42cd
3
+ metadata.gz: d70f53b72ae5d965a51b002434716f68b7d6dd80
4
+ data.tar.gz: a96da94d49a82ace8f74778aae5fe7b1e7ff2067
5
5
  SHA512:
6
- metadata.gz: edbbbf1cd1dd4c9cafe1f4172601b63f4fca893175027c0fc09f8c24f92c3857af0b9a924fed787b8e3410b5192ae8d20818704ee1a42f83d093be7f73f03404
7
- data.tar.gz: 91c2272b5be0c789d633767f0619b7ef2201ec1d8c2af8ae7e0eb36e199d472d30853b9578ff364af5722bee96dadad72aa720b1f5b7c89527cc6b0bd46d1857
6
+ metadata.gz: 618645fdf96799bf57df46b97c1e0244a933137085f1162c72735822b79a38c7b8c4fe7e3e73c7aba95611f36bc84bb91199af3e294faa924c18edc5b7fe0228
7
+ data.tar.gz: 7c0700b3c34bcd6812294cc67dc3571a35f4b4957a9aa2cef485aa15ab56292041ec1df056ebe861a5410cdcdb666041d85167aa00da43a45e92e0eb25bfbe6e
data/HISTORY.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # HEAD
2
2
 
3
+ # 1.2.0
4
+
5
+ * Add support for blocks as value modifiers [[#61](https://github.com/pauldix/sax-machine/pull/61)]
6
+
3
7
  # 1.1.1
4
8
 
5
9
  * Fix Nokogiri autoloading [[#60](https://github.com/pauldix/sax-machine/pull/60)]
data/README.md CHANGED
@@ -80,7 +80,10 @@ end
80
80
 
81
81
  class Atom
82
82
  include SAXMachine
83
- element :title
83
+ # Use block to modify the returned value
84
+ element :title do |title|
85
+ title.strip
86
+ end
84
87
  # The :with argument means that you only match a link tag
85
88
  # that has an attribute of type: "text/html"
86
89
  element :link, value: :href, as: :url, with: {
@@ -39,16 +39,16 @@ module SAXMachine
39
39
  new.parse(*args)
40
40
  end
41
41
 
42
- def element(name, options = {})
42
+ def element(name, options = {}, &block)
43
43
  real_name = (options[:as] ||= name).to_s
44
44
  sax_config.add_top_level_element(name, options)
45
- create_attr(real_name)
45
+ create_attr(real_name, &block)
46
46
  end
47
47
 
48
- def attribute(name, options = {})
48
+ def attribute(name, options = {}, &block)
49
49
  real_name = (options[:as] ||= name).to_s
50
50
  sax_config.add_top_level_attribute(self.class.to_s, options.merge(name: name))
51
- create_attr(real_name)
51
+ create_attr(real_name, &block)
52
52
  end
53
53
 
54
54
  def value(name, options = {})
@@ -115,9 +115,18 @@ module SAXMachine
115
115
  # we only want to insert the getter and setter if they haven't defined it from elsewhere.
116
116
  # this is how we allow custom parsing behavior. So you could define the setter
117
117
  # and have it parse the string into a date or whatever.
118
- def create_attr(real_name)
118
+ def create_attr(real_name, &block)
119
119
  attr_reader(real_name) unless method_defined?(real_name)
120
- attr_writer(real_name) unless method_defined?("#{real_name}=")
120
+
121
+ if !method_defined?("#{real_name}=")
122
+ if block_given?
123
+ define_method("#{real_name}=") do |value|
124
+ instance_variable_set("@#{real_name}", (yield value))
125
+ end
126
+ else
127
+ attr_writer(real_name)
128
+ end
129
+ end
121
130
  end
122
131
  end
123
132
  end
@@ -1,3 +1,3 @@
1
1
  module SAXMachine
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -218,6 +218,41 @@ describe "SAXMachine" do
218
218
  expect(@klass.required?(:date)).to be_truthy
219
219
  end
220
220
  end
221
+
222
+ describe "the block" do
223
+ before do
224
+ @klass = Class.new do
225
+ include SAXMachine
226
+
227
+ element :title do |title|
228
+ "#{title}!!!"
229
+ end
230
+
231
+ attribute :id do |id|
232
+ id.to_i
233
+ end
234
+
235
+ element :link, value: :foo do |link|
236
+ link.downcase
237
+ end
238
+ end
239
+ end
240
+
241
+ it "uses block for element" do
242
+ document = @klass.parse("<title>SAX</title>")
243
+ expect(document.title).to eq("SAX!!!")
244
+ end
245
+
246
+ it 'uses block for attribute' do
247
+ document = @klass.parse("<title id='345'>SAX</title>")
248
+ expect(document.id).to eq(345)
249
+ end
250
+
251
+ it "uses block for attribute value" do
252
+ document = @klass.parse("<link foo='tEst'>hello</link>")
253
+ expect(document.link).to eq("test")
254
+ end
255
+ end
221
256
  end
222
257
 
223
258
  describe "when parsing multiple elements" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-11-20 00:00:00.000000000 Z
14
+ date: 2014-12-23 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec