xmlobject 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ class XmlObject
2
+ NAME = 'XmlObject'
3
+ VERSION = '0.1'
4
+ COPYRIGHT = 'Copyright (C) 2007 Florent Solt'
5
+ DESC = 'Xml object abstraction for both REXML and libxml'
6
+ AUTHOR = 'Florent Solt'
7
+ EMAIL = 'florent@solt.biz'
8
+ HOMEPAGE = 'http://grtm.rubyforge.org'
9
+ LICENSE = 'BSD'
10
+ end
@@ -0,0 +1,48 @@
1
+ class XmlInterface
2
+ def id; method_missing(:id); end
3
+ def inspect; @xml.to_s; end
4
+ def to_s; @xml.to_s; end
5
+ def to_a; [self]; end
6
+
7
+ def initialize(xml)
8
+ @cache = {
9
+ :child => [],
10
+ :attr => [],
11
+ :method => [],
12
+ :object => {}
13
+ }
14
+ end
15
+
16
+ def methods
17
+ @cache[:method] = @cache[:child] + @cache[:attr] if @cache[:method].empty?
18
+ @cache[:method]
19
+ end
20
+
21
+ def method_missing(name, *args)
22
+ if name =~ /\?$/
23
+ name = name.to_s[0..-2]
24
+ if @cache[:child].include?(name)
25
+ true
26
+ elsif @cache[:attr].include?(name)
27
+ if self[name] =~ /^\d+$/
28
+ self[name].to_i != 0
29
+ else
30
+ !self[name].to_s.strip.empty?
31
+ end
32
+ else
33
+ false
34
+ end
35
+ else
36
+ if @cache[:object][name].size == 1
37
+ @cache[:object][name] = @cache[:object][name].first
38
+ yield @cache[:object][name] if block_given?
39
+ @cache[:object][name]
40
+ elsif not @cache[:object][name].empty?
41
+ @cache[:object][name].each { |o| yield o } if block_given?
42
+ @cache[:object][name]
43
+ else
44
+ self[name]
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,68 @@
1
+ class XmlObject < XmlInterface
2
+
3
+ ####################################################################################
4
+ if defined? XML # Libxml backend
5
+ ####################################################################################
6
+
7
+ def initialize(xml)
8
+ super
9
+ @parser = XML::Parser.new
10
+ @parser.string = xml.to_s
11
+ @xml = @parser.parse.root
12
+ end
13
+
14
+ def [](name); @xml[name.to_s]; end
15
+ def text; @xml.child? and @xml.child.text? and @xml.child.to_s or nil; end
16
+
17
+ def methods
18
+ @xml.each_attr { |i| @cache[:attr] << i.name.to_s } if @cache[:attr].empty?
19
+ @xml.each_child { |i| @cache[:child] << i.name.to_s } if @cache[:child].empty?
20
+ super
21
+ end
22
+
23
+ def method_missing(name, *args)
24
+ methods # Build the cache if necessary
25
+ name = name.to_s
26
+ if name !~ /\?$/
27
+ if @cache[:object][name].nil?
28
+ set = @xml.find(name)
29
+ @cache[:object][name] ||= set.collect { |child| XmlObject.new(child) }
30
+ end
31
+ end
32
+ super
33
+ end
34
+
35
+ ####################################################################################
36
+ else # fallback to REXML
37
+ ####################################################################################
38
+
39
+ def initialize(xml)
40
+ super
41
+ @xml = REXML::Document.new(xml.to_s).root
42
+ end
43
+
44
+ def [](name); @xml.attributes[name.to_s]; end
45
+ def text; @xml.text; end
46
+
47
+ def methods
48
+ @xml.attributes.each { |n,v| @cache[:attr] << n.to_s } if @cache[:attr].empty?
49
+ @xml.elements.each { |e| @cache[:child] << e.name.to_s } if @cache[:child].empty?
50
+ super
51
+ end
52
+
53
+ def method_missing(name, *args, &block)
54
+ methods # Build the cache if necessary
55
+ name = name.to_s
56
+ if name !~ /\?$/
57
+ if @cache[:object][name].nil?
58
+ @cache[:object][name] = []
59
+ @xml.elements.each(name) do |child|
60
+ @cache[:object][name] << XmlObject.new(child)
61
+ end
62
+ end
63
+ end
64
+ super
65
+ end
66
+
67
+ end
68
+ end
data/lib/xmlobject.rb ADDED
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'rubygems'
3
+ require 'xml/libxml'
4
+ rescue LoadError
5
+ require 'rexml/document'
6
+ end
7
+
8
+ require 'xmlobject/interface'
9
+ require 'xmlobject/object'
10
+ require 'xmlobject/config'
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: xmlobject
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2007-04-19 00:00:00 +02:00
8
+ summary: Xml object abstraction for both REXML and libxml
9
+ require_paths:
10
+ - lib
11
+ email: florent@solt.biz
12
+ homepage: http://grtm.rubyforge.org
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Florent Solt
31
+ files:
32
+ - lib/xmlobject/object.rb
33
+ - lib/xmlobject/interface.rb
34
+ - lib/xmlobject/config.rb
35
+ - lib/xmlobject.rb
36
+ test_files: []
37
+
38
+ rdoc_options: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ executables: []
43
+
44
+ extensions: []
45
+
46
+ requirements: []
47
+
48
+ dependencies: []
49
+