julien51-sax-machine 0.0.13 → 0.0.14

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.
@@ -3,6 +3,8 @@ require "sax-machine/sax_collection_config"
3
3
 
4
4
  module SAXMachine
5
5
  class SAXConfig
6
+ attr_reader :top_level_elements, :collection_elements
7
+
6
8
  def initialize
7
9
  @top_level_elements = []
8
10
  @collection_elements = []
@@ -41,6 +41,26 @@ module SAXMachine
41
41
  attr_reader options[:as] unless instance_methods.include?(options[:as].to_s)
42
42
  attr_writer options[:as] unless instance_methods.include?("#{options[:as]}=")
43
43
  end
44
+
45
+ def columns
46
+ sax_config.top_level_elements
47
+ end
48
+
49
+ def column(sym)
50
+ columns.select{|c| c.column == sym}[0]
51
+ end
52
+
53
+ def data_class(sym)
54
+ column(sym).data_class
55
+ end
56
+
57
+ def required?(sym)
58
+ column(sym).required?
59
+ end
60
+
61
+ def column_names
62
+ columns.map{|e| e.column}
63
+ end
44
64
 
45
65
  def elements(name, options = {})
46
66
  options[:as] ||= name
@@ -2,7 +2,7 @@ module SAXMachine
2
2
  class SAXConfig
3
3
 
4
4
  class ElementConfig
5
- attr_reader :name, :setter
5
+ attr_reader :name, :setter, :data_class
6
6
 
7
7
  def initialize(name, options)
8
8
  @name = name.to_s
@@ -28,6 +28,16 @@ module SAXMachine
28
28
  else
29
29
  @setter = "#{@as}="
30
30
  end
31
+ @data_class = options[:class]
32
+ @required = options[:required]
33
+ end
34
+
35
+ def column
36
+ @as || @name.to_sym
37
+ end
38
+
39
+ def required?
40
+ @required
31
41
  end
32
42
 
33
43
  def value_from_attrs(attrs)
@@ -22,8 +22,9 @@ module SAXMachine
22
22
  end
23
23
 
24
24
  def start_element(name, attrs = [])
25
+
25
26
  @name = name
26
- @attrs = attrs
27
+ @attrs = attrs.map { |a| SAXHandler.decode_xml(a) }
27
28
 
28
29
  if parsing_collection?
29
30
  @collection_handler.start_element(@name, @attrs)
@@ -49,7 +50,7 @@ module SAXMachine
49
50
  elsif parsing_collection?
50
51
  @collection_handler.end_element(name)
51
52
 
52
- elsif characaters_captured? && !parsed_config?
53
+ elsif characaters_captured?
53
54
  mark_as_parsed
54
55
  @object.send(@element_config.setter, @value)
55
56
  end
@@ -112,5 +113,20 @@ module SAXMachine
112
113
  def sax_config
113
114
  @object.class.sax_config
114
115
  end
116
+
117
+ ##
118
+ # Decodes XML special characters.
119
+ def self.decode_xml(str)
120
+ return str.map &method(:decode_xml) if str.kind_of?(Array)
121
+
122
+ entities = {
123
+ '#38' => '&',
124
+ '#13' => "\r",
125
+ }
126
+ entities.keys.inject(str) { |string, key|
127
+ string.gsub(/&#{key};/, entities[key])
128
+ }
129
+ end
130
+
115
131
  end
116
132
  end
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.13"
10
+ VERSION = "0.0.14"
11
11
  end
@@ -15,6 +15,10 @@ describe "SAXMachine" do
15
15
  document.title = "Title"
16
16
  document.title.should == "Title"
17
17
  end
18
+
19
+ it "should allow introspection of the elements" do
20
+ @klass.column_names.should =~ [:title]
21
+ end
18
22
 
19
23
  it "should not overwrite the setter if there is already one present" do
20
24
  @klass = Class.new do
@@ -28,6 +32,28 @@ describe "SAXMachine" do
28
32
  document.title = "Title"
29
33
  document.title.should == "Title **"
30
34
  end
35
+ describe "the class attribute" do
36
+ before(:each) do
37
+ @klass = Class.new do
38
+ include SAXMachine
39
+ element :date, :class => DateTime
40
+ end
41
+ @document = @klass.new
42
+ @document.date = DateTime.now.to_s
43
+ end
44
+ it "should be available" do
45
+ @klass.data_class(:date).should == DateTime
46
+ end
47
+ end
48
+ describe "the required attribute" do
49
+ it "should be available" do
50
+ @klass = Class.new do
51
+ include SAXMachine
52
+ element :date, :required => true
53
+ end
54
+ @klass.required?(:date).should be_true
55
+ end
56
+ end
31
57
 
32
58
  it "should not overwrite the accessor when the element is not present" do
33
59
  document = @klass.new
@@ -110,6 +136,11 @@ describe "SAXMachine" do
110
136
  end
111
137
  end
112
138
 
139
+ it "should escape correctly the ampersand" do
140
+ document = @klass.parse("<link href='http://api.flickr.com/services/feeds/photos_public.gne?id=49724566@N00&amp;lang=en-us&amp;format=atom' foo='bar'>asdf</link>")
141
+ document.link.should == "http://api.flickr.com/services/feeds/photos_public.gne?id=49724566@N00&amp;lang=en-us&amp;format=atom"
142
+ end
143
+
113
144
  it "should save the value of a matching element" do
114
145
  document = @klass.parse("<link href='test' foo='bar'>asdf</link>")
115
146
  document.link.should == "test"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: julien51-sax-machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix
@@ -44,6 +44,7 @@ files:
44
44
  - spec/sax-machine/sax_document_spec.rb
45
45
  has_rdoc: false
46
46
  homepage: http://github.com/pauldix/sax-machine
47
+ licenses:
47
48
  post_install_message:
48
49
  rdoc_options: []
49
50
 
@@ -64,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
65
  requirements: []
65
66
 
66
67
  rubyforge_project:
67
- rubygems_version: 1.2.0
68
+ rubygems_version: 1.3.5
68
69
  signing_key:
69
70
  specification_version: 2
70
71
  summary: Declarative SAX Parsing with Nokogiri