pipa-xmlnuts 0.0.5 → 0.1

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/README.rdoc ADDED
@@ -0,0 +1,121 @@
1
+ == Introduction
2
+ XmlNuts is an library that allows for bidirectional mapping between Ruby objects and XML.
3
+
4
+ == Features
5
+ - Type awareness (extensible)
6
+ - XML namespaces support
7
+ - Pluggable backends to work with different XML APIs (only REXML implemented so far)
8
+
9
+ == Installation
10
+ gem install pipa-xmlnuts --source http://gems.github.com
11
+
12
+ == Usage
13
+ TODO
14
+ Please see example below.
15
+
16
+ == Issue tracking
17
+ Please file bugs and feature requests at http://pipa.lighthouseapp.com/projects/21756-xmlnuts
18
+
19
+ == License
20
+ Released under the MIT license (included).
21
+
22
+ == Example
23
+ class Cheezburger
24
+ include XmlNuts::Nut
25
+
26
+ attribute :weight, :integer
27
+ end
28
+
29
+ class Cat
30
+ include XmlNuts::Nut
31
+
32
+ namespaces :lol => 'urn:x-lol', :kthnx => 'urn:x-lol:kthnx'
33
+
34
+ root 'kitteh', :xmlns => :lol
35
+
36
+ attribute :has_tail, :boolean, :xmlname => 'has-tail', :xmlns => 'urn:x-lol:kthnx'
37
+ attribute :ears, :integer
38
+
39
+ element :ration, [:string], :xmlname => :eats, :xmlns => :kthnx
40
+ element :name, :string, :whitespace => :collapse, :xmlns => 'urn:x-lol:kthnx'
41
+ elements :paws, :string, :xmlname => :paw
42
+
43
+ element :friends, :xmlname => :pals do # anonymous class definition follows within block
44
+ elements :names, :string, :xmlname => :friend, :xmlname => :pal
45
+ end
46
+
47
+ element :cheezburger, Cheezburger
48
+ end
49
+
50
+ class ParsingTest < Test::Unit::TestCase
51
+ def setup
52
+ @xml_fragment = <<-EOS
53
+ <kitteh xmlns='urn:x-lol' xmlns:kthnx='urn:x-lol:kthnx' ears=' 2 ' kthnx:has-tail=' yes '>
54
+ <name xmlns='urn:x-lol:kthnx'>
55
+ Silly
56
+ Tom
57
+ Писда
58
+ </name>
59
+ <kthnx:eats>
60
+ tigers
61
+ lions
62
+ </kthnx:eats>
63
+ <pals>
64
+ <pal>Chrissy</pal>
65
+ <pal>Missy</pal>
66
+ <pal>Sissy</pal>
67
+ </pals>
68
+ <paw> one</paw>
69
+ <paw> two </paw>
70
+ <paw>three</paw>
71
+ <paw>four</paw>
72
+ <cheezburger weight='2' />
73
+ </kitteh>
74
+ EOS
75
+ @cat = Cat.parse(@xml_fragment)
76
+ end
77
+
78
+ context "A cat" do
79
+ should 'be named Silly Tom' do
80
+ assert_equal 'Silly Tom Писда', @cat.name
81
+ end
82
+
83
+ should 'eat tigers and lions' do
84
+ assert_equal %w(tigers lions), @cat.ration
85
+ end
86
+
87
+ should 'be a friend of Chrissy, Missy & Sissy' do
88
+ assert_equal ['Chrissy', 'Missy', 'Sissy'], @cat.friends.names
89
+ end
90
+
91
+ should 'have 2 ears' do
92
+ assert_equal 2, @cat.ears
93
+ end
94
+
95
+ should 'have tail' do
96
+ assert_equal true, @cat.has_tail
97
+ end
98
+
99
+ should 'have four paws' do
100
+ assert_not_nil @cat.paws
101
+ assert_equal 4, @cat.paws.length
102
+ assert_equal %w(one two three four), @cat.paws
103
+ end
104
+
105
+ should 'has cheezburger' do
106
+ assert_kind_of Cheezburger, @cat.cheezburger
107
+ end
108
+ end
109
+
110
+ context 'A cheezburger' do
111
+ setup do
112
+ @burger = @cat.cheezburger
113
+ end
114
+
115
+ should 'weigh 2 pounds' do
116
+ assert_equal 2, @burger.weight
117
+ end
118
+ end
119
+ end
120
+ ...
121
+ puts @cat.build
@@ -36,9 +36,9 @@ module XmlNuts
36
36
  def from_xml(string)
