| Class | Syndication::Container |
| In: |
lib/syndication/common.rb
lib/syndication/rss.rb |
| Parent: | Object |
A Container is an object in the parse tree that stores data, and possibly other objects. Its naming and behavior is an internal detail, not part of the API, and hence subject to change.
In other words, to use the library you don’t have to know about anything below.
Create a container. parent is the new container’s parent object in the final parse tree. tag is the XML tag which caused creation of the container. attrs is a hash of {attr => value} of the XML attributes in the tag.
# File lib/syndication/common.rb, line 35 def initialize(parent, tag = nil, attrs = nil) @parent = parent @tag = tag # and ignore attrs by default end
Parse a date field on demand. DateTime.parse is sloooow, so don’t call it unless you really have to.
# File lib/syndication/common.rb, line 88 def parse_date(field) if !field return nil end if field.kind_of?(String) dt = DateTime.parse(field) if dt.kind_of?(DateTime) field = dt end end return field end
Store an object in the parse tree, either in self, or in one of self’s ancestors.
# File lib/syndication/common.rb, line 77 def store(tag, obj) method = tag2method(tag) if self.respond_to?(method) self.send(method, obj) else @parent.store(tag, obj) if @parent end end
Strip the parent field from a container, used to make a container more amenable to pretty-printing.
# File lib/syndication/common.rb, line 103 def strip @parent = nil return self end
Convert a tag (possibly with namespace) to a method name.
# File lib/syndication/common.rb, line 27 def tag2method(tag) return tag.downcase.sub(/:/, '_') + '=' end
Handle an end tag, and return what the new current object should be.
If the tag matches the one we were created with, this container is complete and the new current object is its parent.
If there’s no parent (i.e. this is the top level container in the parse tree), the new current object must be unchanged.
Otherwise, pass the end tag up to the parent to see if it can do anything with it.
# File lib/syndication/common.rb, line 65 def tag_end(endtag, current) if @tag == endtag return @parent end if @parent == nil return current end return @parent.tag_end(endtag, current) end
Handle a start tag and attributes. Checks to see if self has a field with the appropriate name. If so, we send it the attributes (if any), and record that the current method is the method to access that field.
# File lib/syndication/common.rb, line 45 def tag_start(tag, attrs = nil) method = tag2method(tag) if self.respond_to?(method) if attrs self.send(method, attrs) end @current_method = method end end