Class: RAXB::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/raxb.rb

Constant Summary collapse

ATTRIBUTE_RE =
/^_/

Instance Method Summary collapse

Constructor Details

#initialize(source, parent = nil, attached = false) ⇒ Element

Returns a new instance of Element.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/raxb.rb', line 71

def initialize(source, parent=nil, attached=false)
  if source.kind_of? REXML::Document
    @rexml = source.root
  elsif source.kind_of? REXML::Element
    @rexml = source
  elsif source.kind_of? String
    @rexml = REXML::Element.new(source)
  end
  @parent = parent
  @attached = attached
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/raxb.rb', line 132

def method_missing(name,*args, &block)  
  name = name.to_s
  if name.gsub!(/=$/,"")   
    if name.gsub!( ATTRIBUTE_RE, "" )
      @rexml.attributes[name] = args[0]
      create!
    else
      raise 'Element setting is not supported yet'
#          elems = self.send(name)
#          elems[0] = args[0]        
    end
  else
    if name.gsub!( ATTRIBUTE_RE, "" )
      return get_attribute(name)
    else
      name = "#{args[0]}:#{name}" if args[0]          
      elems = get_elements(name)
      if elems.empty?
        e = REXML::Element.new(name) 
        elems.push __wrap__(e, false)
      end
      return elems
    end
  end
end

Instance Method Details

#<<(element) ⇒ Object



127
128
129
130
# File 'lib/raxb.rb', line 127

def <<(element)
  @rexml.add_element element.to_rexml
  create!
end

#__attach__(element) ⇒ Object



122
123
124
125
# File 'lib/raxb.rb', line 122

def __attach__(element)
  @rexml.add element
  create!
end

#create!Object



111
112
113
114
115
116
# File 'lib/raxb.rb', line 111

def create!
  if @parent and !@attached
    @parent.__attach__ @rexml
    @attached = true
  end
end

#exists?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/raxb.rb', line 118

def exists?
  return @attached
end

#get_attribute(name) ⇒ Object



98
99
100
# File 'lib/raxb.rb', line 98

def get_attribute(name)
  return @rexml.attributes[ name ].to_s
end

#get_elements(name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/raxb.rb', line 87

def get_elements(name)         
  # TODO have the namespace carry through if it is provided... unless false
  # is specified as the second param
  
  out = __create_new_list__ name
  @rexml.each_element( name ) do |elem|
    out.push( __wrap__( elem ) )
  end
  return out
end

#textObject



102
103
104
# File 'lib/raxb.rb', line 102

def text
  return @rexml.text
end

#text=(t) ⇒ Object



106
107
108
109
# File 'lib/raxb.rb', line 106

def text=(t)
  @rexml.text=t
  create!
end

#to_rexmlObject



83
84
85
# File 'lib/raxb.rb', line 83

def to_rexml
  return @rexml
end

#to_sObject



158
159
160
# File 'lib/raxb.rb', line 158

def to_s
  return @rexml.to_s
end