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 +4 -4
- data/HISTORY.md +4 -0
- data/README.md +4 -1
- data/lib/sax-machine/sax_document.rb +15 -6
- data/lib/sax-machine/version.rb +1 -1
- data/spec/sax-machine/sax_document_spec.rb +35 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d70f53b72ae5d965a51b002434716f68b7d6dd80
|
4
|
+
data.tar.gz: a96da94d49a82ace8f74778aae5fe7b1e7ff2067
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 618645fdf96799bf57df46b97c1e0244a933137085f1162c72735822b79a38c7b8c4fe7e3e73c7aba95611f36bc84bb91199af3e294faa924c18edc5b7fe0228
|
7
|
+
data.tar.gz: 7c0700b3c34bcd6812294cc67dc3571a35f4b4957a9aa2cef485aa15ab56292041ec1df056ebe861a5410cdcdb666041d85167aa00da43a45e92e0eb25bfbe6e
|
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -80,7 +80,10 @@ end
|
|
80
80
|
|
81
81
|
class Atom
|
82
82
|
include SAXMachine
|
83
|
-
|
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
|
-
|
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
|
data/lib/sax-machine/version.rb
CHANGED
@@ -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.
|
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-
|
14
|
+
date: 2014-12-23 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rspec
|