Class: XmlInterface

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

Direct Known Subclasses

XmlObject

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ XmlInterface



8
9
10
11
12
13
14
15
# File 'lib/xmlobject/interface.rb', line 8

def initialize(xml)
  @cache = {
    :child => [],
    :attr => [],
    :method => [],
    :object => {}
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xmlobject/interface.rb', line 22

def method_missing(name, *args)
  if name =~ /\?$/
    name = name.to_s[0..-2]
    if @cache[:child].include?(name)
      true
    elsif @cache[:attr].include?(name)
      if self[name] =~ /^\d+$/
        self[name].to_i != 0
      else
        !self[name].to_s.strip.empty?
      end
    else
      false
    end
  else
    if @cache[:object][name].size == 1
      @cache[:object][name] = @cache[:object][name].first
      yield @cache[:object][name] if block_given?
      @cache[:object][name]
    elsif not @cache[:object][name].empty?
      @cache[:object][name].each { |o| yield o } if block_given?
      @cache[:object][name]
    else
      self[name]
    end
  end
end

Instance Method Details

#idObject



3
# File 'lib/xmlobject/interface.rb', line 3

def id; method_missing(:id); end

#inspectObject



4
# File 'lib/xmlobject/interface.rb', line 4

def inspect; @xml.to_s; end

#methodsObject



17
18
19
20
# File 'lib/xmlobject/interface.rb', line 17

def methods
  @cache[:method] = @cache[:child] + @cache[:attr] if @cache[:method].empty?
  @cache[:method]
end

#to_aObject



6
# File 'lib/xmlobject/interface.rb', line 6

def to_a; [self]; end

#to_sObject



5
# File 'lib/xmlobject/interface.rb', line 5

def to_s; @xml.to_s; end