relief 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,8 +12,9 @@ begin
12
12
  gem.email = "tyler@tylerhunt.com"
13
13
  gem.homepage = "http://github.com/tylerhunt/relief"
14
14
  gem.authors = ["Tyler Hunt"]
15
+ gem.rubyforge_project = 'relief'
15
16
 
16
- gem.add_dependency 'nokogiri', '~> 1.2.3'
17
+ gem.add_dependency 'nokogiri', '~> 1.3.3'
17
18
 
18
19
  gem.add_development_dependency('jeweler', '~> 0.11.0')
19
20
  end
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 0
4
- :patch: 4
4
+ :patch: 5
@@ -1,3 +1,5 @@
1
+ require 'time'
2
+
1
3
  module Relief
2
4
  class Element
3
5
  attr_reader :name, :options, :children
@@ -24,9 +26,13 @@ module Relief
24
26
  else
25
27
  value = target.to_s
26
28
 
27
- if type == Integer then value.to_i
29
+ if type.nil? then value
30
+ elsif value.empty? then nil
31
+ elsif type == Integer then value.to_i
28
32
  elsif type == Float then value.to_f
29
33
  elsif type == Date then Date.parse(value)
34
+ elsif type == Time then Time.parse(value)
35
+ elsif type == DateTime then DateTime.parse(value)
30
36
  elsif type.is_a?(Parser) then type.parse(document)
31
37
  else value
32
38
  end
@@ -65,7 +71,7 @@ module Relief
65
71
  else
66
72
  attribute = @options[:attribute]
67
73
  attribute = name if attribute == true
68
- !attribute ? "#{name}/text()" : "@#{attribute}"
74
+ !attribute ? "#{name}/text()" : ".//@#{attribute}"
69
75
  end
70
76
  end
71
77
  end
@@ -1,7 +1,10 @@
1
- gem 'nokogiri', '~> 1.2.3'
1
+ gem 'nokogiri', '~> 1.3.3'
2
2
  require 'nokogiri'
3
3
 
4
4
  module Relief
5
+ class Error < StandardError ; end
6
+ class ParseError < Error ; end
7
+
5
8
  class Parser
6
9
  attr_reader :root
7
10
 
@@ -14,6 +17,8 @@ module Relief
14
17
  document = Nokogiri::XML(document.to_s)
15
18
  end
16
19
 
20
+ raise ParseError if document.root.nil?
21
+
17
22
  @root.parse(document)
18
23
  end
19
24
  end
@@ -1,6 +1,17 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
3
  describe Relief::Parser do
4
+ it "raises a ParseError if the document can't be parsed" do
5
+ parser = Relief::Parser.new(:photo) do
6
+ element :name
7
+ element :url
8
+ end
9
+
10
+ lambda {
11
+ parser.parse('')
12
+ }.should raise_error(Relief::ParseError)
13
+ end
14
+
4
15
  it "parses elements" do
5
16
  parser = Relief::Parser.new(:photo) do
6
17
  element :name
@@ -50,6 +61,8 @@ describe Relief::Parser do
50
61
 
51
62
  it "parses attributes" do
52
63
  parser = Relief::Parser.new(:photos) do
64
+ attribute :status
65
+
53
66
  elements :photo do
54
67
  attribute :name
55
68
  attribute :url
@@ -58,13 +71,14 @@ describe Relief::Parser do
58
71
 
59
72
  photos = parser.parse(<<-XML)
60
73
  <?xml version="1.0" encoding="UTF-8"?>
61
- <photos>
74
+ <photos status="fine">
62
75
  <photo name="Cucumbers" url="/photos/cucumbers.jpg" />
63
76
  <photo name="Lemons" url="/photos/lemons.jpg" />
64
77
  </photos>
65
78
  XML
66
79
 
67
80
  photos.should == {
81
+ :status => 'fine',
68
82
  :photo => [
69
83
  { :name => 'Cucumbers', :url => '/photos/cucumbers.jpg' },
70
84
  { :name => 'Lemons', :url => '/photos/lemons.jpg' }
@@ -128,7 +142,9 @@ describe Relief::Parser do
128
142
  parser = Relief::Parser.new(:photo) do
129
143
  element :id, :type => Integer
130
144
  element :rating, :type => Float
131
- element :published, :type => Date
145
+ element :taken, :type => Date
146
+ element :modified, :type => Time
147
+ element :published, :type => DateTime
132
148
  end
133
149
 
134
150
  photo = parser.parse(<<-XML)
@@ -136,14 +152,18 @@ describe Relief::Parser do
136
152
  <photo>
137
153
  <id>86634</id>
138
154
  <rating>3.5</rating>
139
- <published>2009-05-08T18:23:26-07:00</url>
155
+ <taken>2009-05-06</taken>
156
+ <modified>2009-05-08T18:23:48-07:00</modified>
157
+ <published>2009-05-08T18:23:26-07:00</published>
140
158
  </photo>
141
159
  XML
142
160
 
143
161
  photo.should == {
144
162
  :id => 86634,
145
163
  :rating => 3.5,
146
- :published => Date.parse('2009-05-08T18:23:26-07:00')
164
+ :taken => Date.new(2009, 5, 6),
165
+ :modified => Time.parse('2009-05-08T18:23:48-07:00'),
166
+ :published => DateTime.new(2009, 5, 8, 18, 23, 26, Date.time_to_day_fraction(-7, 0, 0))
147
167
  }
148
168
  end
149
169
 
@@ -174,4 +194,33 @@ describe Relief::Parser do
174
194
  }
175
195
  }
176
196
  end
197
+
198
+ it "doesn't type cast elements with empty values" do
199
+ parser = Relief::Parser.new(:photo) do
200
+ element :name
201
+ element :id, :type => Integer
202
+ element :rating, :type => Float
203
+ element :taken, :type => Date
204
+ element :published, :type => DateTime
205
+ end
206
+
207
+ photo = parser.parse(<<-XML)
208
+ <?xml version="1.0" encoding="UTF-8"?>
209
+ <photo>
210
+ <name></name>
211
+ <id></id>
212
+ <rating></rating>
213
+ <taken></taken>
214
+ <published></published>
215
+ </photo>
216
+ XML
217
+
218
+ photo.should == {
219
+ :name => '',
220
+ :id => nil,
221
+ :rating => nil,
222
+ :taken => nil,
223
+ :published => nil
224
+ }
225
+ end
177
226
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relief
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Hunt
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-12 00:00:00 -04:00
12
+ date: 2009-08-31 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ~>
22
22
  - !ruby/object:Gem::Version
23
- version: 1.2.3
23
+ version: 1.3.3
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jeweler
@@ -53,6 +53,8 @@ files:
53
53
  - spec/spec_helper.rb
54
54
  has_rdoc: true
55
55
  homepage: http://github.com/tylerhunt/relief
56
+ licenses: []
57
+
56
58
  post_install_message:
57
59
  rdoc_options:
58
60
  - --charset=UTF-8
@@ -72,10 +74,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
74
  version:
73
75
  requirements: []
74
76
 
75
- rubyforge_project:
76
- rubygems_version: 1.3.1
77
+ rubyforge_project: relief
78
+ rubygems_version: 1.3.5
77
79
  signing_key:
78
- specification_version: 2
80
+ specification_version: 3
79
81
  summary: An XML to hash Ruby parser DSL.
80
82
  test_files:
81
83
  - spec/parser_spec.rb