shenanigans 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/doc/Hash.html +64 -0
- data/doc/created.rid +3 -2
- data/doc/js/search_index.js +1 -1
- data/doc/table_of_contents.html +2 -0
- data/lib/shenanigans/hash/to_ostruct.rb +37 -0
- data/lib/shenanigans/hash.rb +1 -0
- data/test/hash/test_to_ostruct.rb +19 -0
- metadata +4 -2
data/doc/Hash.html
CHANGED
@@ -48,6 +48,7 @@
|
|
48
48
|
<h3 class="section-header">Defined In</h3>
|
49
49
|
<ul>
|
50
50
|
<li>lib/shenanigans/hash/has_shape_pred.rb
|
51
|
+
<li>lib/shenanigans/hash/to_ostruct.rb
|
51
52
|
</ul>
|
52
53
|
</nav>
|
53
54
|
|
@@ -72,6 +73,8 @@
|
|
72
73
|
|
73
74
|
<li><a href="#method-i-has_shape-3F">#has_shape?</a>
|
74
75
|
|
76
|
+
<li><a href="#method-i-to_ostruct">#to_ostruct</a>
|
77
|
+
|
75
78
|
</ul>
|
76
79
|
</nav>
|
77
80
|
|
@@ -112,6 +115,27 @@
|
|
112
115
|
|
113
116
|
<div id="description" class="description">
|
114
117
|
|
118
|
+
<p>Recursively converts a <code>Hash</code> and all nested <code>Hash</code>es
|
119
|
+
to <code>OpenStruct</code>s. Especially useful for parsing YAML.</p>
|
120
|
+
|
121
|
+
<pre class="ruby"><span class="ruby-identifier">yaml</span>=<span class="ruby-string">"subject: Programming Languages
|
122
|
+
languages:
|
123
|
+
- name : Ruby
|
124
|
+
creator : Matz
|
125
|
+
- name : Python
|
126
|
+
creator : Guido van Rossum
|
127
|
+
- name : Perl
|
128
|
+
creator : Larry Wall
|
129
|
+
"</span>
|
130
|
+
<span class="ruby-identifier">struct</span> = <span class="ruby-constant">YAML</span>.<span class="ruby-identifier">load</span>(<span class="ruby-identifier">yaml</span>).<span class="ruby-identifier">to_ostruct</span>
|
131
|
+
<span class="ruby-identifier">struct</span>.<span class="ruby-identifier">subject</span>
|
132
|
+
<span class="ruby-comment">#=> "Programming Languages"</span>
|
133
|
+
<span class="ruby-identifier">struct</span>.<span class="ruby-identifier">languages</span>.<span class="ruby-identifier">first</span>
|
134
|
+
<span class="ruby-comment">#=> #<OpenStruct name="Ruby", creator="Matz"></span>
|
135
|
+
<span class="ruby-identifier">struct</span>.<span class="ruby-identifier">languages</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">creator</span>
|
136
|
+
<span class="ruby-comment">#=> "Matz"</span>
|
137
|
+
</pre>
|
138
|
+
|
115
139
|
</div><!-- description -->
|
116
140
|
|
117
141
|
|
@@ -179,6 +203,46 @@
|
|
179
203
|
</div><!-- has_shape-3F-method -->
|
180
204
|
|
181
205
|
|
206
|
+
<div id="method-i-to_ostruct" class="method-detail ">
|
207
|
+
|
208
|
+
<div class="method-heading">
|
209
|
+
<span class="method-name">to_ostruct</span><span
|
210
|
+
class="method-args">()</span>
|
211
|
+
<span class="method-click-advice">click to toggle source</span>
|
212
|
+
</div>
|
213
|
+
|
214
|
+
|
215
|
+
<div class="method-description">
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
<div class="method-source-code" id="to_ostruct-source">
|
222
|
+
<pre><span class="ruby-comment"># File lib/shenanigans/hash/to_ostruct.rb, line 24</span>
|
223
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">to_ostruct</span>
|
224
|
+
<span class="ruby-identifier">arr</span> = <span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
|
225
|
+
<span class="ruby-keyword">case</span> <span class="ruby-identifier">v</span>
|
226
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span>
|
227
|
+
[<span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span>.<span class="ruby-identifier">to_open_struct</span>]
|
228
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span>
|
229
|
+
[<span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">el</span><span class="ruby-operator">|</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">el</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">el</span>.<span class="ruby-identifier">to_ostruct</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">el</span> }]
|
230
|
+
<span class="ruby-keyword">else</span>
|
231
|
+
[<span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span>]
|
232
|
+
<span class="ruby-keyword">end</span>
|
233
|
+
<span class="ruby-keyword">end</span>
|
234
|
+
<span class="ruby-constant">OpenStruct</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">arr</span>)
|
235
|
+
<span class="ruby-keyword">end</span></pre>
|
236
|
+
</div><!-- to_ostruct-source -->
|
237
|
+
|
238
|
+
</div>
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
</div><!-- to_ostruct-method -->
|
244
|
+
|
245
|
+
|
182
246
|
</section><!-- public-instance-method-details -->
|
183
247
|
|
184
248
|
</section><!-- 5Buntitled-5D -->
|
data/doc/created.rid
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
|
1
|
+
Thu, 21 Jun 2012 19:19:16 +0200
|
2
2
|
README.rdoc Thu, 17 May 2012 11:45:08 +0200
|
3
3
|
lib/shenanigans/array/random_subarray.rb Thu, 17 May 2012 00:03:23 +0200
|
4
4
|
lib/shenanigans/array/zip_with.rb Tue, 19 Jun 2012 21:23:27 +0200
|
5
5
|
lib/shenanigans/array.rb Wed, 16 May 2012 20:09:21 +0200
|
6
6
|
lib/shenanigans/hash/has_shape_pred.rb Tue, 19 Jun 2012 21:25:03 +0200
|
7
|
-
lib/shenanigans/hash.rb
|
7
|
+
lib/shenanigans/hash/to_ostruct.rb Thu, 21 Jun 2012 19:19:11 +0200
|
8
|
+
lib/shenanigans/hash.rb Thu, 21 Jun 2012 19:13:44 +0200
|
8
9
|
lib/shenanigans/kernel/fn.rb Wed, 16 May 2012 23:57:36 +0200
|
9
10
|
lib/shenanigans/kernel/prompt.rb Thu, 17 May 2012 00:10:42 +0200
|
10
11
|
lib/shenanigans/kernel/with.rb Thu, 17 May 2012 00:00:08 +0200
|
data/doc/js/search_index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
var search_data = {"index":{"searchIndex":["array","hash","kernel","object","d()","display()","fn()","has_shape?()","identity()","prompt()","random_subarray()","with()","zip_with()","readme"],"longSearchIndex":["array","hash","kernel","object","object#d()","object#display()","kernel#fn()","hash#has_shape?()","object#identity()","kernel#prompt()","array#random_subarray()","kernel#with()","array#zip_with()",""],"info":[["Array","","Array.html","",""],["Hash","","Hash.html","",""],["Kernel","","Kernel.html","",""],["Object","","Object.html","",""],["d","Object","Object.html#method-i-d","(new_line = true)",""],["display","Object","Object.html#method-i-display","(new_line = true)","<p>Outputs the object and also returns it. Will use <code>puts</code> if\n<code>new_line</code> is <code>true</code> and <code>print</code>\notherwise.\n\n<pre>"foo".display ...</pre>\n"],["fn","Kernel","Kernel.html#method-i-fn","(*funs)","<p>Composes a list of functions. Functions can be specified as symbols or\nlambdas.\n\n<pre>["foo bar", "baz qux"].map ...</pre>\n"],["has_shape?","Hash","Hash.html#method-i-has_shape-3F","(shape)","<p>Checks if a hash has a certain structure.\n\n<pre>h = { k1: 1, k2: "1" }\nh.has_shape?(k1: Fixnum, k2: String) ...</pre>\n"],["identity","Object","Object.html#method-i-identity","()","<p>An identity method that provides access to an object’s <code>self</code>.\n\n<pre>[1,2,3,4,5,1,2,2,3].group_by(&:identity) ...</pre>\n"],["prompt","Kernel","Kernel.html#method-i-prompt","(text='', conversion=nil)","<p>Displays a prompt and returns chomped input. Modelled after the Python\nmethod <code>raw_input</code>, but also can …\n"],["random_subarray","Array","Array.html#method-i-random_subarray","(n=1)","<p>Generates random subarrays. Uses random numbers and bit-fiddling to assure\nperformant uniform distributions …\n"],["with","Kernel","Kernel.html#method-i-with","(o, &blk)","<p>A Pascal/ActionScript like <code>with</code> method. Yields its argument to\nthe provided block and then returns it. …\n"],["zip_with","Array","Array.html#method-i-zip_with","(other, op=nil)","<p>Zip <code>self</code> with <code>other</code>, combining the elements with\nthe provided block or symbol. The resulting array will …\n"],["README","","README_rdoc.html","","<p>Shenanigans\n<p><strong>shenanigan</strong> (plural shenanigans):\n<p>Trickery, games; skulduggery. To “call”, “claim” …\n"]]}}
|
1
|
+
var search_data = {"index":{"searchIndex":["array","hash","kernel","object","d()","display()","fn()","has_shape?()","identity()","prompt()","random_subarray()","to_ostruct()","with()","zip_with()","readme"],"longSearchIndex":["array","hash","kernel","object","object#d()","object#display()","kernel#fn()","hash#has_shape?()","object#identity()","kernel#prompt()","array#random_subarray()","hash#to_ostruct()","kernel#with()","array#zip_with()",""],"info":[["Array","","Array.html","",""],["Hash","","Hash.html","","<p>Recursively converts a <code>Hash</code> and all nested <code>Hash</code>es\nto <code>OpenStruct</code>s. Especially useful for parsing YAML …\n"],["Kernel","","Kernel.html","",""],["Object","","Object.html","",""],["d","Object","Object.html#method-i-d","(new_line = true)",""],["display","Object","Object.html#method-i-display","(new_line = true)","<p>Outputs the object and also returns it. Will use <code>puts</code> if\n<code>new_line</code> is <code>true</code> and <code>print</code>\notherwise.\n\n<pre>"foo".display ...</pre>\n"],["fn","Kernel","Kernel.html#method-i-fn","(*funs)","<p>Composes a list of functions. Functions can be specified as symbols or\nlambdas.\n\n<pre>["foo bar", "baz qux"].map ...</pre>\n"],["has_shape?","Hash","Hash.html#method-i-has_shape-3F","(shape)","<p>Checks if a hash has a certain structure.\n\n<pre>h = { k1: 1, k2: "1" }\nh.has_shape?(k1: Fixnum, k2: String) ...</pre>\n"],["identity","Object","Object.html#method-i-identity","()","<p>An identity method that provides access to an object’s <code>self</code>.\n\n<pre>[1,2,3,4,5,1,2,2,3].group_by(&:identity) ...</pre>\n"],["prompt","Kernel","Kernel.html#method-i-prompt","(text='', conversion=nil)","<p>Displays a prompt and returns chomped input. Modelled after the Python\nmethod <code>raw_input</code>, but also can …\n"],["random_subarray","Array","Array.html#method-i-random_subarray","(n=1)","<p>Generates random subarrays. Uses random numbers and bit-fiddling to assure\nperformant uniform distributions …\n"],["to_ostruct","Hash","Hash.html#method-i-to_ostruct","()",""],["with","Kernel","Kernel.html#method-i-with","(o, &blk)","<p>A Pascal/ActionScript like <code>with</code> method. Yields its argument to\nthe provided block and then returns it. …\n"],["zip_with","Array","Array.html#method-i-zip_with","(other, op=nil)","<p>Zip <code>self</code> with <code>other</code>, combining the elements with\nthe provided block or symbol. The resulting array will …\n"],["README","","README_rdoc.html","","<p>Shenanigans\n<p><strong>shenanigan</strong> (plural shenanigans):\n<p>Trickery, games; skulduggery. To “call”, “claim” …\n"]]}}
|
data/doc/table_of_contents.html
CHANGED
@@ -76,6 +76,8 @@
|
|
76
76
|
|
77
77
|
<li class="method"><a href="Array.html#method-i-random_subarray">#random_subarray — Array</a>
|
78
78
|
|
79
|
+
<li class="method"><a href="Hash.html#method-i-to_ostruct">#to_ostruct — Hash</a>
|
80
|
+
|
79
81
|
<li class="method"><a href="Kernel.html#method-i-with">#with — Kernel</a>
|
80
82
|
|
81
83
|
<li class="method"><a href="Array.html#method-i-zip_with">#zip_with — Array</a>
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
# Recursively converts a <tt>Hash</tt> and all nested <tt>Hash</tt>es to
|
4
|
+
# <tt>OpenStruct</tt>s. Especially useful for parsing YAML.
|
5
|
+
# yaml=<<EOY
|
6
|
+
# subject: Programming Languages
|
7
|
+
# languages:
|
8
|
+
# - name : Ruby
|
9
|
+
# creator : Matz
|
10
|
+
# - name : Python
|
11
|
+
# creator : Guido van Rossum
|
12
|
+
# - name : Perl
|
13
|
+
# creator : Larry Wall
|
14
|
+
# EOY
|
15
|
+
# struct = YAML.load(yaml).to_ostruct
|
16
|
+
# struct.subject
|
17
|
+
# #=> "Programming Languages"
|
18
|
+
# struct.languages.first
|
19
|
+
# #=> #<OpenStruct name="Ruby", creator="Matz">
|
20
|
+
# struct.languages.first.creator
|
21
|
+
# #=> "Matz"
|
22
|
+
|
23
|
+
class Hash
|
24
|
+
def to_ostruct
|
25
|
+
arr = map do |k, v|
|
26
|
+
case v
|
27
|
+
when Hash
|
28
|
+
[k, v.to_ostruct]
|
29
|
+
when Array
|
30
|
+
[k, v.map { |el| Hash === el ? el.to_ostruct : el }]
|
31
|
+
else
|
32
|
+
[k, v]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
OpenStruct.new(arr)
|
36
|
+
end
|
37
|
+
end
|
data/lib/shenanigans/hash.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'shenanigans/hash/to_ostruct'
|
3
|
+
|
4
|
+
class ToOstruct < MiniTest::Unit::TestCase
|
5
|
+
def test_simple_hash
|
6
|
+
struct = {a: 1, b: 2}.to_ostruct
|
7
|
+
assert struct.b == 2
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_nested_hash
|
11
|
+
struct = {a: 1, b: {c: 3}}.to_ostruct
|
12
|
+
assert struct.b.c == 3
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_nested_array
|
16
|
+
struct = {a: 1, b: [{c: 2}]}.to_ostruct
|
17
|
+
assert struct.b.first.c == 2
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shenanigans
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: citizen428@gmail.com
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- lib/shenanigans/array/zip_with.rb
|
64
64
|
- lib/shenanigans/array.rb
|
65
65
|
- lib/shenanigans/hash/has_shape_pred.rb
|
66
|
+
- lib/shenanigans/hash/to_ostruct.rb
|
66
67
|
- lib/shenanigans/hash.rb
|
67
68
|
- lib/shenanigans/kernel/fn.rb
|
68
69
|
- lib/shenanigans/kernel/prompt.rb
|
@@ -77,6 +78,7 @@ files:
|
|
77
78
|
- test/array/test_random_subarray.rb
|
78
79
|
- test/array/test_zip_with.rb
|
79
80
|
- test/hash/test_has_shape_pred.rb
|
81
|
+
- test/hash/test_to_ostruct.rb
|
80
82
|
- test/kernel/test_fn.rb
|
81
83
|
- test/kernel/test_prompt.rb
|
82
84
|
- test/kernel/test_with.rb
|