genx4r 0.04 → 0.05

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.
data/CHANGES CHANGED
@@ -1,7 +1,19 @@
1
+ [ Changes for 0.05 - Nov 25, 2004 ]
2
+
3
+ * Added GenX::Builder, which works much like Jim Weirich's Builder
4
+ library, but filters the output through GenX, so you get the benefit
5
+ of sanity checking.
6
+
7
+ * Fixed some bugs in the element, namespace, and attribute declaration
8
+ code that could have resulted in spurious errors when there was nothing
9
+ wrong.
10
+
1
11
  [ Changes for 0.04 - Oct 1, 2004 ]
2
12
 
3
13
  * Ruby 1.6.8 compatability. Really. I mean it this time. Sigh.
14
+
4
15
  * Update to latest genx, fixes a memory leak in genxDispose.
16
+
5
17
  * Added a gemspec for RubyGems installation.
6
18
 
7
19
  [ Changes for 0.03 - Jul 11, 2004 ]
@@ -19,7 +19,7 @@
19
19
  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
21
21
 
22
- /* $Id: genx4r.c 278 2004-07-26 13:09:20Z rooneg $ */
22
+ /* $Id: genx4r.c 494 2004-11-25 15:21:41Z rooneg $ */
23
23
 
24
24
  #include "ruby.h"
25
25
 
@@ -400,26 +400,12 @@ writer_new (VALUE klass)
400
400
  static VALUE
401
401
  writer_declare_namespace (int argc, VALUE *argv, VALUE self)
402
402
  {
403
- VALUE uri, prefix;
403
+ VALUE uri, prefix = 0;
404
+ genxStatus st = 0;
404
405
  genxNamespace ns;
405
- genxStatus st;
406
406
  genxWriter w;
407
407
 
408
- switch (argc)
409
- {
410
- case 1:
411
- uri = argv[0];
412
- prefix = 0;
413
- break;
414
-
415
- case 2:
416
- uri = argv[0];
417
- prefix = argv[1];
418
- break;
419
-
420
- default:
421
- rb_raise (rb_eRuntimeError, "invalid arguments");
422
- }
408
+ rb_scan_args (argc, argv, "11", &uri, &prefix);
423
409
 
424
410
  Check_Type (uri, T_STRING);
425
411
  if (prefix)
@@ -433,7 +419,7 @@ writer_declare_namespace (int argc, VALUE *argv, VALUE self)
433
419
  &st);
434
420
 
435
421
  if (st)
436
- rb_raise (rb_cGenXException, "%s", genxLastErrorMessage (w));
422
+ rb_raise (rb_cGenXException, "%s", genxGetErrorMessage (w, st));
437
423
 
438
424
  return Data_Wrap_Struct (rb_cGenXNamespace, NULL, NULL, ns);
439
425
  }
@@ -442,8 +428,8 @@ static VALUE
442
428
  writer_declare_element (int argc, VALUE *argv, VALUE self)
443
429
  {
444
430
  genxNamespace ns = 0;
431
+ genxStatus st = 0;
445
432
  genxElement elem;
446
- genxStatus st;
447
433
  genxWriter w;
448
434
  VALUE name;
449
435
 
@@ -472,7 +458,7 @@ writer_declare_element (int argc, VALUE *argv, VALUE self)
472
458
 
473
459
  elem = genxDeclareElement (w, ns, RSTRING (name)->ptr, &st);
474
460
  if (st)
475
- rb_raise (rb_cGenXException, "%s", genxLastErrorMessage (w));
461
+ rb_raise (rb_cGenXException, "%s", genxGetErrorMessage (w, st));
476
462
 
477
463
  return Data_Wrap_Struct (rb_cGenXElement, NULL, NULL, elem);
478
464
  }
@@ -482,7 +468,7 @@ writer_declare_attribute (int argc, VALUE *argv, VALUE self)
482
468
  {
483
469
  genxNamespace ns = 0;
484
470
  genxAttribute attr;
485
- genxStatus st;
471
+ genxStatus st = 0;
486
472
  genxWriter w;
487
473
  VALUE name;
488
474
 
@@ -511,7 +497,7 @@ writer_declare_attribute (int argc, VALUE *argv, VALUE self)
511
497
 
512
498
  attr = genxDeclareAttribute (w, ns, RSTRING (name)->ptr, &st);
513
499
  if (st)
514
- rb_raise (rb_cGenXException, "%s", genxLastErrorMessage (w));
500
+ rb_raise (rb_cGenXException, "%s", genxGetErrorMessage (w, st));
515
501
 
516
502
  return Data_Wrap_Struct (rb_cGenXAttribute, NULL, NULL, attr);
517
503
  }
@@ -1,12 +1,14 @@
1
1
  require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
