sax-machine 0.0.16 → 0.2.0.rc1

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.
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class A
4
+
5
+ SAXMachine.configure(A) do |c|
6
+ c.element :title
7
+ end
8
+
9
+ end
10
+
11
+ class B < A
12
+
13
+ SAXMachine.configure(B) do |c|
14
+ c.element :b
15
+ end
16
+
17
+ end
18
+
19
+ class C < B
20
+
21
+ SAXMachine.configure(C) do |c|
22
+ c.element :c
23
+ end
24
+
25
+ end
26
+
27
+ describe "SAXMachine configure" do
28
+ before do
29
+ xml = "<top><title>Test</title><b>Matched!</b><c>And Again</c></top>"
30
+ @a = A.new
31
+ @a.parse xml
32
+ @b = B.new
33
+ @b.parse xml
34
+ @c = C.new
35
+ @c.parse xml
36
+ end
37
+ it { @a.should be_a(A) }
38
+ it { @a.should_not be_a(B) }
39
+ it { @a.should be_a(SAXMachine) }
40
+ it { @a.title.should == "Test" }
41
+ it { @b.should be_a(A) }
42
+ it { @b.should be_a(B) }
43
+ it { @b.should be_a(SAXMachine) }
44
+ it { @b.title.should == "Test" }
45
+ it { @b.b.should == "Matched!" }
46
+ it { @c.should be_a(A) }
47
+ it { @c.should be_a(B) }
48
+ it { @c.should be_a(C) }
49
+ it { @c.should be_a(SAXMachine) }
50
+ it { @c.title.should == "Test" }
51
+ it { @c.b.should == "Matched!" }
52
+ it { @c.c.should == "And Again" }
53
+ end
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class A
4
+ include SAXMachine
5
+ element :title
6
+ end
7
+
8
+ class B < A
9
+ element :b
10
+ end
11
+
12
+ class C < B
13
+ element :c
14
+ end
15
+
16
+ describe "SAXMachine inheritance" do
17
+ before do
18
+ xml = "<top><title>Test</title><b>Matched!</b><c>And Again</c></top>"
19
+ @a = A.new
20
+ @a.parse xml
21
+ @b = B.new
22
+ @b.parse xml
23
+ @c = C.new
24
+ @c.parse xml
25
+ end
26
+ it { @a.should be_a(A) }
27
+ it { @a.should_not be_a(B) }
28
+ it { @a.should be_a(SAXMachine) }
29
+ it { @a.title.should == "Test" }
30
+ it { @b.should be_a(A) }
31
+ it { @b.should be_a(B) }
32
+ it { @b.should be_a(SAXMachine) }
33
+ it { @b.title.should == "Test" }
34
+ it { @b.b.should == "Matched!" }
35
+ it { @c.should be_a(A) }
36
+ it { @c.should be_a(B) }
37
+ it { @c.should be_a(C) }
38
+ it { @c.should be_a(SAXMachine) }
39
+ it { @c.title.should == "Test" }
40
+ it { @c.b.should == "Matched!" }
41
+ it { @c.c.should == "And Again" }
42
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe "SAXMachine" do
4
4
  describe "element" do
@@ -20,11 +20,26 @@ describe "SAXMachine" do
20
20
  @klass.column_names.should =~ [:title]
21
21
  end
22
22
 
23
+ it "should not overwrite the getter is there is already one present" do
24
+ @klass = Class.new do
25
+ def title
26
+ "#{@title} ***"
27
+ end
28
+
29
+ include SAXMachine
30
+ element :title
31
+ end
32
+ document = @klass.new
33
+ document.title = "Title"
34
+ document.title.should == "Title ***"
35
+ end
36
+
23
37
  it "should not overwrite the setter if there is already one present" do
24
38
  @klass = Class.new do
25
39
  def title=(val)
26
40
  @title = "#{val} **"
27
41
  end
42
+
28
43
  include SAXMachine
29
44
  element :title
30
45
  end
@@ -44,6 +59,43 @@ describe "SAXMachine" do
44
59
  it "should be available" do