37
37
  return nil unless string
38
38
  string = case @whitespace
39
- when :trim then string.strip
39
+ when :trim then string.gsub(/(\A\s*)|(\s*\Z)/, '')
40
40
  when :preserve then string
41
- when :collapse then string.gsub(/\s+/, ' ').strip
41
+ when :collapse then string.gsub(/\s+/, ' ').gsub(/\A\s*|\s*\Z|\s*(?=\s)/, '')
42
42
  end
43
43
  end
44
44
  end
data/lib/xmlnuts/nuts.rb CHANGED
@@ -54,7 +54,8 @@ module XmlNuts #:nodoc:
54
54
  @root ||= Root.new('root')
55
55
  end
56
56
 
57
- # element(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue
57
+ # element(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue
58
+ # element(name[, options]) { block } -> Mappings::Element
58
59
  #
59
60
  # Defines single-element mapping.
60
61
  #
@@ -62,6 +63,7 @@ module XmlNuts #:nodoc:
62
63
  # +name+:: Accessor name
63
64
  # +type+:: Element type. +:string+ assumed if omitted (see +Converter+).
64
65
  # +options+:: +:xmlname+, +:xmlns+, converter options (see +Converter+).
66
+ # +block+:: An anonymous class definition.
65
67
  #
66
68
  # === Example:
67
69
  # class Cat
@@ -71,12 +73,14 @@ module XmlNuts #:nodoc:
71
73
  # element :cheeseburger, Cheeseburger, :xmlname => :cheezburger
72
74
  # ...
73
75
  # end
74
- def element(name, type = :string, options = {})
76
+ def element(name, type = :string, options = {}, &block)
77
+ type, options = prepare_args(type, options, &block)
75
78
  define_accessor name
76
79
  (mappings << (type.is_a?(Class) ? Element : ElementValue).new(name, type, prepare_options(options))).last
77
80
  end
78
81
 
79
- # elements(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue
82
+ # elements(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue
83
+ # elements(name[, options]) { block } -> Mappings::Element
80
84
  #
81
85
  # Defines multiple elements mapping.
82
86
  #
@@ -84,16 +88,18 @@ module XmlNuts #:nodoc:
84
88
  # +name+:: Accessor name
85
89
  # +type+:: Element type. +:string+ assumed if omitted (see +Converter+).
86
90
  # +options+:: +:xmlname+, +:xmlns+, converter options (see +Converter+).
91
+ # +block+:: An anonymous class definition.
87
92
  #
88
93
  # === Example:
89
94
  # class RichCat
90
95
  # include XmlNuts::Nut
91
96
  # ...
92
97
  # elements :ration, :string, :whitespace => :collapse
93
- # elements :cheeseburgers, Cheeseburger, :xmlname => :cheezburgers
98
+ # elements :cheeseburgers, Cheeseburger, :xmlname => :cheezburger
94
99
  # ...
95
100
  # end
96
- def elements(name, type = :string, options = {})
101
+ def elements(name, type = :string, options = {}, &block)
102
+ type, options = prepare_args(type, options, &block)
97
103
  define_accessor name
98
104
  (mappings << (type.is_a?(Class) ? Elements : ElementValues).new(name, type, prepare_options(options))).last
99
105
  end
@@ -122,7 +128,7 @@ module XmlNuts #:nodoc:
122
128
 
123
129
  # mappings -> Array
124
130
  #
125
- # Returns all previously defined XmlNuts mappings on a class.
131
+ # Returns all XmlNuts mappings defined on a class.
126
132
  def mappings
