Permalink
Browse files

adding ability to set the parent

  • Loading branch information...
1 parent b749e60 commit a86eb6f2108ac3869dca4b88b73d1cf677b70252 @archiloque archiloque committed Jul 3, 2011
View
@@ -23,6 +23,7 @@ class AtomEntry
element :summary
element :content
element :published
+ parent :parent
end
# Class for parsing Atom feeds
@@ -43,10 +44,11 @@ feed = Atom.parse(xml_text)
feed.title # => whatever the title of the blog is
feed.url # => the main url of the blog
feed.feed_url # => goes to the feedburner feed
-
+
feed.entries.first.title # => title of the first entry
feed.entries.first.author # => the author of the first entry
feed.entries.first.url # => the permalink on the blog for this entry
+feed.entries.first.parent # => the Atom parent
# etc ...
# you can also use the elements method without specifying a class like so
@@ -62,22 +64,22 @@ response.messages.last # => "world"
h2. LICENSE
(The MIT License)
-
-Copyright (c) 2009:
-
+
+Copyright (c) 2009 - 2011:
+
"Paul Dix":http://pauldix.net
-
+
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
-
+
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
-
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -1,47 +1,52 @@
require "sax-machine/sax_element_config"
require "sax-machine/sax_collection_config"
+require "sax-machine/sax_parent_config"
module SAXMachine
class SAXConfig
- attr_accessor :top_level_elements, :collection_elements
-
+
+ attr_accessor :top_level_elements, :collection_elements, :parents
+
def initialize
- @top_level_elements = {}
- @collection_elements = {}
+ # Default value is an empty array
+ @top_level_elements = Hash.new { |hash, key| hash[key] = [] }
+ @collection_elements = Hash.new { |hash, key| hash[key] = [] }
+ @parents = []
end
-
+
def columns
@top_level_elements.map {|name, ecs| ecs }.flatten
end
-
+
def initialize_copy(sax_config)
+ super
@top_level_elements = sax_config.top_level_elements.clone
@collection_elements = sax_config.collection_elements.clone
+ @parents = sax_config.parents.clone
end
def add_top_level_element(name, options)
- @top_level_elements[name.to_s] = [] unless @top_level_elements[name.to_s]
@top_level_elements[name.to_s] << ElementConfig.new(name, options)
end
def add_collection_element(name, options)
- @collection_elements[name.to_s] = [] unless @collection_elements[name.to_s]
@collection_elements[name.to_s] << CollectionConfig.new(name, options)
end
+ def add_parent(name, options)
+ @parents << ParentConfig.new(name, options)
+ end
+
def collection_config(name, attrs)
- ces = @collection_elements[name.to_s]
- ces && ces.detect { |cc| cc.attrs_match?(attrs) }
+ @collection_elements[name.to_s].detect { |cc| cc.attrs_match?(attrs) }
end
def element_configs_for_attribute(name, attrs)
- tes = @top_level_elements[name.to_s]
- tes && tes.select { |ec| ec.has_value_and_attrs_match?(attrs) } || []
+ @top_level_elements[name.to_s].select { |ec| ec.has_value_and_attrs_match?(attrs) }
end
def element_config_for_tag(name, attrs)
- tes = @top_level_elements[name.to_s]
- tes && tes.detect { |ec| ec.attrs_match?(attrs) }
+ @top_level_elements[name.to_s].detect { |ec| ec.attrs_match?(attrs) }
end
end
end
@@ -20,14 +20,9 @@ def parse(xml_text)
end
def element(name, options = {})
- options[:as] ||= name
+ real_name = (options[:as] ||= name).to_s
sax_config.add_top_level_element(name, options)
-
- # we only want to insert the getter and setter if they haven't defined it from elsewhere.
- # this is how we allow custom parsing behavior. So you could define the setter
- # and have it parse the string into a date or whatever.
- attr_reader options[:as] unless method_defined?(options[:as].to_s)
- attr_writer options[:as] unless method_defined?("#{options[:as]}=")
+ create_attr real_name
end
def columns
@@ -74,9 +69,23 @@ def #{options[:as]}
attr_writer options[:as] unless method_defined?("#{options[:as]}=")
end
+ def parent(name, options = {})
+ real_name = (options[:as] ||= name).to_s
+ sax_config.add_parent(name, options)
+ create_attr(real_name)
+ end
+
def sax_config
@sax_config ||= SAXConfig.new
end
+
+ # we only want to insert the getter and setter if they haven't defined it from elsewhere.
+ # this is how we allow custom parsing behavior. So you could define the setter
+ # and have it parse the string into a date or whatever.
+ def create_attr real_name
+ attr_reader real_name unless method_defined?(real_name)
+ attr_writer real_name unless method_defined?("#{real_name}=")
+ end
end
end
@@ -52,6 +52,14 @@ def end_element(name)
object.send(config.setter, value) unless value == ""
mark_as_parsed(object, config)
end
+
+ # try to set the parent
+ sax_config = element.class.respond_to?(:sax_config) ? element.class.sax_config : nil
+ if sax_config
+ sax_config.parents.each do |parent|
+ element.send(parent.setter, object)
+ end
+ end
end
stack.pop
end
@@ -0,0 +1,21 @@
+module SAXMachine
+ class SAXConfig
+
+ class ParentConfig
+ attr_reader :name, :setter
+
+ def initialize(name, options)
+ @name = name.to_s
+
+ @as = options[:as]
+ @setter = "#{@as}="
+ end
+
+ def column
+ @as || @name.to_sym
+ end
+
+ end
+
+ end
+end
@@ -490,6 +490,7 @@ class Category
element :category, :value => :id, :as => :id
element :title
element :categories, :as => :collection, :class => CategoryCollection
+ parent :parent
end
class CategoryCollection
include SAXMachine
@@ -501,6 +502,7 @@ class CategoryCollection
it "should parse the first category" do
@collection.categories.first.id.should == "1"
@collection.categories.first.title.should == "First"
+ @collection.categories.first.parent.should == @collection
end
it "should parse the nested category" do

0 comments on commit a86eb6f

Please sign in to comment.