45
60
  @klass.data_class(:date).should == DateTime
46
61
  end
62
+
63
+ it "should handle an integer class" do
64
+ @klass = Class.new do
65
+ include SAXMachine
66
+ element :number, :class => Integer
67
+ end
68
+ document = @klass.parse("<number>5</number>")
69
+ document.number.should == 5
70
+ end
71
+
72
+ it "should handle an float class" do
73
+ @klass = Class.new do
74
+ include SAXMachine
75
+ element :number, :class => Float
76
+ end
77
+ document = @klass.parse("<number>5.5</number>")
78
+ document.number.should == 5.5
79
+ end
80
+
81
+ it "should handle an string class" do
82
+ @klass = Class.new do
83
+ include SAXMachine
84
+ element :number, :class => String
85
+ end
86
+ document = @klass.parse("<number>5.5</number>")
87
+ document.number.should == "5.5"
88
+ end
89
+
90
+ it "should handle a time class" do
91
+ @klass = Class.new do
92
+ include SAXMachine
93
+ element :time, :class => Time
94
+ end
95
+ document = @klass.parse("<time>1994-02-04T06:20:00Z</time>")
96
+ document.time.should == Time.utc(1994, 2, 4, 6, 20, 0, 0)
97
+ end
98
+
47
99
  end
48
100
  describe "the required attribute" do
49
101
  it "should be available" do
@@ -74,6 +126,16 @@ describe "SAXMachine" do
74
126
  document.title.should == "My Title"
75
127
  end
76
128
 
129
+ if RUBY_VERSION >= "1.9.0"
130
+ it "should keep the document encoding for elements" do
131
+ data = "<title>My Title</title>"
132
+ data.encode!("utf-8")
133
+
134
+ document = @klass.parse(data)
135
+ document.title.encoding.should == data.encoding
136
+ end
137
+ end
138
+
77
139
  it "should save cdata into an accessor" do
78
140
  document = @klass.parse("<title><![CDATA[A Title]]></title>")
79
141
  document.title.should == "A Title"
@@ -105,6 +167,36 @@ describe "SAXMachine" do
105
167
  document.name.should == "Paul"
106
168
  document.title.should == "My Title"
107
169
  end
170
+
171
+
172
+ it "should not overwrite the getter is there is already one present" do
173
+ @klass = Class.new do
174
+ def items
175
+ []
176
+ end
177
+
178
+ include SAXMachine
179
+ elements :items
180
+ end
181
+ document = @klass.new
182
+ document.items = [1, 2, 3, 4]
183
+ document.items.should == []
184
+ end
185
+
186
+ it "should not overwrite the setter if there is already one present" do
187
+ @klass = Class.new do
188
+ def items=(val)
189
+ @items = [1, *val]
190
+ end
191
+
192
+ include SAXMachine
193
+ elements :items
194
+ end
195
+ document = @klass.new
196
+ document.items = [2, 3]
197
+ document.items.should == [1, 2, 3]
198
+ end
199
+
108
200
  end
109
201
 
110
202
  describe "when using options for parsing elements" do
@@ -340,7 +432,7 @@ describe "SAXMachine" do
340
432
  elements :item, :as => :items, :with => {:type => 'Foo'}, :class => Foo
341
433
  end
342
434
  end
343
-
435
+
344
436
  it "should cast into the correct class" do
345
437
  document = @klass.parse("<items><item type=\"Bar\"><title>Bar title</title></item><item type=\"Foo\"><title>Foo title</title></item></items>")
346
438
  document.items.size.should == 2
@@ -382,6 +474,23 @@ describe "SAXMachine" do
382
474
  document.entries.first.title.should == "correct title"
383
475
  end
384
476
 
