hash_magic 0.0.1 → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ Copyright (c) 2006 BehindLogic
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4
+ and associated documentation files (the "Software"), to deal in the Software without restriction,
5
+ including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6
+ and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
7
+ subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial
10
+ portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13
+ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
14
+ NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
15
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
16
+ OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,44 @@
1
+ HashMagic simply contains a couple of spin-off data structures based on the basic Ruby Hash:
2
+ 1. SlashedHash
3
+ 2. OrderedHash
4
+
5
+ You can use either of those separately or in combination. Note that ruby-1.9 Hashes are made to keep their order, but I'm not sure about setting an order explicitly for them, and definitely not setting an order ahead of time.
6
+
7
+ ==SlashedHash
8
+ The concept of a SlashedHash is to flatten down a multi-level Hash structure into a single-level Hash whose keys represent the pathway to the data in the original multi-level structure. For example,
9
+ >> sh = {'a' => 'b', 'c' => {'d' => :e, 'f' => ['a', 'b']}, 'd' => ['e', 'f']}.slashed
10
+ => {slashed: "a"=>"b", "c/d"=>:e, "d"=>["e", "f"], "c/f"=>["a", "b"]}
11
+
12
+ Once you transform a Hash into a SlashedHash, the only negative differences are:
13
+ 1. All but the end-point (or 'leaf node') data are turned into strings. Restated: Any data at the "end" of a Hash multi-level structure is left alone, but all keys in between, whether string, symbol, or anything else, is turned into a string in order to pack them together into a "slashed" key.
14
+ 2. Speed. A Ruby Hash is built into the core and written mostly in C so it is very fast; but a SlashedHash clones the same C functionality on all the Hash's methods so that it can act as a normal Hash but with more sugar.
15
+
16
+ Like I said, the above mentioned things are the only negative differences. You can still access items by their original keys -- to continue the example above,
17
+ >> sh['c']['d']
18
+ => :e
19
+ BUT you can also access the same values by their "slashed" keys:
20
+ >> sh['c/d']
21
+ => :e
22
+ >> sh['c/f']
23
+ => ["a", "b"]
24
+ And you can access the keys either normally or flattened:
25
+ >> sh.keys
26
+ => ["a", "c", "d"]
27
+ >> sh.flat.keys
28
+ => ["a", "c/d", "d", "c/f"]
29
+ Accessing one level into the SlashedHash gives you another SlashedHash, if applicable:
30
+ >> sh['c']
31
+ => {slashed: "d"=>:e, "f"=>["a", "b"]}
32
+
33
+ ==What good is SlashedHash? What can it do for me?
34
+
35
+ 1) It comes in VERY handy when doing special transformations or multi-level key mappings. Think of reading an XML structure into specific property names (example uses the formattedstring gem syntax):
36
+ >> joe = "<person><name>Joe Schmoe</name><age>25</age><parent><name>Joseph Schmoe</name><age>56</age></parent>".formatted(:xml).to_hash.slashed
37
+ => {slashed: "person"=>{"name"=>"Joe Schmoe", "age"=>"25", "parent"=>{"name"=>"Joseph Schmoe", "age"=>"56"}}}
38
+ >> mapping = {'name' => 'person/name', 'age' => 'person/age', 'parent_name' => 'person/parent/name', 'parent_age' => 'person/parent/age'}
39
+ >> joe.transform_keys_with_mapping(mapping)
40
+ => {"name"=>"Joe Schmoe", "parent_name"=>"Joseph Schmoe", "age"=>"25", "parent_age"=>"56"}
41
+
42
+ 2) It is useful for sorting multi-level hashes by integration with OrderedHash.
43
+
44
+ / / / Still writing, will come back to this sometime... :) / / /
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
6
6
  require 'rake/contrib/rubyforgepublisher'
7
7
 
8
8
  PKG_NAME = 'hash_magic'
9
- PKG_VERSION = "0.0.1"
9
+ PKG_VERSION = "0.1.0"
10
10
 
