Class: ESS::Element

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

Direct Known Subclasses

ESS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

uuid

Constructor Details

#initialize(name, dtd) ⇒ Element

Returns a new instance of Element.



11
12
13
# File 'lib/ess/element.rb', line 11

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



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ess/element.rb', line 74

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 Attribute Details

#dtdObject (readonly)

Returns the value of attribute dtd.



9
10
11
# File 'lib/ess/element.rb', line 9

def dtd
  @dtd
end

Instance Method Details

#inspectObject



24
25
26
# File 'lib/ess/element.rb', line 24

def inspect
  "#<#{self.class}:#{object_id} text=\"#{@text}\">"
end

#name!Object



15
16
17
# File 'lib/ess/element.rb', line 15

def name!
  @name
end

#push_to_aggregators(options = {}) ⇒ Object

Raises:

  • (RuntimeError)


64
65
66
67
68
# File 'lib/ess/element.rb', line 64

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



19
20
21
22
# File 'lib/ess/element.rb', line 19

def text! text=nil
  return @text ||= "" if text.nil?
  @text = do_text_postprocessing_of text
end

#to_sObject



70
71
72
# File 'lib/ess/element.rb', line 70

def to_s
  to_xml!
end

#to_xml!(xml = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ess/element.rb', line 44

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

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'lib/ess/element.rb', line 35

def valid?
  begin
    validate
  rescue
    return false
  end
  return true
end

#validateObject



28
29
30
31
32
33
# File 'lib/ess/element.rb', line 28

def validate
  run_tag_validators
  check_attributes
  check_child_tags
  return nil # if no errors found, i.e. no exceptions have been raised
end