Class: ESS::Element

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/ess/element.rb

Direct Known Subclasses

ESS

Instance Method Summary collapse

Methods included from Helpers

uuid

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 child_tags[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_postprocessingObject

Disables postprocessing of tag values.



128
129
130
# File 'lib/ess/element.rb', line 128

def disable_postprocessing
  @@postprocessing_disabled = true
end

#dtdObject

Returns the dictionary describing this tag.



17
18
19
# File 'lib/ess/element.rb', line 17

def dtd
  return @dtd
end

#enable_postprocessingObject

Enables postprocessing of tag values.



134
135
136
# File 'lib/ess/element.rb', line 134

def enable_postprocessing
  @@postprocessing_disabled = false
end

#inspectObject

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.

Returns:

  • (Boolean)


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.

Raises:

  • (RuntimeError)


112
113
114
115
116
# File 'lib/ess/element.rb', line 112

def push_to_aggregators options={}
  raise RuntimeError, "only ESS root element can be pushed to aggregators" if @name != :ess
  options[:data] = self.to_xml!
  Pusher::push_to_aggregators options
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_sObject

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
    child_tags.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.

Returns:

  • (Boolean)


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

#validateObject

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
  check_child_tags
  return nil # if no errors found, i.e. no exceptions have been raised
end