11
11
  PKG_FILES = FileList[
12
12
  "lib/**/*", "rspec/**/*", "[A-Z]*", "Rakefile", "doc/**/*"
@@ -29,8 +29,8 @@ desc "Create documentation"
29
29
  Rake::RDocTask.new("doc") { |rdoc|
30
30
  rdoc.title = "Hash Magic"
31
31
  rdoc.rdoc_dir = 'doc'
32
- # rdoc.rdoc_files.include('README')
33
- # rdoc.rdoc_files.include('LICENSE')
32
+ rdoc.rdoc_files.include('README')
33
+ rdoc.rdoc_files.include('LICENSE')
34
34
  rdoc.rdoc_files.include('lib/**/*.rb')
35
35
  }
36
36
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Parker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-31 00:00:00 -04:00
12
+ date: 2008-04-15 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,41 +23,9 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - lib/hash_magic.rb
26
+ - LICENSE
26
27
  - Rakefile
27
- - doc/classes
28
- - doc/classes/Hash.html
29
- - doc/classes/Hash.src
30
- - doc/classes/Hash.src/M000012.html
31
- - doc/classes/Hash.src/M000013.html
32
- - doc/classes/OrderedHash.html
33
- - doc/classes/OrderedHash.src
34
- - doc/classes/OrderedHash.src/M000014.html
35
- - doc/classes/OrderedHash.src/M000015.html
36
- - doc/classes/OrderedHash.src/M000016.html
37
- - doc/classes/OrderedHash.src/M000017.html
38
- - doc/classes/OrderedHash.src/M000018.html
39
- - doc/classes/SlashedHash.html
40
- - doc/classes/SlashedHash.src
41
- - doc/classes/SlashedHash.src/M000001.html
42
- - doc/classes/SlashedHash.src/M000002.html
43
- - doc/classes/SlashedHash.src/M000003.html
44
- - doc/classes/SlashedHash.src/M000004.html
45
- - doc/classes/SlashedHash.src/M000005.html
46
- - doc/classes/SlashedHash.src/M000006.html
47
- - doc/classes/SlashedHash.src/M000007.html
48
- - doc/classes/SlashedHash.src/M000008.html
49
- - doc/classes/SlashedHash.src/M000009.html
50
- - doc/classes/SlashedHash.src/M000010.html
51
- - doc/classes/SlashedHash.src/M000011.html
52
- - doc/created.rid
53
- - doc/files
54
- - doc/files/lib
55
- - doc/files/lib/hash_magic_rb.html
56
- - doc/fr_class_index.html
57
- - doc/fr_file_index.html
58
- - doc/fr_method_index.html
59
- - doc/index.html
60
- - doc/rdoc-style.css
28
+ - README
61
29
  has_rdoc: true
62
30
  homepage: http://hash_magic.rubyforge.org
63
31
  post_install_message:
@@ -1,177 +0,0 @@
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/hash_magic_rb.html">
59
- lib/hash_magic.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
- <div id="description">
82
- <p>
83
- The aim of this gem:
84
- </p>
85
- <ol>
86
- <li>Don&#8216;t extend <a href="Hash.html">Hash</a>
87
-
88
- </li>
89
- <li>Provide <a href="SlashedHash.html">SlashedHash</a>
90
-
91
- </li>
92
- <li>Provide <a href="OrderedHash.html">OrderedHash</a>
93
-
94
- </li>
95
- <li>Make a <a href="SlashedHash.html">SlashedHash</a> orderable
96
-
97
- </li>
98
- <li>Make an <a href="OrderedHash.html">OrderedHash</a> convert correctly to an
99
- <a href="Hash.html#M000013">ordered</a> <a
100
- href="SlashedHash.html">SlashedHash</a>
101
-
102
- </li>
103
- </ol>
104
-
105
- </div>
106
-
107
-
108
- </div>
109
-
110
- <div id="method-list">
111
- <h3 class="section-bar">Methods</h3>
112
-
113
- <div class="name-list">
114
- <a href="#M000013">ordered</a>&nbsp;&nbsp;
115
- <a href="#M000012">slashed</a>&nbsp;&nbsp;
116
- </div>
117
- </div>
118
-
119
- </div>
120
-
121
-
122
- <!-- if includes -->
123
-
124
- <div id="section">
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
- <!-- if method_list -->
134
- <div id="methods">
135
- <h3 class="section-bar">Public Instance methods</h3>
136
-
137
- <div id="method-M000013" class="method-detail">
138
- <a name="M000013"></a>
139
-
140
- <div class="method-heading">
141
- <a href="Hash.src/M000013.html" target="Code" class="method-signature"
142
- onclick="popupCode('Hash.src/M000013.html');return false;">
143
- <span class="method-name">ordered</span><span class="method-args">(*keys_in_order)</span>
144
- </a>
145
- </div>
146
-
147
- <div class="method-description">
148
- </div>
149
- </div>
150
-
151
- <div id="method-M000012" class="method-detail">
152
- <a name="M000012"></a>
153
-
154
- <div class="method-heading">
155
- <a href="Hash.src/M000012.html" target="Code" class="method-signature"
156
- onclick="popupCode('Hash.src/M000012.html');return false;">
157
- <span class="method-name">slashed</span><span class="method-args">()</span>
158
- </a>
159
- </div>
160
-
161
- <div class="method-description">
162
- </div>
163
- </div>
164
-
165
-
166
- </div>
167
-
168
-
169
- </div>
170
-
171
-
172
- <div id="validator-badges">
173
- <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
174
- </div>
175
-
176
- </body>
177
- </html>
@@ -1,18 +0,0 @@
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>
7
- <head>
8
- <title>slashed (Hash)</title>
9
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
- <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
11
- </head>
12
- <body class="standalone-code">
13
- <pre><span class="ruby-comment cmt"># File lib/hash_magic.rb, line 9</span>
14
- <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">slashed</span>
15
- <span class="ruby-constant">SlashedHash</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>)
16
- <span class="ruby-keyword kw">end</span></pre>
17
- </body>
18
- </html>
@@ -1,18 +0,0 @@
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>
7
- <head>
8
- <title>ordered (Hash)</title>
9
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
- <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
11
- </head>
12
- <body class="standalone-code">
13
- <pre><span class="ruby-comment cmt"># File lib/hash_magic.rb, line 12</span>
14
- <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">ordered</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">keys_in_order</span>)
15
- <span class="ruby-constant">OrderedHash</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">keys_in_order</span>).<span class="ruby-identifier">update!</span>(<span class="ruby-keyword kw">self</span>)
16
- <span class="ruby-keyword kw">end</span></pre>
17
- </body>
18
- </html>
@@ -1,208 +0,0 @@
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: OrderedHash</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">OrderedHash</td>
54
- </tr>
55
- <tr class="top-aligned-row">
56
- <td><strong>In:</strong></td>
57
- <td>
58
- <a href="../files/lib/hash_magic_rb.html">
59
- lib/hash_magic.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
- <a href="Hash.html">
69
- Hash
70
- </a>
71
- </td>
72
- </tr>
73
- </table>
74
- </div>
75
- <!-- banner header -->
76
-
77
- <div id="bodyContent">
78
-
79
-
80
-
81
- <div id="contextContent">
82
-
83
-
84
-
85
- </div>
86
-
87
- <div id="method-list">
88
- <h3 class="section-bar">Methods</h3>
89
-
90
- <div class="name-list">
91
- <a href="#M000015">[]=</a>&nbsp;&nbsp;
92
- <a href="#M000016">inspect</a>&nbsp;&nbsp;
93
- <a href="#M000017">keys</a>&nbsp;&nbsp;
94
- <a href="#M000014">new</a>&nbsp;&nbsp;
95
- <a href="#M000018">slashed</a>&nbsp;&nbsp;
96
- </div>
97
- </div>
98
-
99
- </div>
100
-
101
-
102
- <!-- if includes -->
103
- <div id="includes">
104
- <h3 class="section-bar">Included Modules</h3>
105
-
106
- <div id="includes-list">
107
- <span class="include-name">StandardHashMethodsInRuby</span>
108
- </div>
109
- </div>
110
-
111
- <div id="section">
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
- <!-- if method_list -->
121
- <div id="methods">
122
- <h3 class="section-bar">Public Class methods</h3>
123
-
124
- <div id="method-M000014" class="method-detail">
125
- <a name="M000014"></a>
126
-
127
- <div class="method-heading">
128
- <a href="OrderedHash.src/M000014.html" target="Code" class="method-signature"
129
- onclick="popupCode('OrderedHash.src/M000014.html');return false;">
130
- <span class="method-name">new</span><span class="method-args">(*args)</span>
131
- </a>
132
- </div>
133
-
134
- <div class="method-description">
135
- </div>
136
- </div>
137
-
138
- <h3 class="section-bar">Public Instance methods</h3>
139
-
140
- <div id="method-M000015" class="method-detail">
141
- <a name="M000015"></a>
142
-
143
- <div class="method-heading">
144
- <a href="OrderedHash.src/M000015.html" target="Code" class="method-signature"
145
- onclick="popupCode('OrderedHash.src/M000015.html');return false;">
146
- <span class="method-name">[]=</span><span class="method-args">(key,value)</span>
147
- </a>
148
- </div>
149
-
150
- <div class="method-description">
151
- </div>
152
- </div>
153
-
154
- <div id="method-M000016" class="method-detail">
155
- <a name="M000016"></a>
156
-
157
- <div class="method-heading">
158
- <a href="OrderedHash.src/M000016.html" target="Code" class="method-signature"
159
- onclick="popupCode('OrderedHash.src/M000016.html');return false;">
160
- <span class="method-name">inspect</span><span class="method-args">()</span>
161
- </a>
162
- </div>
163
-
164
- <div class="method-description">
165
- </div>
166
- </div>
167
-
168
- <div id="method-M000017" class="method-detail">
169
- <a name="M000017"></a>
170
-
171
- <div class="method-heading">
172
- <a href="OrderedHash.src/M000017.html" target="Code" class="method-signature"
173
- onclick="popupCode('OrderedHash.src/M000017.html');return false;">
174
- <span class="method-name">keys</span><span class="method-args">()</span>
175
- </a>
176
- </div>
177
-
178
- <div class="method-description">
179
- </div>
180
- </div>
181
-
182
- <div id="method-M000018" class="method-detail">
183
- <a name="M000018"></a>
184
-
185
- <div class="method-heading">
186
- <a href="OrderedHash.src/M000018.html" target="Code" class="method-signature"
187
- onclick="popupCode('OrderedHash.src/M000018.html');return false;">
188
- <span class="method-name">slashed</span><span class="method-args">()</span>
189
- </a>
190
- </div>
191
-
192
- <div class="method-description">
193
- </div>
194
- </div>
195
-
196
-
197
- </div>
198
-
199
-
200
- </div>
201
-
202
-
203
- <div id="validator-badges">
204
- <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
205
- </div>
206
-
207
- </body>
208
- </html>