xmlobject 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  class XmlObject
2
2
  NAME = 'XmlObject'
3
- VERSION = '0.1'
3
+ VERSION = '0.2'
4
4
  COPYRIGHT = 'Copyright (C) 2007 Florent Solt'
5
5
  DESC = 'Xml object abstraction for both REXML and libxml'
6
6
  AUTHOR = 'Florent Solt'
@@ -0,0 +1,77 @@
1
+
2
+ # XmlObject is a Xml abstraction for both REXML and libxml. If libxml is present use it,
3
+ # if not switch to slower implementation REXML.
4
+ #
5
+ # Before anything else, begin with :
6
+ # require 'xmlobject'
7
+ #
8
+ # For example, you have this xml in the "buffer" variable :
9
+ # <root_tag attr_string="foo" attr_int="1" attr_int_false="0">
10
+ # <children>
11
+ # <child name='1'/>
12
+ # <child name='2'/>
13
+ # <child name='3'/>
14
+ # </children>
15
+ # <some_text>
16
+ # FooBar
17
+ # </some_text>
18
+ # </root_tag>
19
+ #
20
+ # Create an XmlObject :
21
+ # xml = XmlObject.new(buffer)
22
+ #
23
+ # Then get the root attributes :
24
+ # puts xml.attr_string -> foo
25
+ # puts xml['attr_string'] -> foo
26
+ # puts xml.attr_int -> 1
27
+ #
28
+ # Ask if some attributes or child are present or true :
29
+ # puts xml.attr_string? -> true
30
+ # puts xml.not_here? -> false
31
+ # puts xml.attr_int? -> true
32
+ # puts xml.attr_int_false? -> false
33
+ # puts xml.children? -> true
34
+ #
35
+ # Iter through children :
36
+ # xml.children.child do |child|
37
+ # puts child.name -> 1, 2, 3
38
+ # end
39
+ #
40
+ # Get some text :
41
+ # puts xml.some_text.text.strip
42
+ class XmlObject < XmlInterface
43
+
44
+ # Constructor of XmlObject based on an xml string.
45
+ # If the string is empty, raise an exception
46
+ def initialize(xml)
47
+ end
48
+
49
+ # Attribute accessor
50
+ def [](name)
51
+ end
52
+
53
+ # Text section accessor
54
+ def text
55
+ end
56
+
57
+ # Return the list of the "methods" of the xml, it's an array containing
58
+ # all attributes and all children names. This array is cached after the
59
+ # first call
60
+ def methods
61
+ end
62
+
63
+ # This is the "magic" part :)
64
+ #
65
+ # If the method name end with a "?" then the result will be a boolean.
66
+ # If the name of the method match :
67
+ # * a string attibute or a child name : return true, false otherwise
68
+ # * a integer attribute : return true if the value differ from 0, else false
69
+ #
70
+ # If the methode name does not end with a "?", then it return the matched child
71
+ # or attribute, nil if not found.
72
+ #
73
+ # All XmlObject returned are cached after the first call
74
+ def method_missing(name, *args)
75
+ end
76
+
77
+ end
@@ -1,4 +1,5 @@
1
1
  class XmlInterface
2
+
2
3
  def id; method_missing(:id); end
3
4
  def inspect; @xml.to_s; end
4
5
  def to_s; @xml.to_s; end
@@ -4,23 +4,30 @@ class XmlObject < XmlInterface
4
4
  if defined? XML # Libxml backend
5
5
  ####################################################################################
6
6
 
7
- def initialize(xml)
7
+ def initialize(xml) # :nodoc:
8
+ raise "Empty xml" if xml.to_s.strip.empty?
8
9
  super
9
10
  @parser = XML::Parser.new
10
11
  @parser.string = xml.to_s
11
12
  @xml = @parser.parse.root
12
13
  end
13
14
 
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
15
+
16
+ def [](name) # :nodoc:
17
+ @xml[name.to_s];
18
+ end
19
+
20
+ def text # :nodoc:
21
+ @xml.child? and @xml.child.text? and @xml.child.to_s or nil
22
+ end
16
23
 
17
- def methods
24
+ def methods # :nodoc:
18
25
  @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?
26
+ @xml.each_child { |i| @cache[:child] << i.name.to_s if i.element? } if @cache[:child].empty?
20
27
  super
21
28
  end
22
29
 
23
- def method_missing(name, *args)
30
+ def method_missing(name, *args) # :nodoc:
24
31
  methods # Build the cache if necessary
25
32
  name = name.to_s
26
33
  if name !~ /\?$/
@@ -36,21 +43,27 @@ if defined? XML # Libxml backend
36
43
  else # fallback to REXML
37
44
  ####################################################################################
38
45
 
39
- def initialize(xml)
46
+ def initialize(xml) # :nodoc:
47
+ raise "Empty xml" if xml.to_s.strip.empty?
40
48
  super
41
49
  @xml = REXML::Document.new(xml.to_s).root
42
50
  end
43
51
 
44
- def [](name); @xml.attributes[name.to_s]; end
45
- def text; @xml.text; end
52
+ def [](name) # :nodoc:
53
+ @xml.attributes[name.to_s]
54
+ end
55
+
56
+ def text # :nodoc:
57
+ @xml.text
58
+ end
46
59
 
47
- def methods
60
+ def methods # :nodoc:
48
61
  @xml.attributes.each { |n,v| @cache[:attr] << n.to_s } if @cache[:attr].empty?
49
62
  @xml.elements.each { |e| @cache[:child] << e.name.to_s } if @cache[:child].empty?
50
63
  super
51
64
  end
52
65
 
53
- def method_missing(name, *args, &block)
66
+ def method_missing(name, *args, &block) # :nodoc:
54
67
  methods # Build the cache if necessary
55
68
  name = name.to_s
56
69
  if name !~ /\?$/
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: xmlobject
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.1"
7
- date: 2007-04-19 00:00:00 +02:00
6
+ version: "0.2"
7
+ date: 2007-04-20 00:00:00 +02:00
8
8
  summary: Xml object abstraction for both REXML and libxml
9
9
  require_paths:
10
10
  - lib
@@ -32,6 +32,7 @@ files:
32
32
  - lib/xmlobject/object.rb
33
33
  - lib/xmlobject/interface.rb
34
34
  - lib/xmlobject/config.rb
35
+ - lib/xmlobject/doc.rb
35
36
  - lib/xmlobject.rb
36
37
  test_files: []
37
38