Module: AXML::Autoload

Defined in:
lib/axml/autoload.rb

Class Method Summary collapse

Class Method Details

.install_instructions(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/axml/autoload.rb', line 60

def self.install_instructions(name)
  if name == :all
    doublets = AXML::PREFERRED.map do |nm|
      [nm, install_instructions(nm)]
    end
    string = ""
    doublets.each do |k,v|
      if v
        string << '-' * k.to_s.size << "\n"
        string << "#{k}\n"
        string << '-' * k.to_s.size << "\n"
        string << "#{v}"
      end
    end
    string
  else
    case name
    when :xmlparser
      string = <<END
debian/ubuntu: sudo apt-get install libxml-parser-ruby1.8

cygwin:
   Download the XMLParser module: http://www.yoshidam.net/Ruby.html
   Build and install:
   ruby extconf.rb --with-expat-lib=/usr/lib \ 
                 --with-expat-include=/usr/include
   make
   make site-install

windows: included in one-click-installer
END
    when :libxml
      string = <<END 
deb/ubuntu: sudo apt-get install libxml-ruby

install as gem:
sudo gem install -r libxml-ruby

for more info: http://libxml.rubyforge.org/install.xml
END
    end
  end
end

.parser(name) ⇒ Object

loads the parser (if available) and returns an object ( that should respond_to parse_io and parse_string. If the parser is not available returns nil.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/axml/autoload.rb', line 45

def self.parser(name)
  req = 'axml/' << name.to_s.gsub('_', '/')
  begin
    require req
    const_str = AXML::CLASS_MAPPINGS[name]
    if AXML.const_defined?(const_str)
      AXML.const_get(const_str)
    else
      nil
    end
  rescue LoadError
    nil
  end
end

.parser!(name = nil) ⇒ Object

if given a name, loads the parser class name if no arg (or nil) tries to load a parser from the AXML::PREFERRED list, returning the first one that works. Sets AXML::DEFAULT with the parser name if it is available and raises any warnings in AXML::WARN. Raises a RuntimeError if no parser is found.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/axml/autoload.rb', line 11

def self.parser!(name=nil)
  parser_name_to_use = nil
  parser_obj = nil
  if name.nil?
    PREFERRED.each do |nm|
      parser_obj = parser(nm)
      if parser_obj 
        parser_name_to_use = nm
        break
      end
    end
  else
    parser_name_to_use = name
    parser_obj = parser(name)
  end
  if message = WARN[parser_obj]
    warn message
  end
  if parser_obj
    AXML::DEFAULTS[:parser] = parser_name_to_use
  else
    STDERR.puts "NO PARSERS CURRENTLY AVAILABLE!"
    STDERR.puts "INSTALL INSTRUCTIONS:"
    STDERR.puts "*****************************************************"
    STDERR.puts install_instructions(:all)
    STDERR.puts "*****************************************************"
    raise RuntimeError, "no parser currently available!"
  end
  parser_obj
end