- s.name = 'genx4r'
5
- s.version = '0.04'
4
+ s.name = 'genx4r'
5
+ s.version = '0.05'
6
+
6
7
  s.summary = <<-EOF
7
8
  GenX4r is a Ruby wrapper around the GenX library, which allows you to
8
9
  programatically generate correct, cannonical XML output.
9
10
  EOF
11
+
10
12
  s.description = <<-EOF
11
13
  GenX4r is a Ruby wrapper around the GenX library, which allows you to
12
14
  programatically generate correct, cannonical XML output.
@@ -16,14 +18,16 @@ spec = Gem::Specification.new do |s|
16
18
 
17
19
  s.extensions << "ext/genx4r/extconf.rb"
18
20
 
19
- s.require_path = 'lib' # Use these for libraries.
21
+ s.require_path = 'lib'
22
+
20
23
  s.autorequire = 'genx4r'
21
24
 
22
25
  s.has_rdoc = false
23
- s.test_suite_file = "test/basics.rb"
24
26
 
25
- s.author = "Garrett Rooney"
26
- s.email = "rooneg@electricjellyfish.net"
27
- s.homepage = "http://genx4r.rubyforge.org"
27
+ s.test_files = Dir.glob('test/*.rb')
28
+
29
+ s.author = "Garrett Rooney"
30
+ s.email = "rooneg@electricjellyfish.net"
31
+ s.homepage = "http://genx4r.rubyforge.org"
28
32
  s.rubyforge_project = "genx4r"
29
33
  end
@@ -0,0 +1,123 @@
1
+ require 'genx4r'
2
+
3
+ # $Id: builder.rb 449 2004-10-02 20:15:49Z rooneg $
4
+
5
+ module GenX
6
+ class Blank
7
+ instance_methods.each do |m|
8
+ undef_method m unless m =~ /^(__|instance_eval)/
9
+ end
10
+ end
11
+
12
+ class Builder < Blank
13
+ def initialize(writer)
14
+ @writer = writer
15
+
16
+ @elements = {}
17
+ @attributes = {}
18
+ @namespaces = {}
19
+
20
+ @indent = 2
21
+ @level = 0
22
+ end
23
+
24
+ def method_missing(sym, *args, &block)
25
+ text = nil
26
+ attrs = nil
27
+
28
+ args.each do |arg|
29
+ case arg
30
+ when Hash
31
+ attrs ||= {}
32
+ attrs.merge!(arg)
33
+ else
34
+ text ||= ''
35
+ text << arg.to_s
36
+ end
37
+ end
38
+
39
+ _cache_element(sym)
40
+ _cache_attrs(attrs) unless attrs.nil?
41
+
42
+ if block
43
+ raise ArgumentError, 'can\'t have both text and block' unless text.nil?
44
+ _indent
45
+ @writer.element(@elements[sym]) do
46
+ _write_attrs(attrs) unless attrs.nil?
47
+ _write_contents(block)
48
+ end
49
+ _newline
50
+ else
51
+ _indent
52
+ @writer.element(@elements[sym]) do
53
+ _write_attrs(attrs) unless attrs.nil?
54
+ @writer.text(text) unless text.nil?
55
+ end
56
+ _newline
57
+ end
58
+ end
59
+
60
+ def _write_contents(block)
61
+ begin
62
+ @level += 1
63
+ _newline
64
+ block.call(self)
65
+ ensure
66
+ @level -= 1
67
+ _indent
68
+ end
69
+ end
70
+
71
+ def _write_attrs(attrs)
72
+ attrs.each_key do |attr|
73
+ @writer.attribute(@attributes[attr], attrs[attr])
74
+ end
75
+ end
76
+
77
+ def _cache_attrs(attrs)
78
+ attrs.each_key do |attr|
79
+ unless @attributes.has_key?(attr)
80
+ @attributes[attr] = @writer.declare_attribute(attr.to_s)
81
+ end
82
+ end
83
+ end
84
+
85
+ def _cache_element(sym)
86
+ unless @elements.has_key?(sym.to_s)
87
+ @elements[sym] = @writer.declare_element(sym.to_s)
88
+ end
89
+ end
90
+
91
+ def _newline
92
+ unless @indent == 0 || @level == 0
93
+ @writer.text("\n")
94
+ end
95
+ end
96
+
97
+ def _indent
98
+ unless @indent == 0 || @level == 0
99
+ @writer.text(" " * (@indent * @level))
100
+ end
101
+ end
102
+
103
+ def _begin_doc(io)
104
+ @writer.begin_document(io)
105
+ end
106
+
107
+ def _end_doc
108
+ @writer.end_document
109
+ end
110
+ end
111
+
112
+ class Writer
113
+ def builder(io, &block)
114
+ b = Builder.new(self)
115
+ begin
116
+ b._begin_doc(io)
117
+ block.call(b)
118
+ ensure
119
+ b._end_doc
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,49 @@
1
+ require 'test/unit'
2
+
3
+ $LOAD_PATH.unshift('ext/genx4r')
4
+ $LOAD_PATH.unshift('lib')
5
+
6
+ require 'genx4r/builder'
7
+
8
+ # $Id: builder.rb 493 2004-11-25 15:17:36Z rooneg $
9
+
10
+ class BuilderTest < Test::Unit::TestCase
11
+ def test_basics
12
+ w = GenX::Writer.new()
13
+
14
+ s = ''
15
+
16
+ w.builder(s) do |b|
17
+ b.foo
18
+ end
19
+
20
+ assert s == '<foo></foo>'
21
+
22
+ s = ''
23
+
24
+ w.builder(s) do |b|
25
+ b.foo('bar')
26
+ end
27
+
28
+ assert s == '<foo>bar</foo>'
29
+
30
+ s = ''
31
+
32
+ w.builder(s) do |b|
33
+ b.foo do
34
+ b.bar('baz')
35
+ b.baz('zot', 'foo' => 'bar')
36
+ b.zim
37
+ end
38
+ end
39
+
40
+ # the \n is just so this works with the here doc...
41
+ assert (s + "\n") == <<EOF
42
+ <foo>
43
+ <bar>baz</bar>
44
+ <baz foo="bar">zot</baz>
45
+ <zim></zim>
46
+ </foo>
47
+ EOF
48
+ end
49
+ end
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift('ext/genx4r')
5
5
 