477
+ it "should parse elements, and make attributes and inner text available" do
478
+ class Related
479
+ include SAXMachine
480
+ element 'related', :as=>:item
481
+ element 'related', :as=>:attr, :value=>'attr'
482
+ end
483
+ class Foo
484
+ elements 'related', :as=>'items', :class=>Related
485
+ end
486
+
487
+ doc = Foo.parse(%{<xml><collection><related attr='baz'>something</related><related>somethingelse</related></collection></xml>})
488
+ doc.items.first.should_not be_nil
489
+ doc.items.size.should == 2
490
+ doc.items.first.item.should == 'something'
491
+ doc.items.last.item.should == 'somethingelse'
492
+ end
493
+
385
494
  it "should parse out an attribute value from the tag that starts the collection" do
386
495
  class Foo
387
496
  element :entry, :value => :href, :as => :url
@@ -392,7 +501,18 @@ describe "SAXMachine" do
392
501
  document.entries.first.url.should == "http://pauldix.net"
393
502
  end
394
503
  end
395
-
504
+ end
505
+
506
+ describe "when dealing with element names containing dashes" do
507
+ it 'should automatically convert dashes to underscores' do
508
+ class Dashes
509
+ include SAXMachine
510
+ element :dashed_element
511
+ end
512
+
513
+ parsed = Dashes.parse('<dashed-element>Text</dashed-element>')
514
+ parsed.dashed_element.should eq "Text"
515
+ end
396
516
  end
397
517
 
398
518
  describe "full example" do
@@ -437,13 +557,15 @@ describe "SAXMachine" do
437
557
  </category>
438
558
  </categories>
439
559
  ]
440
- class CategoryCollection; end
560
+ class CategoryCollection;
561
+ end
441
562
  class Category
442
563
  include SAXMachine
443
564
  attr_accessor :id
444
565
  element :category, :value => :id, :as => :id
445
566
  element :title
446
567
  element :categories, :as => :collection, :class => CategoryCollection
568
+ ancestor :ancestor
447
569
  end
448
570
  class CategoryCollection
449
571
  include SAXMachine
@@ -455,6 +577,7 @@ describe "SAXMachine" do
455
577
  it "should parse the first category" do
456
578
  @collection.categories.first.id.should == "1"
457
579
  @collection.categories.first.title.should == "First"
580
+ @collection.categories.first.ancestor.should == @collection
458
581
  end
459
582
 
460
583
  it "should parse the nested category" do
@@ -524,4 +647,151 @@ describe "SAXMachine" do
524
647
  @item.title.should == "Hello"
525
648
  end
526
649
  end
