xml_mapper 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.5.2
data/lib/xml_mapper.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "nokogiri"
2
2
  require "xml_mapper_hash"
3
+ require "parsedate"
4
+ require "date"
3
5
 
4
6
  class XmlMapper
5
7
  attr_accessor :mappings, :after_map_block, :within_xpath
@@ -206,4 +208,16 @@ class XmlMapper
206
208
  def string_to_boolean(value)
207
209
  MAPPINGS[value.to_s.downcase]
208
210
  end
211
+
212
+ def parse_duration(string)
213
+ return string.to_i if string.match(/^\d+$/)
214
+ string = "00:#{string}" if string.match(/^(\d+):(\d+)$/)
215
+ string = string.to_s.gsub(/PT(\d+M.*)/,"PT0H\\1") # insert 0H into PT3M12S, for example: PT0H3M12S
216
+ year, month, day, hour, minute, second = ParseDate.parsedate(string)
217
+ return (hour.to_i * 1 * 3600 + minute.to_i * 1 * 60 + second.to_i).to_i
218
+ end
219
+
220
+ def parse_date(text)
221
+ text.to_s.strip.length > 0 ? Date.parse(text.to_s.strip) : nil
222
+ end
209
223
  end
@@ -20,5 +20,18 @@ class XmlMapper
20
20
  def hashes_from_into_keys(into_keys)
21
21
  [into_keys].flatten.map { |key| self[key] }.flatten.compact
22
22
  end
23
+
24
+ def strip_attributes!(hash)
25
+ hash.each do |key, value|
26
+ if value.is_a?(Hash)
27
+ strip_attributes!(value)
28
+ elsif value.is_a?(Array)
29
+ value.map { |array_value| strip_attributes!(array_value) }
30
+ elsif value.respond_to?(:strip!)
31
+ value.strip!
32
+ hash[key] = nil if value.length == 0
33
+ end
34
+ end
35
+ end
23
36
  end
24
37
  end
@@ -69,4 +69,36 @@ describe XmlMapper::XmlMapperHash do
69
69
  }
70
70
  end
71
71
  end
72
+
73
+ describe "#strip_attributes!" do
74
+ it "strips strings in simple hashes" do
75
+ hash = XmlMapper::XmlMapperHash.new.merge(:artist_name => " Test ")
76
+ hash.strip_attributes!(hash)
77
+ hash.should == { :artist_name => "Test" }
78
+ end
79
+
80
+ it "sets blank attributes to nil" do
81
+ hash = XmlMapper::XmlMapperHash.new.merge(:artist_name => " ")
82
+ hash.strip_attributes!(hash)
83
+ hash.should == { :artist_name => nil }
84
+ end
85
+
86
+ it "does not change nil values" do
87
+ hash = XmlMapper::XmlMapperHash.new.merge(:artist_name => nil)
88
+ hash.strip_attributes!(hash)
89
+ hash.should == { :artist_name => nil }
90
+ end
91
+
92
+ it "does strip nested hashes" do
93
+ hash = XmlMapper::XmlMapperHash.new.merge(:meta => { :title => " title " })
94
+ hash.strip_attributes!(hash)
95
+ hash.should == { :meta => { :title => "title" } }
96
+ end
97
+
98
+ it "does strip values in nested arrays" do
99
+ hash = XmlMapper::XmlMapperHash.new.merge(:tracks => [ { :title => " title 1 " }, { :title => " title 2 " } ])
100
+ hash.strip_attributes!(hash)
101
+ hash.should == { :tracks => [ { :title => "title 1" }, { :title => "title 2" } ] }
102
+ end
103
+ end
72
104
  end
@@ -15,6 +15,38 @@ describe "XmlMapper" do
15
15
  @mapper = XmlMapper.new
16
16
  end
17
17
 
18
+ describe "#parse_duration" do
19
+ it "convert human readable strings to lenghs" do
20
+ XmlMapper.new.parse_duration("00:01").should == 1
21
+ XmlMapper.new.parse_duration("00:00:01").should == 1
22
+ XmlMapper.new.parse_duration("00:01:01").should == 61
23
+ XmlMapper.new.parse_duration("01:01:01").should == 3661
24
+ end
25
+
26
+ it "should convert iso string to lengths" do
27
+ XmlMapper.new.parse_duration("PT0H5M54S").should == 354
28
+ XmlMapper.new.parse_duration("PT3M12S").should == 192
29
+ end
30
+
31
+ it "should not convert integer values" do
32
+ XmlMapper.new.parse_duration("60").should == 60
33
+ end
34
+ end
35
+
36
+ describe "#parse_date" do
37
+ it "returns nil when text is blank" do
38
+ XmlMapper.new.parse_date(" ").should be_nil
39
+ end
40
+
41
+ it "returns nil when text is nil" do
42
+ XmlMapper.new.parse_date(nil).should be_nil
43
+ end
44
+
45
+ it "parses dates correctly" do
46
+ XmlMapper.new.parse_date("2010-09-01").should == Date.new(2010, 9, 1)
47
+ end
48
+ end
49
+
18
50
  describe "#add_mapping" do
19
51
  it "adds the correct mappings when only one symbol given" do
20
52
  @mapper.add_mapping(:text, :title)
data/xml_mapper.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{xml_mapper}
8
- s.version = "0.5.1"
8
+ s.version = "0.5.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Schwab"]
12
- s.date = %q{2011-04-07}
12
+ s.date = %q{2011-04-08}
13
13
  s.description = %q{Declarative XML to Ruby mapping}
14
14
  s.email = %q{tobias.schwab@dynport.de}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml_mapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 1
10
- version: 0.5.1
9
+ - 2
10
+ version: 0.5.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-07 00:00:00 +02:00
18
+ date: 2011-04-08 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency