pauldix-sax-machine 0.0.5 → 0.0.6

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/README.textile ADDED
@@ -0,0 +1,78 @@
1
+ h1. SAX Machine
2
+
3
+ "http://github.com/pauldix/sax-machine/wikis":http://github.com/pauldix/sax-machine/wikis
4
+
5
+ "http://github.com/pauldix/sax-machine/tree/master":http://github.com/pauldix/sax-machine/tree/master
6
+
7
+ h2. Description
8
+
9
+ A declarative SAX parsing library backed by Nokogiri
10
+
11
+ h2. Usage
12
+
13
+ <pre>
14
+ require 'sax-machine'
15
+
16
+ # Class for parsing an atom entry out of a feedburner atom feed
17
+ class AtomEntry
18
+ include SAXMachine
19
+ element :title
20
+ # the :as argument makes this available through atom_entry.author instead of .name
21
+ element :name, :as => :author
22
+ element "feedburner:origLink", :as => :url
23
+ element :summary
24
+ element :content
25
+ element :published
26
+ end
27
+
28
+ # Class for parsing Atom feeds
29
+ class Atom
30
+ include SAXMachine
31
+ element :title
32
+ # the :with argument means that you only match a link tag that has an attribute of :type => "text/html"
33
+ # the :value argument means that instead of setting the value to the text between the tag,
34
+ # it sets it to the attribute value of :href
35
+ element :link, :value => :href, :as => :url, :with => {:type => "text/html"}
36
+ element :link, :value => :href, :as => :feed_url, :with => {:type => "application/atom+xml"}
37
+ elements :entry, :as => :entries, :class => AtomEntry
38
+ end
39
+
40
+ # you can then parse like this
41
+ feed = Atom.parse(xml_text)
42
+ # then you're ready to rock
43
+ feed.title # => whatever the title of the blog is
44
+ feed.url # => the main url of the blog
45
+ feed.feed_url # => goes to the feedburner feed
46
+
47
+ feed.entries.first.title # => title of the first entry
48
+ feed.entries.first.author # => the author of the first entry
49
+ feed.entries.first.url # => the permalink on the blog for this entry
50
+ # etc ...
51
+ </pre>
52
+
53
+ h2. LICENSE
54
+
55
+ (The MIT License)
56
+
57
+ Copyright (c) 2009:
58
+
59
+ "Paul Dix":http://pauldix.net
60
+
61
+ Permission is hereby granted, free of charge, to any person obtaining
62
+ a copy of this software and associated documentation files (the
63
+ 'Software'), to deal in the Software without restriction, including
64
+ without limitation the rights to use, copy, modify, merge, publish,
65
+ distribute, sublicense, and/or sell copies of the Software, and to
66
+ permit persons to whom the Software is furnished to do so, subject to
67
+ the following conditions:
68
+
69
+ The above copyright notice and this permission notice shall be
70
+ included in all copies or substantial portions of the Software.
71
+
72
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
73
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
75
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
76
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
77
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
78
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -22,7 +22,15 @@ module SAXMachine
22
22
  def element(name, options = {})
23
23
  options[:as] ||= name
24
24
  sax_config.add_top_level_element(name, options)
25
- attr_accessor options[:as]
25
+
26
+ # we only want to insert the setter if they haven't defined it from elsewhere.
27
+ # this is how we allow custom parsing behavior. So you could define the setter
28
+ # and have it parse the string into a date or whatever.
29
+ if instance_methods.include?("#{options[:as]}=")
30
+ attr_reader options[:as]
31
+ else
32
+ attr_accessor options[:as]
33
+ end
26
34
  end
27
35
 
28
36
  def elements(name, options = {})
data/lib/sax-machine.rb CHANGED
@@ -7,5 +7,5 @@ require "sax-machine/sax_handler"
7
7
  require "sax-machine/sax_config"
8
8
 
9
9
  module SAXMachine
10
- VERSION = "0.0.5"
10
+ VERSION = "0.0.6"
11
11
  end
@@ -16,6 +16,19 @@ describe "SAXMachine" do
16
16
  document.title.should == "Title"
17
17
  end
18
18
 
19
+ it "should not overwrite the setter if there is already one present" do
20
+ @klass = Class.new do
21
+ def title=(val)
22
+ @title = "#{val} **"
23
+ end
24
+ include SAXMachine
25
+ element :title
26
+ end
27
+ document = @klass.new
28
+ document.title = "Title"
29
+ document.title.should == "Title **"
30
+ end
31
+
19
32
  it "should not overwrite the accessor when the element is not present" do
20
33
  document = @klass.new
21
34
  document.title = "Title"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pauldix-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix
@@ -36,12 +36,12 @@ files:
36
36
  - lib/sax-machine/sax_element_config.rb
37
37
  - lib/sax-machine/sax_document.rb
38
38
  - lib/sax-machine/sax_handler.rb
39
- - README.rdoc
39
+ - README.textile
40
40
  - Rakefile
41
41
  - spec/spec.opts
42
42
  - spec/spec_helper.rb
43
43
  - spec/sax-machine/sax_document_spec.rb
44
- has_rdoc: true
44
+ has_rdoc: false
45
45
  homepage: http://github.com/pauldix/sax-machine
46
46
  post_install_message:
47
47
  rdoc_options: []