127
133
  @mappings ||= []
128
134
  end
@@ -143,6 +149,18 @@ module XmlNuts #:nodoc:
143
149
  end
144
150
 
145
151
  private
152
+ def prepare_args(type, options, &block)
153
+ if block_given?
154
+ options = type if type.is_a?(Hash)
155
+ type = Class.new
156
+ type.class_eval do
157
+ include XmlNuts::Nut
158
+ class_eval(&block)
159
+ end
160
+ end
161
+ return type, prepare_options(options)
162
+ end
163
+
146
164
  def prepare_options(options)
147
165
  ns = options[:xmlns]
148
166
  if ns.is_a?(Symbol)
data/lib/xmlnuts.rb CHANGED
@@ -1,48 +1 @@
1
1
  require 'xmlnuts/nuts'
2
-
3
- #class Cheezburger
4
- # include XmlNuts::Nut
5
- #
6
- # attribute :weight, :integer
7
- #end
8
- #
9
- #class Pet
10
- # include XmlNuts::Nut
11
- #
12
- # namespaces :pipi => 'a', :piz => 'b'
13
- #
14
- # root 'anus', :xmlns => :piz
15
- #
16
- # element :eats, [:string], :xmlname => :ration, :xmlns => 'a'
17
- # element :species, :string, :whitespace => :collapse
18
- # elements :paws, :string, :xmlname => :paw
19
- #
20
- # attribute :has_tail, :boolean, :xmlname => 'has-tail', :xmlns => 'b'
21
- # attribute :height, :integer
22
- #
23
- # element :cheezburger, Cheezburger
24
- #end
25
- #
26
- #xml_fragment = <<-EOS
27
- # <mypet xmlns='lol' xmlns:pizda="b" height=' 12 ' pizda:has-tail=' yes '>
28
- # <species>
29
- #silly
30
- # mouse
31
- # </species>
32
- # <pi:ration xmlns:pi="a">
33
- # tigers
34
- # lions
35
- # </pi:ration>
36
- # <paw> one</paw>
37
- # <paw> two </paw>
38
- # <paw>three</paw>
39
- # <paw>four</paw>
40
- # <cheezburger weight='2'>
41
- # </cheezburger>
42
- # <cub age='4'>
43
- # </cub>
44
- # </mypet>
45
- #EOS
46
- #pet = Pet.parse(xml_fragment)
47
- #
48
- #puts pet.build
data/test/parsing_test.rb CHANGED
@@ -13,16 +13,20 @@ end
13
13
  class Cat
14
14
  include XmlNuts::Nut
15
15
 
16
- namespaces :lol => 'urn:lol', :p => 'b'
16
+ namespaces :lol => 'urn:x-lol', :kthnx => 'urn:x-lol:kthnx'
17
17
 
18
- root 'kitteh', :xmlns => 'lol'
18
+ root 'kitteh', :xmlns => :lol
19
19
 
20
- element :eats, [:string], :xmlname => :ration, :xmlns => :lol
21
- element :friend, :string, :whitespace => :collapse, :xmlns => 'c'
20
+ attribute :has_tail, :boolean, :xmlname => 'has-tail', :xmlns => 'urn:x-lol:kthnx'
21
+ attribute :ears, :integer
22
+
23
+ element :ration, [:string], :xmlname => :eats, :xmlns => :kthnx
24
+ element :name, :string, :whitespace => :collapse, :xmlns => 'urn:x-lol:kthnx'
22
25
  elements :paws, :string, :xmlname => :paw
23
26
 
24
- attribute :has_tail, :boolean, :xmlname => 'has-tail', :xmlns => 'b'
25
- attribute :height, :integer
27
+ element :friends, :xmlname => :pals do
28
+ elements :names, :string, :xmlname => :pal
29
+ end
26
30
 
27
31
  element :cheezburger, Cheezburger
28
32
  end
@@ -30,39 +34,46 @@ end
30
34
  class ParsingTest < Test::Unit::TestCase
31
35
  def setup
