raxb 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.
Files changed (3) hide show
  1. data/lib/raxb.rb +41 -15
  2. data/tests/test.rb +54 -15
  3. metadata +2 -2
@@ -3,7 +3,7 @@ require 'rexml/element'
3
3
 
4
4
  module RAXB
5
5
 
6
- # This function is used to create a RAXB object from a source doucment (either
6
+ # This function is used to create a RAXB object from a source document (either
7
7
  # REXML::Document, or anything accepted by the REXML::Document constructor).
8
8
  def unmarshal(source)
9
9
  if source.kind_of? REXML::Element
@@ -19,10 +19,15 @@ module RAXB
19
19
 
20
20
  class ElementList < Array
21
21
  def initialize(parent, name)
22
+ super()
22
23
  @parent = parent
23
24
  @name = name
24
25
  end
25
26
 
27
+ def <<(element)
28
+ self[0] << element
29
+ end
30
+
26
31
  def [](i)
27
32
  if i == size
28
33
  if i == 1 and !at(0).exists?
@@ -44,20 +49,20 @@ module RAXB
44
49
  rexml = element.to_rexml.dup
45
50
  @parent.to_rexml.elements[i+1] = rexml
46
51
  fill Element.new(rexml, @parent, true), i, 1
47
- elsif i = size
48
- @parent.__attach__ element
49
- e = Element.new(@name, element)
50
- push e
51
- return e
52
+ # QUESTION should this be included?
53
+ # elsif i = size
54
+ # @parent.__attach__ element.to_rexml
55
+ # push(element)
52
56
  else
53
57
  raise "Index out of bounds: (#{i}) when size=#{size}"
54
58
  end
55
59
  end
56
-
60
+
57
61
  def method_missing( name, *args )
58
62
  name = name.to_s
59
63
  self[0].send( name, *args )
60
64
  end
65
+
61
66
  end
62
67
 
63
68
  class Element
@@ -79,7 +84,10 @@ module RAXB
79
84
  return @rexml
80
85
  end
81
86
 
82
- def get_elements(name)
87
+ def get_elements(name)
88
+ # TODO have the namespace carry through if it is provided... unless false
89
+ # is specified as the second param
90
+
83
91
  out = __create_new_list__ name
84
92
  @rexml.each_element( name ) do |elem|
85
93
  out.push( __wrap__( elem ) )
@@ -89,7 +97,7 @@ module RAXB
89
97
 
90
98
  def get_attribute(name)
91
99
  return @rexml.attributes[ name ].to_s
92
- end
100
+ end
93
101
 
94
102
  def text
95
103
  return @rexml.text
@@ -97,6 +105,10 @@ module RAXB
97
105
 
98
106
  def text=(t)
99
107
  @rexml.text=t
108
+ create!
109
+ end
110
+
111
+ def create!
100
112
  if @parent and !@attached
101
113
  @parent.__attach__ @rexml
102
114
  @attached = true
@@ -109,10 +121,12 @@ module RAXB
109
121
 
110
122
  def __attach__(element)
111
123
  @rexml.add element
112
- if @parent and !@attached
113
- @parent.__attach__ @rexml
114
- @attached = true
115
- end
124
+ create!
125
+ end
126
+
127
+ def <<(element)
128
+ @rexml.add_element element.to_rexml
129
+ create!
116
130
  end
117
131
 
118
132
  def method_missing(name,*args, &block)
@@ -120,14 +134,17 @@ module RAXB
120
134
  if name.gsub!(/=$/,"")
121
135
  if name.gsub!( ATTRIBUTE_RE, "" )
122
136
  @rexml.attributes[name] = args[0]
137
+ create!
123
138
  else
124
- elems = self.send(name)
125
- elems[0] = args[0]
139
+ raise 'Element setting is not supported yet'
140
+ # elems = self.send(name)
141
+ # elems[0] = args[0]
126
142
  end
127
143
  else
128
144
  if name.gsub!( ATTRIBUTE_RE, "" )
129
145
  return get_attribute(name)
130
146
  else
147
+ name = "#{args[0]}:#{name}" if args[0]
131
148
  elems = get_elements(name)
132
149
  if elems.empty?
133
150
  e = REXML::Element.new(name)
@@ -142,6 +159,15 @@ module RAXB
142
159
  return @rexml.to_s
143
160
  end
144
161
 
162
+ # QUESTION do we need these
163
+ # def dup
164
+ # return __wrap__(@rexml.dup, false)
165
+ # end
166
+ #
167
+ # def clone
168
+ # return __wrap__(@rexml.clone, false)
169
+ # end
170
+
145
171
  private