650
+
651
+ describe "with config to pull multiple attributes" do
652
+ before do
653
+ @xml = %[
654
+ <item id="1">
655
+ <author name="John Doe" role="writer" />
656
+ </item>
657
+ ]
658
+ class AuthorElement
659
+ include SAXMachine
660
+ attribute :name
661
+ attribute :role
662
+ end
663
+ class ItemElement
664
+ include SAXMachine
665
+ element :author, :class => AuthorElement
666
+ end
667
+ @item = ItemElement.parse(@xml)
668
+ end
669
+
670
+ it 'should have the child element' do
671
+ @item.author.should_not be_nil
672
+ end
673
+
674
+ it 'should have the author name' do
675
+ @item.author.name.should == 'John Doe'
676
+ end
677
+
678
+ it 'should have the author role' do
679
+ @item.author.role.should == 'writer'
680
+ end
681
+ end
682
+
683
+ describe "with multiple elements and multiple attributes" do
684
+ before do
685
+ @xml = %[
686
+ <item id="1">
687
+ <author name="John Doe" role="writer" />
688
+ <author name="Jane Doe" role="artist" />
689
+ </item>
690
+ ]
691
+ class AuthorElement2
692
+ include SAXMachine
693
+ attribute :name
694
+ attribute :role
695
+ end
696
+ class ItemElement2
697
+ include SAXMachine
698
+ elements :author, :as => :authors, :class => AuthorElement2
699
+ end
700
+ @item = ItemElement2.parse(@xml)
701
+ end
702
+
703
+ it 'should have the child elements' do
704
+ @item.authors.should_not be_nil
705
+ @item.authors.count.should == 2
706
+ end
707
+
708
+ it 'should have the author names' do
709
+ @item.authors.first.name.should == 'John Doe'
710
+ @item.authors.last.name.should == 'Jane Doe'
711
+ end
712
+
713
+ it 'should have the author roles' do
714
+ @item.authors.first.role.should == 'writer'
715
+ @item.authors.last.role.should == 'artist'
716
+ end
717
+ end
718
+
719
+ describe "with mixed attributes and element values" do
720
+ before do
721
+ @xml = %[
722
+ <item id="1">
723
+ <author role="writer">John Doe</author>
724
+ </item>
725
+ ]
726
+ class AuthorElement3
727
+ include SAXMachine
728
+ value :name
729
+ attribute :role
730
+ end
731
+ class ItemElement3
732
+ include SAXMachine
733
+ element :author, :class => AuthorElement3
734
+ end
735
+ @item = ItemElement3.parse(@xml)
736
+ end
737
+
738
+ it 'should have the child elements' do
739
+ @item.author.should_not be_nil
740
+ end
741
+
742
+ it 'should have the author names' do
743
+ @item.author.name.should == 'John Doe'
744
+ end
745
+
746
+ it 'should have the author roles' do
747
+ @item.author.role.should == 'writer'
748
+ end
749
+ end
750
+
751
+ describe "with multiple mixed attributes and element values" do
752
+ before do
753
+ @xml = %[
754
+ <item id="1">
755
+ <title>sweet</title>
756
+ <author role="writer">John Doe</author>
757
+ <author role="artist">Jane Doe</author>
758
+ </item>
759
+ ]
760
+ class AuthorElement4
761
+ include SAXMachine
762
+ value :name
763
+ attribute :role
764
+ end
765
+ class ItemElement4
766
+ include SAXMachine
767
+ element :title
768
+ elements :author, :as => :authors, :class => AuthorElement4
769
+
770
+ def title=(blah)
771
+ #raise 'woo'
772
+ @title = blah
773
+ end
774
+ end
775
+ @item = ItemElement4.parse(@xml)
776
+ end
777
+
778
+ it 'should have the title' do
779
+ @item.title.should == 'sweet'
780
+ end
781
+
782
+ it 'should have the child elements' do
783
+ @item.authors.should_not be_nil
784
+ @item.authors.count.should == 2
785
+ end
786
+
787
+ it 'should have the author names' do
788
+ @item.authors.first.name.should == 'John Doe'
789
+ @item.authors.last.name.should == 'Jane Doe'
790
+ end
791
+
792
+ it 'should have the author roles' do
793
+ @item.authors.first.role.should == 'writer'
794
+ @item.authors.last.role.should == 'artist'
795
+ end
796
+ end
527
797
  end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,15 @@
1
- require 'date'
2
-
3
- # gem install redgreen for colored test output
4
- begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
5
-
6
- path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
7
- $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
8
-
9
- require "lib/sax-machine"
10
-
11
- # Spec::Runner.configure do |config|
12
- # end
1
+ begin
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/spec/"
5
+ end
6
+ rescue LoadError
7
+ end
8
+
9
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/sax-machine')
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+ end
metadata CHANGED
@@ -1,94 +1,117 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sax-machine
3
- version: !ruby/object:Gem::Version
4
- hash: 63
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 16
10
- version: 0.0.16
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0.rc1
5
+ prerelease: 6
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Paul Dix
9
+ - Julien Kirch
10
+ - Ezekiel Templin
14
11
  autorequire:
15
12
  bindir: bin
16
13
  cert_chain: []
17
-
18
- date: 2009-01-13 00:00:00 -05:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
14
+ date: 2012-06-04 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
22
17
  name: nokogiri
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirement: !ruby/object:Gem::Requirement
25
19
  none: false
26
- requirements:
27
- - - ">"
28
- - !ruby/object:Gem::Version
29
- hash: 31
30
- segments:
31
- - 0
32
- - 0
33
- - 0
34
- version: 0.0.0
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.5.2
35
24
  type: :runtime
36
- version_requirements: *id001
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: 1.5.2
32
+ - !ruby/object:Gem::Dependency
33
+ name: rspec
34
+ requirement: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: 2.10.0
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 2.10.0
37
48
  description:
