hashutils 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.
Files changed (48) hide show
  1. data/README +93 -0
  2. data/Rakefile +51 -0
  3. data/doc/classes/Array.html +131 -0
  4. data/doc/classes/Hash.html +137 -0
  5. data/doc/classes/HashUtils.html +131 -0
  6. data/doc/classes/HashUtils.src/M000018.html +20 -0
  7. data/doc/classes/HashUtilsNamedAttributes.html +131 -0
  8. data/doc/classes/HashUtilsNamedAttributes.src/M000019.html +29 -0
  9. data/doc/classes/HashUtilsXML.html +164 -0
  10. data/doc/classes/HashUtilsXML.src/M000015.html +56 -0
  11. data/doc/classes/HashUtilsXML.src/M000016.html +44 -0
  12. data/doc/classes/HashUtilsXML.src/M000017.html +29 -0
  13. data/doc/classes/TC_HashUtilsFromXML.html +170 -0
  14. data/doc/classes/TC_HashUtilsFromXML.src/M000012.html +25 -0
  15. data/doc/classes/TC_HashUtilsFromXML.src/M000013.html +37 -0
  16. data/doc/classes/TC_HashUtilsFromXML.src/M000014.html +18 -0
  17. data/doc/classes/TC_HashUtilsHashAsAttributes.html +170 -0
  18. data/doc/classes/TC_HashUtilsHashAsAttributes.src/M000008.html +18 -0
  19. data/doc/classes/TC_HashUtilsHashAsAttributes.src/M000009.html +19 -0
  20. data/doc/classes/TC_HashUtilsHashAsAttributes.src/M000010.html +20 -0
  21. data/doc/classes/TC_HashUtilsToXML.html +237 -0
  22. data/doc/classes/TC_HashUtilsToXML.src/M000001.html +34 -0
  23. data/doc/classes/TC_HashUtilsToXML.src/M000002.html +22 -0
  24. data/doc/classes/TC_HashUtilsToXML.src/M000003.html +24 -0
  25. data/doc/classes/TC_HashUtilsToXML.src/M000004.html +30 -0
  26. data/doc/classes/TC_HashUtilsToXML.src/M000005.html +22 -0
  27. data/doc/classes/TC_HashUtilsToXML.src/M000006.html +26 -0
  28. data/doc/classes/TC_HashUtilsToXML.src/M000007.html +31 -0
  29. data/doc/classes/TS_HashUtils.html +137 -0
  30. data/doc/classes/TS_HashUtils.src/M000011.html +22 -0
  31. data/doc/created.rid +1 -0
  32. data/doc/files/lib/hashutils_rb.html +108 -0
  33. data/doc/files/rakefile_rb.html +101 -0
  34. data/doc/files/test/tc_hash_utils_from_xml_rb.html +109 -0
  35. data/doc/files/test/tc_hash_utils_hash_as_attributes_rb.html +109 -0
  36. data/doc/files/test/tc_hash_utils_to_xml_rb.html +110 -0
  37. data/doc/files/test/ts_hash_utils_rb.html +112 -0
  38. data/doc/fr_class_index.html +35 -0
  39. data/doc/fr_file_index.html +32 -0
  40. data/doc/fr_method_index.html +45 -0
  41. data/doc/index.html +24 -0
  42. data/doc/rdoc-style.css +208 -0
  43. data/lib/hashutils.rb +178 -0
  44. data/test/tc_hash_utils_from_xml.rb +47 -0
  45. data/test/tc_hash_utils_hash_as_attributes.rb +24 -0
  46. data/test/tc_hash_utils_to_xml.rb +103 -0
  47. data/test/ts_hash_utils.rb +18 -0
  48. metadata +101 -0