146
172
  def __create_new_list__(name)
147
173
  ElementList.new self, name
@@ -1,3 +1,6 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib/')
2
+
3
+ require 'rubygems'
1
4
  require 'raxb'
2
5
  require 'test/unit'
3
6
 
@@ -18,15 +21,16 @@ class SimpleTest < Test::Unit::TestCase
18
21
  assert_equal test1.to_s, test2.to_s
19
22
  end
20
23
 
21
- def test_1
22
- test1 = unmarshal('<test><foo><bar>hello</bar></foo></test>')
23
- test2 = unmarshal("<test><etc></etc></test>")
24
-
25
- test2.etc = test1.foo
26
-
27
- assert_equal test1.to_s, test2.to_s
28
- assert_equal test2.etc.exists?, false
29
- end
24
+ # TODO? element setting
25
+ # def test_1
26
+ # test1 = unmarshal('<test><foo><bar>hello</bar></foo></test>')
27
+ # test2 = unmarshal("<test><etc></etc></test>")
28
+ #
29
+ # test2.etc = test1.foo
30
+ #
31
+ # assert_equal test1.to_s, test2.to_s
32
+ # assert_equal test2.etc.exists?, false
33
+ # end
30
34
 
31
35
  def test_2
32
36
  test1 = unmarshal('<test><foo>hello</foo><foo>bonjour</foo></test>')
@@ -36,25 +40,25 @@ class SimpleTest < Test::Unit::TestCase
36
40
 
37
41
  assert_equal test1.foo[0].text, 'hello'
38
42
  assert_equal test1.foo[1].text, 'hola'
39
- assert_equal test1.foo[2].exists?, false
43
+ assert_equal false, test1.foo[2].exists?
40
44
  assert_equal test2.foo.text, 'hola'
41
45
  end
42
46
 
43
47
  def test_3
44
- test1 = unmarshal('<test><foo><bar>hello</bar></foo></test>')
48
+ s = '<test><foo><bar>hello</bar></foo></test>'
49
+ test1 = unmarshal(s)
45
50
  test2 = unmarshal("<test></test>")
46
51
 
47
- test2.etc = test1.foo
52
+ test2 << test1.foo
48
53
 
49
- assert_equal test1.to_s, test2.to_s
50
- assert_equal test2.etc.exists?, false
54
+ assert_equal s, test2.to_s
51
55
  end
52
56
 
53
57
  def test_4
54
58
  test = unmarshal('<test><foo><bar>hello</bar></foo></test>')
55
59
  test.foo._taxi = 'cab'
56
60
 
57
- assert_equal test.foo._taxi, 'cab'
61
+ assert_equal 'cab', test.foo._taxi
58
62
  end
59
63
 
60
64
  def test_5
@@ -69,4 +73,39 @@ class SimpleTest < Test::Unit::TestCase
69
73
 
70
74
  assert_equal rexml.to_s, rexml2.to_s
71
75
  end
76
+
77
+ def test_6
78
+ test = create "test"
79
+
80
+ test.foo.text = 'hello'
81
+
82
+ a = create "foo"
83
+ a.text = 'hola'
84
+
85
+ test << a
86
+
87
+ assert_equal 'hello', test.foo[0].text
88
+ assert_equal 'hola', test.foo[1].text
89
+ assert_equal false, test.foo[2].exists?
90
+ end
91
+
92
+ def test_7
93
+ test = create "test"
94
+
95
+ test.foo._bar = 'hello'
96
+
97
+ assert test.foo.exists?
98
+ assert_equal 'hello', test.foo._bar
99
+ end
100
+
101
+ def test_8
102
+ ja = Element.new('JobAnnouncement')
103
+ jaDutyLocation = Element.new('DutyLocation')
104
+ jaDutyLocation.Location.text = 'hello'
105
+ ja.DutyLocations << jaDutyLocation
106
+
107
+ assert ja.DutyLocations.exists?
108
+ assert ja.DutyLocations.DutyLocation.exists?
109
+ assert_equal 'hello', ja.DutyLocations.DutyLocation.Location.text
110
+ end
72
111
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: raxb
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.1"
7
- date: 2008-03-18 00:00:00 -05:00
6
+ version: "0.2"
7
+ date: 2008-06-16 00:00:00 -05:00
8
8
  summary: Ruby API for XML Binding
9
9
  require_paths:
10
10
  - lib