38
49
  email: paul@pauldix.net
39
50
  executables: []
40
-
41
51
  extensions: []
42
-
43
52
  extra_rdoc_files: []
44
-
45
- files:
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - .travis.yml
57
+ - Gemfile
58
+ - Guardfile
59
+ - HISTORY.md
60
+ - README.md
61
+ - Rakefile
46
62
  - lib/sax-machine.rb
47
- - lib/sax-machine/sax_config.rb
63
+ - lib/sax-machine/sax_ancestor_config.rb
64
+ - lib/sax-machine/sax_attribute_config.rb
48
65
  - lib/sax-machine/sax_collection_config.rb
49
- - lib/sax-machine/sax_element_config.rb
66
+ - lib/sax-machine/sax_config.rb
67
+ - lib/sax-machine/sax_configure.rb
50
68
  - lib/sax-machine/sax_document.rb
69
+ - lib/sax-machine/sax_element_config.rb
70
+ - lib/sax-machine/sax_element_value_config.rb
51
71
  - lib/sax-machine/sax_handler.rb
52
- - README.textile
53
- - Rakefile
54
- - .rspec
55
- - Gemfile
56
- - Gemfile.lock
57
- - spec/spec_helper.rb
72
+ - lib/sax-machine/version.rb
73
+ - sax-machine.gemspec
74
+ - spec/benchmarks/amazon.xml
75
+ - spec/benchmarks/benchmark.rb
76
+ - spec/benchmarks/public_timeline.xml
77
+ - spec/sax-machine/atom.xml
78
+ - spec/sax-machine/configure_sax_machine_spec.rb
79
+ - spec/sax-machine/include_sax_machine_spec.rb
58
80
  - spec/sax-machine/sax_document_spec.rb
59
- has_rdoc: true
81
+ - spec/spec_helper.rb
60
82
  homepage: http://github.com/pauldix/sax-machine
61
83
  licenses: []
62
-
63
84
  post_install_message:
64
85
  rdoc_options: []
65
-
66
- require_paths:
86
+ require_paths:
67
87
  - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
88
+ required_ruby_version: !ruby/object:Gem::Requirement
69
89
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
75
95
  - 0
76
- version: "0"
77
- required_rubygems_version: !ruby/object:Gem::Requirement
96
+ hash: 3574605312337356673
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
98
  none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
84
- - 0
85
- version: "0"
99
+ requirements:
100
+ - - ! '>'
101
+ - !ruby/object:Gem::Version
102
+ version: 1.3.1
86
103
  requirements: []
87
-
88
104
  rubyforge_project:
89
- rubygems_version: 1.3.7
105
+ rubygems_version: 1.8.24
90
106
  signing_key:
91
- specification_version: 2
107
+ specification_version: 3
92
108
  summary: Declarative SAX Parsing with Nokogiri
93
- test_files: []
94
-
109
+ test_files:
110
+ - spec/benchmarks/amazon.xml
111
+ - spec/benchmarks/benchmark.rb
112
+ - spec/benchmarks/public_timeline.xml
113
+ - spec/sax-machine/atom.xml
114
+ - spec/sax-machine/configure_sax_machine_spec.rb
115
+ - spec/sax-machine/include_sax_machine_spec.rb
116
+ - spec/sax-machine/sax_document_spec.rb
117
+ - spec/spec_helper.rb
data/Gemfile.lock DELETED
@@ -1,20 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- diff-lcs (1.1.2)
5
- nokogiri (1.4.4)
6
- rspec (2.4.0)
7
- rspec-core (~> 2.4.0)
8
- rspec-expectations (~> 2.4.0)
9
- rspec-mocks (~> 2.4.0)
10
- rspec-core (2.4.0)
11
- rspec-expectations (2.4.0)
12
- diff-lcs (~> 1.1.2)
13
- rspec-mocks (2.4.0)
14
-
15
- PLATFORMS
16
- ruby
17
-
18
- DEPENDENCIES
19
- nokogiri (>= 1.4.4)
20
- rspec (>= 2.4.0)