data/README ADDED
@@ -0,0 +1,93 @@
1
+ = Hashutils
2
+
3
+ Hashutils is a collection of utilities for dealing with hashes.
4
+ Most remarkable features are to_xml, from_xml and accepting methods as key accessors
5
+
6
+ == Installing Hashutils
7
+
8
+ You may get the latest stable version from Rubyforge. Win32 binaries and source
9
+ gems are available.
10
+
11
+ $ gem install hashutils
12
+
13
+ == Using Hashutils
14
+
15
+ === Loading Hashutils Itself
16
+
17
+ You have probably got the gem, right? To load:
18
+
19
+ require 'rubygems'
20
+ require 'hashutils'
21
+
22
+ === Creating XML out of a hash
23
+
24
+ Take any hash an try:
25
+
26
+ hash.to_xml
27
+
28
+ A XML with all key-value pairs will be generated as a String.
29
+
30
+ If you wish to have a root element node, try:
31
+
32
+ hash.to_xml :name => :root
33
+
34
+ This will create the same XML as before but surrounded by an element node named
35
+ <tt>root</tt>.
36
+
37
+ There's also the possibility of generating pretty-print XML like this:
38
+
39
+ hash.to_xml :pretty => true
40
+
41
+ And both parameters can be combined:
42
+
43
+ hash.to_xml :name => :root, :pretty => true
44
+
45
+ === Mapping attributes and elements
46
+
47
+ The attribute <tt>xml_attributes</tt> is added to hashes and corresponds to an Array
48
+ of key names that will be treated as attributes during XML generation. By default
49
+ all keys are considered to be elements.
50
+
51
+ hash.xml_attributes = [:attr1, :attr2]
52
+
53
+ This will ensure that keys named :attr1 and :attr2 will be generated as XML attributes.
54
+
55
+ === Dealing with Arrays
56
+
57
+ By default any array found during the XML generation process will be rendered with each
58
+ array item in an element node called <tt>item</tt> like this:
59
+
60
+ <array>
61
+ <item>1</item>
62
+ <item>2</item>
63
+ </array>
64
+
65
+ In order to change this behaviour the array's attribute <tt>xml_name</tt> must be changed.
66
+
67
+ hash[:array].xml_name = :number
68
+
69
+ Will generate this:
70
+
71
+ <array>
72
+ <number>1</number>
73
+ <number>2</number>
74
+ </array>
75
+
76
+ === Parsing an XML into a hash
77
+
78
+ Any valid XML in String format can be parsed into a hashby using <tt>from_xml</tt>:
79
+
80
+ new_hash = old_hash.from_xml(xml)
81
+
82
+ A muttable form is also available:
83
+
84
+ hash.from_xml!(xml)
85
+
86
+ === Accessing values using key as methods
87
+
88
+ Sometimes the form <tt>hash[:key]</tt> is awkward. Hashutils allows keys to be used
89
+ as methods in the following fashion:
90
+
91
+ hash.key
92
+
93
+
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ desc "Same as 'test'"
2
+ task :default => [:test]
3
+
4
+ desc "Run test suite"
5
+ task :test do
6
+ ruby "test/ts_hash_utils.rb"
7
+ end
8
+
9
+ desc "Create docs"
10
+ task :doc do
11
+ sh "rdoc"
12
+ end
13
+
14
+ desc "Remove all generated docs and distribution packages"
15
+ task :cleanall => [:cleandoc, :cleandist] do
16
+ end
17
+
18
+ desc "Remove generated docs"
19
+ task :cleandoc do
20
+ FileUtils.rmtree("doc")
21
+ end
22
+
23
+ desc "Remove all distribution packages"
24
+ task :cleandist do
25
+ FileUtils.rmtree("dist")
26
+ end
27
+
28
+ desc "Create a distribution package"
29
+ task :dist, [:version] => [:doc, :cleandist] do |t, args|
30
+ raise "Must specify version: rake dist[0.1]" if args.version.nil?
31
+ Dir.mkdir("dist")
32
+ sh "tar cvzf dist/hashutils-#{args.version}.tar.gz *"
33
+ spec = Gem::Specification.new do |s|
34
+ s.platform = Gem::Platform::RUBY
35
+ s.author = 'Tiago Luchini'
36
+ s.email = 'info@tiagoluchini.eu'
37
+ s.name = 'hashutils'
38
+ s.version = args.version
39
+ s.summary = 'Generic hash utilities (and some array ones along the way).'
40
+ s.files = FileList['lib/**/*.rb', 'bin/*', '[A-Z]*', 'test/**/*', 'doc/**/*'].to_a
41
+ s.requirements << 'none'
42
+ s.description = <<-EOF
43
+ Hashutils is a collection of utilities for dealing with hashes.
44
+ Most remarkable features are to_xml, from_xml and accepting methods as key accessors
45
+ EOF
46
+ s.homepage = 'http://hashutils.rubyforge.org'
47
+ s.rubyforge_project = 'hashutils'
48
+ end
49
+ Gem::Builder.new(spec).build
50
+ FileUtils.mv(Dir.glob('*.gem'), 'dist')
51
+ end
@@ -0,0 +1,131 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Class: Array</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Class</strong></td>
53
+ <td class="class-name-in-header">Array</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/hashutils_rb.html">
59
+ lib/hashutils.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ <tr class="top-aligned-row">
66
+ <td><strong>Parent:</strong></td>
67
+ <td>
68
+ Object
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ </div>
73
+ <!-- banner header -->
74
+
75
+ <div id="bodyContent">
76
+
77
+
78
+
79
+ <div id="contextContent">
80
+
81
+
82
+
83
+ </div>
84
+
85
+
86
+ </div>
87
+
88
+
89
+ <!-- if includes -->
90
+ <div id="includes">
91
+ <h3 class="section-bar">Included Modules</h3>
92
+
93
+ <div id="includes-list">
94
+ <span class="include-name"><a href="HashUtilsXML.html">HashUtilsXML</a></span>
95
+ </div>
96
+ </div>
97
+
98
+ <div id="section">
99
+
100
+
101
+
102
+
103
+
104
+ <div id="attribute-list">
105
+ <h3 class="section-bar">Attributes</h3>
106
+
107
+ <div class="name-list">
108
+ <table>
109
+ <tr class="top-aligned-row context-row">
110
+ <td class="context-item-name">xml_name</td>
111
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
112
+ <td class="context-item-desc"></td>
113
+ </tr>
114
+ </table>
115
+ </div>
116
+ </div>
117
+
118
+
119
+
120
+ <!-- if method_list -->
121
+
122
+
123
+ </div>
124
+
125
+
126
+ <div id="validator-badges">
127
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
128
+ </div>
129
+
130
+ </body>
131
+ </html>
@@ -0,0 +1,137 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Class: Hash</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Class</strong></td>
53
+ <td class="class-name-in-header">Hash</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/hashutils_rb.html">
59
+ lib/hashutils.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ <tr class="top-aligned-row">
66
+ <td><strong>Parent:</strong></td>
67
+ <td>
68
+ Object
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ </div>
73
+ <!-- banner header -->
74
+
75
+ <div id="bodyContent">
76
+
77
+
78
+
79
+ <div id="contextContent">
80
+
81
+
82
+
83
+ </div>
84
+
85
+
86
+ </div>
87
+
88
+
89
+ <!-- if includes -->
90
+ <div id="includes">
91
+ <h3 class="section-bar">Included Modules</h3>
92
+
93
+ <div id="includes-list">
94
+ <span class="include-name"><a href="HashUtilsXML.html">HashUtilsXML</a></span>
95
+ <span class="include-name"><a href="HashUtilsNamedAttributes.html">HashUtilsNamedAttributes</a></span>
96
+ </div>
97
+ </div>
98
+
99
+ <div id="section">
100
+
101
+
102
+
103
+
104
+
105
+ <div id="attribute-list">
106
+ <h3 class="section-bar">Attributes</h3>
107
+
108
+ <div class="name-list">
109
+ <table>
110
+ <tr class="top-aligned-row context-row">
111
+ <td class="context-item-name">xml_attributes</td>
112
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
113
+ <td class="context-item-desc"></td>
114
+ </tr>
115
+ <tr class="top-aligned-row context-row">
116
+ <td class="context-item-name">xml_doc_name</td>
117
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
118
+ <td class="context-item-desc"></td>
119
+ </tr>
120
+ </table>
121
+ </div>
122
+ </div>
123
+
124
+
125
+
126
+ <!-- if method_list -->
127
+
128
+
129
+ </div>
130
+
131
+
132
+ <div id="validator-badges">
133
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
134
+ </div>
135
+
136
+ </body>
137
+ </html>
@@ -0,0 +1,131 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: HashUtils</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">HashUtils</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/hashutils_rb.html">
59
+ lib/hashutils.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ </table>
66
+ </div>
67
+ <!-- banner header -->
68
+
69
+ <div id="bodyContent">
70
+
71
+
72
+
73
+ <div id="contextContent">
74
+
75
+
76
+
77
+ </div>
78
+
79
+ <div id="method-list">
80
+ <h3 class="section-bar">Methods</h3>
81
+
82
+ <div class="name-list">
83
+ <a href="#M000018">from_xml</a>&nbsp;&nbsp;
84
+ </div>
85
+ </div>
86
+
87
+ </div>
88
+
89
+
90
+ <!-- if includes -->
91
+
92
+ <div id="section">
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ <!-- if method_list -->
102
+ <div id="methods">
103
+ <h3 class="section-bar">Public Class methods</h3>
104
+
105
+ <div id="method-M000018" class="method-detail">
106
+ <a name="M000018"></a>
107
+
108
+ <div class="method-heading">
109
+ <a href="HashUtils.src/M000018.html" target="Code" class="method-signature"
110
+ onclick="popupCode('HashUtils.src/M000018.html');return false;">
111
+ <span class="method-name">from_xml</span><span class="method-args">(xml)</span>
112
+ </a>
113
+ </div>
114
+
115
+ <div class="method-description">
116
+ </div>
117
+ </div>
118
+
119
+
120
+ </div>
121
+
122
+
123
+ </div>
124
+
125
+
126
+ <div id="validator-badges">
127
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
128
+ </div>
129
+
130
+ </body>
131
+ </html>