32
36
  @xml_fragment = <<-EOS
33
- <mypet xmlns='lol' xmlns:aa='urn:lol' xmlns:bb='b' height=' 12 ' bb:has-tail=' yes '>
34
- <friend xmlns='c'>
35
- silly
36
- mouse
37
- </friend>
38
- <aa:ration>
37
+ <kitteh xmlns='urn:x-lol' xmlns:kthnx='urn:x-lol:kthnx' ears=' 2 ' kthnx:has-tail=' yes '>
38
+ <name xmlns='urn:x-lol:kthnx'>
39
+ Silly
40
+ Tom
41
+ Писта
42
+ </name>
43
+ <kthnx:eats>
39
44
  tigers
40
45
  lions
41
- </aa:ration>
46
+ </kthnx:eats>
47
+ <pals>
48
+ <pal>Chrissy</pal>
49
+ <pal>Missy</pal>
50
+ <pal>Sissy</pal>
51
+ </pals>
42
52
  <paw> one</paw>
43
53
  <paw> two </paw>
44
54
  <paw>three</paw>
45
55
  <paw>four</paw>
46
- <cheezburger weight='2'>
47
- </cheezburger>
48
- <cub age='4'>
49
- </cub>
50
- </mypet>
56
+ <cheezburger weight='2' />
57
+ </kitteh>
51
58
  EOS
52
59
  @cat = Cat.parse(@xml_fragment)
53
60
  end
54
61
 
55
62
  context "A cat" do
56
- should 'be a friend of a silly mouse' do
57
- assert_equal 'silly mouse', @cat.friend
63
+ should 'be named Silly Tom' do
64
+ assert_equal 'Silly Tom Писта', @cat.name
58
65
  end
59
66
 
60
67
  should 'eat tigers and lions' do
61
- assert_equal ['tigers', 'lions'], @cat.eats
68
+ assert_equal %w(tigers lions), @cat.ration
69
+ end
70
+
71
+ should 'be a friend of Chrissy, Missy & Sissy' do
72
+ assert_equal ['Chrissy', 'Missy', 'Sissy'], @cat.friends.names
62
73
  end
63
74
 
64
- should 'be 12 meters tall' do
65
- assert_equal 12, @cat.height
75
+ should 'have 2 ears' do
76
+ assert_equal 2, @cat.ears
66
77
  end
67
78
 
68
79
  should 'have tail' do
@@ -85,7 +96,7 @@ silly
85
96
  @burger = @cat.cheezburger
86
97
  end
87
98
 
88
- should 'weigh 2 kg' do
99
+ should 'weigh 2 pounds' do
89
100
  assert_equal 2, @burger.weight
90
101
  end
91
102
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipa-xmlnuts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: "0.1"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Gunko
@@ -13,17 +13,17 @@ date: 2008-12-16 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: XmlNuts is an XML to ruby object and back again mapping library.
16
+ description: XmlNuts is an XML to Ruby and back again mapping library.
17
17
  email: tekmon@gmail.com
18
18
  executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README
23
+ - README.rdoc
24
24
  - MIT-LICENSE
25
25
  files:
26
- - README
26
+ - README.rdoc
27
27
  - MIT-LICENSE
28
28
  - lib/xmlnuts.rb
29
29
  - lib/xmlnuts/nuts.rb
@@ -38,7 +38,7 @@ rdoc_options:
38
38
  - --line-numbers
39
39
  - --inline-source
40
40
  - --main
41
- - README
41
+ - README.rdoc
42
42
  require_paths:
43
43
  - lib
44
44
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -59,6 +59,6 @@ rubyforge_project:
59
59
  rubygems_version: 1.2.0
60
60
  signing_key:
61
61
  specification_version: 2
62
- summary: Making xml<->ruby binding easy
62
+ summary: Making XML <-> Ruby binding easy
63
63
  test_files:
64
64
  - test/parsing_test.rb
data/README DELETED
@@ -1,5 +0,0 @@
1
- == xmlnuts
2
-
3
-
4
-
5
- Released under the MIT license (included)