class EasyDom

Attributes

e[R]

Public Class Methods

new(obj=nil, debug: false, root: 'root') click to toggle source
# File lib/easydom.rb, line 12
def initialize(obj=nil, debug: false, root: 'root')

  @debug = debug
  
  obj ||= "<#{root}/>"

  @e = obj.is_a?(String) ? FreeDom.new(obj).root : obj      

end

Public Instance Methods

==(v) click to toggle source
# File lib/easydom.rb, line 22
def ==(v)
  @e.attributes.values.first.to_s == v
end
method_missing(sym, *args, &block) click to toggle source
# File lib/easydom.rb, line 34
def method_missing(sym, *args, &block)

  puts "name: %s; args: %s" % [sym, args.inspect] if @debug

  if @e.respond_to? sym then
    
    puts 'attribute found' if @debug
    @e.method(sym).call(*args)
    
  elsif sym.to_s[-1] == '='
    
    # assumes the 1st attribute it finds is the attribute to be set
    
    puts '==' if @debug
    e = @e.element(sym.to_s[0..-2])
    
    if e.nil? then

      attribute = sym[0..-2]
      
      @e.instance_eval("def #{attribute}()
        attributes[:#{attribute}]
      end")
      
      @e.instance_eval("def #{sym}(val)
        attributes[:#{attribute}] = val
        val
      end")
      
      return @e.method(sym).call(args.first)
    end
    
    attr = e.attributes.keys.first.to_s        
    
    puts 'attribute: ' + attr.inspect if @debug
    
    e.method((attr + '=').to_sym).call(*args)
    
  else
    
    puts 'query element: ' + sym.to_s if @debug
    e = @e.element(sym.to_s)
    
    # if the element doesn't exist, create it
    e = @e.add FreeDom::Element.new(sym, attributes: {}) unless e
    
    EasyDom.new(e, debug: @debug) if e
    
  end

end
to_s() click to toggle source
# File lib/easydom.rb, line 26
def to_s()
  @e.attributes.values.first.to_s
end
to_sliml() click to toggle source
# File lib/easydom.rb, line 30
def to_sliml()
  @e.to_sliml()
end