6
6
  require 'genx4r'
7
7
 
8
- # $Id: declare.rb 268 2004-07-10 00:33:11Z rooneg $
8
+ # $Id: declare.rb 491 2004-11-25 03:26:54Z rooneg $
9
9
 
10
10
  class DeclareTest < Test::Unit::TestCase
11
11
  def init_writer
@@ -55,5 +55,9 @@ class DeclareTest < Test::Unit::TestCase
55
55
 
56
56
  assert io.string == "<bar:blah xmlns:bar=\"http://foo.com/bar\"" +
57
57
  " bar:zot=\"zang\"></bar:blah>"
58
+
59
+ assert_raise(ArgumentError) do
60
+ w.declare_namespace
61
+ end
58
62
  end
59
63
  end
metadata CHANGED
@@ -3,16 +3,18 @@ rubygems_version: 0.8.1
3
3
  specification_version: 1
4
4
  name: genx4r
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.04"
7
- date: 2004-10-01
8
- summary: "GenX4r is a Ruby wrapper around the GenX library, which allows you to programatically generate correct, cannonical XML output."
6
+ version: "0.05"
7
+ date: 2004-11-25
8
+ summary: "GenX4r is a Ruby wrapper around the GenX library, which allows you to
9
+ programatically generate correct, cannonical XML output."
9
10
  require_paths:
10
11
  - lib
11
12
  author: Garrett Rooney
12
13
  email: rooneg@electricjellyfish.net
13
14
  homepage: http://genx4r.rubyforge.org
14
15
  rubyforge_project: genx4r
15
- description: "GenX4r is a Ruby wrapper around the GenX library, which allows you to programatically generate correct, cannonical XML output."
16
+ description: "GenX4r is a Ruby wrapper around the GenX library, which allows you to
17
+ programatically generate correct, cannonical XML output."
16
18
  autorequire: genx4r
17
19
  default_executable:
18
20
  bindir: bin
@@ -26,26 +28,33 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
26
28
  version:
27
29
  platform: ruby
28
30
  files:
29
- - CHANGES
31
+ - test
30
32
  - ext
31
- - genx4r.gemspec
33
+ - lib
32
34
  - LICENSE
33
- - README
34
35
  - setup.rb
35
- - test
36
36
  - TODO
37
+ - CHANGES
38
+ - README
39
+ - genx4r.gemspec
40
+ - test/non-io.rb
41
+ - test/declare.rb
42
+ - test/basics.rb
43
+ - test/builder.rb
37
44
  - ext/genx4r
38
45
  - ext/genx4r/charProps.c
39
- - ext/genx4r/extconf.rb
40
46
  - ext/genx4r/genx.c
47
+ - ext/genx4r/MANIFEST
41
48
  - ext/genx4r/genx.h
49
+ - ext/genx4r/extconf.rb
42
50
  - ext/genx4r/genx4r.c
43
- - ext/genx4r/MANIFEST
44
- - test/basics.rb
45
- - test/declare.rb
46
- - test/non-io.rb
51
+ - lib/genx4r
52
+ - lib/genx4r/builder.rb
47
53
  test_files:
54
+ - test/non-io.rb
55
+ - test/declare.rb
48
56
  - test/basics.rb
57
+ - test/builder.rb
49
58
  rdoc_options: []
50
59
  extra_rdoc_files: []
51
60
  executables: []