Class: Syndication::Container
- Inherits:
-
Object
- Object
- Syndication::Container
- Defined in:
- lib/syndication/common.rb,
lib/syndication/rss.rb
Overview
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.
Direct Known Subclasses
Atom::Category, Atom::Data, Atom::Entry, Atom::Feed, Atom::Link, Atom::Person, RSS::Channel, RSS::Cloud, RSS::Enclosure, RSS::Feed, RSS::Image, RSS::Item, RSS::TextInput
Instance Method Summary collapse
-
#initialize(parent, tag = nil, attrs = nil) ⇒ Container
constructor
Create a container.
-
#parse_date(field) ⇒ Object
Parse a date field on demand.
-
#store(tag, obj) ⇒ Object
Store an object in the parse tree, either in self, or in one of self’s ancestors.
-
#store_category(cat) ⇒ Object
This method is used by objects in RSS feeds that accept <category> elements.
-
#strip ⇒ Object
Strip the parent field from a container, used to make a container more amenable to pretty-printing.
-
#tag2method(tag) ⇒ Object
Convert a tag (possibly with namespace) to a method name.
-
#tag_end(endtag, current) ⇒ Object
Handle an end tag, and return what the new current object should be.
-
#tag_start(tag, attrs = nil) ⇒ Object
Handle a start tag and attributes.
Constructor Details
#initialize(parent, tag = nil, attrs = nil) ⇒ Container
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 => value of the XML attributes in the tag.
33 34 35 36 37 |
# File 'lib/syndication/common.rb', line 33 def initialize(parent, tag = nil, attrs = nil) @parent = parent @tag = tag # and ignore attrs by default end |
Instance Method Details
#parse_date(field) ⇒ Object
Parse a date field on demand. DateTime.parse is sloooow, so don’t call it unless you really have to.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/syndication/common.rb', line 86 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(tag, obj) ⇒ Object
Store an object in the parse tree, either in self, or in one of self’s ancestors.
75 76 77 78 79 80 81 82 |
# File 'lib/syndication/common.rb', line 75 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 |
#store_category(cat) ⇒ Object
This method is used by objects in RSS feeds that accept <category> elements
19 20 21 22 23 24 25 26 |
# File 'lib/syndication/rss.rb', line 19 def store_category(cat) if cat.kind_of?(String) if !defined? @category @category = Array.new end @category << cat end end |
#strip ⇒ Object
Strip the parent field from a container, used to make a container more amenable to pretty-printing.
101 102 103 104 |
# File 'lib/syndication/common.rb', line 101 def strip @parent = nil return self end |
#tag2method(tag) ⇒ Object
Convert a tag (possibly with namespace) to a method name.
25 26 27 |
# File 'lib/syndication/common.rb', line 25 def tag2method(tag) return tag.downcase.sub(/:/, '_') + '=' end |
#tag_end(endtag, current) ⇒ Object
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.
63 64 65 66 67 68 69 70 71 |
# File 'lib/syndication/common.rb', line 63 def tag_end(endtag, current) if @tag == endtag return @parent end if @parent == nil return current end return @parent.tag_end(endtag, current) end |
#tag_start(tag, attrs = nil) ⇒ Object
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.
43 44 45 46 47 48 49 50 51 |
# File 'lib/syndication/common.rb', line 43 def tag_start(tag, attrs = nil) method = tag2method(tag) if self.respond_to?(method) if attrs and !attrs.empty? self.send(method, attrs) end @current_method = method end end |