Class: ESS::Element
Direct Known Subclasses
Instance Method Summary collapse
-
#disable_postprocessing ⇒ Object
Disables postprocessing of tag values.
-
#dtd ⇒ Object
Returns the dictionary describing this tag.
-
#enable_postprocessing ⇒ Object
Enables postprocessing of tag values.
-
#initialize(name, dtd) ⇒ Element
constructor
There should never be a need to create an Element object yourself, except if you’re the developer of this library.
-
#inspect ⇒ Object
Returns a short description of this object.
-
#method_missing(m, *args, &block) ⇒ Object
Handles methods corresponding to a tag name, ending with either _list or _attr, or starting with add_ .
-
#name! ⇒ Object
Return the name of the tag as a symbol.
-
#postprocessing_disabled? ⇒ Boolean
Returns true if postprocessing has been disabled.
-
#push_to_aggregators(options = {}) ⇒ Object
A convenience method for pushing the current document to aggregators.
-
#text!(text = nil) ⇒ Object
Returns or sets the text contained in this tag.
-
#to_s ⇒ Object
Same as #to_xml!, but accepts no arguments.
-
#to_xml!(xml = nil) ⇒ Object
Returns the feed as an XML document in a string.
-
#valid? ⇒ Boolean
Same as #validate, but returns false if an error is found, instead of throwing exceptions.
-
#validate ⇒ Object
Validates the tag according to its DTD and all child tags.
Methods included from Helpers
Constructor Details
#initialize(name, dtd) ⇒ Element
There should never be a need to create an Element object yourself, except if you’re the developer of this library. Use ESS::Maker.make if you need to create a new ESS document.
Parameters
- name
-
a symbol, the name of the tag being created
- dtd
-
a hash describing the tag
31 32 33 |
# File 'lib/ess/element.rb', line 31 def initialize name, dtd @name, @dtd = name, dtd end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
Handles methods corresponding to a tag name, ending with either _list or _attr, or starting with add_ .
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ess/element.rb', line 149 def method_missing m, *args, &block if method_name_is_tag_name? m return assign_tag(m, args, &block) elsif method_name_is_tag_adder_method? m return extend_tag_list(m, args, &block) elsif method_name_is_tag_list_method? m return [m[0..-6].to_sym] ||= [] elsif method_name_is_attr_accessor_method? m return assign_attribute(m[0..-6].to_sym, args, &block) end super(m, *args, &block) end |
Instance Method Details
#disable_postprocessing ⇒ Object
Disables postprocessing of tag values.
128 129 130 |
# File 'lib/ess/element.rb', line 128 def disable_postprocessing @@postprocessing_disabled = true end |
#dtd ⇒ Object
Returns the dictionary describing this tag.
17 18 19 |
# File 'lib/ess/element.rb', line 17 def dtd return @dtd end |
#enable_postprocessing ⇒ Object
Enables postprocessing of tag values.
134 135 136 |
# File 'lib/ess/element.rb', line 134 def enable_postprocessing @@postprocessing_disabled = false end |
#inspect ⇒ Object
Returns a short description of this object.
53 54 55 |
# File 'lib/ess/element.rb', line 53 def inspect "#<#{self.class}:#{object_id} text=\"#{@text}\">" end |
#name! ⇒ Object
Return the name of the tag as a symbol.
38 39 40 |
# File 'lib/ess/element.rb', line 38 def name! @name end |
#postprocessing_disabled? ⇒ Boolean
Returns true if postprocessing has been disabled.
141 142 143 |
# File 'lib/ess/element.rb', line 141 def postprocessing_disabled? @@postprocessing_disabled ||= false end |
#push_to_aggregators(options = {}) ⇒ Object
A convenience method for pushing the current document to aggregators. It calls the ESS::Pusher.push_to_aggregators method and passes all options to it.
112 113 114 115 116 |
# File 'lib/ess/element.rb', line 112 def push_to_aggregators ={} raise RuntimeError, "only ESS root element can be pushed to aggregators" if @name != :ess [:data] = self.to_xml! Pusher::push_to_aggregators end |
#text!(text = nil) ⇒ Object
Returns or sets the text contained in this tag
45 46 47 48 |
# File 'lib/ess/element.rb', line 45 def text! text=nil return @text ||= "" if text.nil? @text = do_text_postprocessing_of text end |
#to_s ⇒ Object
Same as #to_xml!, but accepts no arguments.
121 122 123 |
# File 'lib/ess/element.rb', line 121 def to_s to_xml! end |
#to_xml!(xml = nil) ⇒ Object
Returns the feed as an XML document in a string. An Builder::XmlMarkup object can be passed as an argument and used as output, instead of generating a string object.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ess/element.rb', line 87 def to_xml! xml=nil convert_to_string = true if xml.nil? xml = Builder::XmlMarkup.new if xml.nil? if @name == :ess xml.instruct! :xml, :encoding => "UTF-8" xml.declare! :DOCTYPE, :ess, :PUBLIC, "-//ESS//DTD", "http://essfeed.org/history/0.9/index.dtd" end xml.tag! @name, attributes do |p| if !@text.nil? if @dtd[:cdata] p.cdata! @text else p.text! @text end end .values.each { |tag_list| tag_list.each { |tag| tag.to_xml!(p) } } end xml.target! if convert_to_string end |
#valid? ⇒ Boolean
Same as #validate, but returns false if an error is found, instead of throwing exceptions.
73 74 75 76 77 78 79 80 |
# File 'lib/ess/element.rb', line 73 def valid? begin validate rescue return false end return true end |
#validate ⇒ Object
Validates the tag according to its DTD and all child tags. Throws an ESS::Validation::ValidationError exception in case the document is incomplete or an invalid value if found.
62 63 64 65 66 67 |
# File 'lib/ess/element.rb', line 62 def validate run_tag_validators check_attributes return nil # if no errors found, i.e. no exceptions have been raised end |