nuggets 0.9.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +663 -0
  3. data/ChangeLog +7 -0
  4. data/README +74 -0
  5. data/Rakefile +50 -0
  6. data/lib/nuggets.rb +73 -0
  7. data/lib/nuggets/all.rb +35 -0
  8. data/lib/nuggets/all_mixins.rb +33 -0
  9. data/lib/nuggets/ansicolor2css.rb +124 -0
  10. data/lib/nuggets/argv/option.rb +3 -0
  11. data/lib/nuggets/argv/option_mixin.rb +84 -0
  12. data/lib/nuggets/array/boost.rb +5 -0
  13. data/lib/nuggets/array/boost_mixin.rb +77 -0
  14. data/lib/nuggets/array/combination.rb +62 -0
  15. data/lib/nuggets/array/correlation.rb +5 -0
  16. data/lib/nuggets/array/correlation_mixin.rb +63 -0
  17. data/lib/nuggets/array/flatten_once.rb +56 -0
  18. data/lib/nuggets/array/flush.rb +5 -0
  19. data/lib/nuggets/array/flush_mixin.rb +46 -0
  20. data/lib/nuggets/array/format.rb +65 -0
  21. data/lib/nuggets/array/hashify.rb +5 -0
  22. data/lib/nuggets/array/hashify_mixin.rb +47 -0
  23. data/lib/nuggets/array/histogram.rb +5 -0
  24. data/lib/nuggets/array/histogram_mixin.rb +169 -0
  25. data/lib/nuggets/array/in_order.rb +50 -0
  26. data/lib/nuggets/array/limit.rb +5 -0
  27. data/lib/nuggets/array/limit_mixin.rb +56 -0
  28. data/lib/nuggets/array/mean.rb +5 -0
  29. data/lib/nuggets/array/mean_mixin.rb +202 -0
  30. data/lib/nuggets/array/median.rb +5 -0
  31. data/lib/nuggets/array/median_mixin.rb +73 -0
  32. data/lib/nuggets/array/mode.rb +5 -0
  33. data/lib/nuggets/array/mode_mixin.rb +69 -0
  34. data/lib/nuggets/array/monotone.rb +83 -0
  35. data/lib/nuggets/array/only.rb +41 -0
  36. data/lib/nuggets/array/rand.rb +44 -0
  37. data/lib/nuggets/array/regression.rb +5 -0
  38. data/lib/nuggets/array/regression_mixin.rb +149 -0
  39. data/lib/nuggets/array/runiq.rb +5 -0
  40. data/lib/nuggets/array/runiq_mixin.rb +52 -0
  41. data/lib/nuggets/array/shuffle.rb +132 -0
  42. data/lib/nuggets/array/standard_deviation.rb +5 -0
  43. data/lib/nuggets/array/standard_deviation_mixin.rb +50 -0
  44. data/lib/nuggets/array/to_hash.rb +64 -0
  45. data/lib/nuggets/array/variance.rb +5 -0
  46. data/lib/nuggets/array/variance_mixin.rb +81 -0
  47. data/lib/nuggets/content_type.rb +97 -0
  48. data/lib/nuggets/dotted_decimal.rb +59 -0
  49. data/lib/nuggets/enumerable/agrep.rb +79 -0
  50. data/lib/nuggets/enumerable/all_any_extended.rb +66 -0
  51. data/lib/nuggets/enumerable/minmax.rb +102 -0
  52. data/lib/nuggets/env/set.rb +3 -0
  53. data/lib/nuggets/env/set_mixin.rb +67 -0
  54. data/lib/nuggets/env/user_encoding.rb +3 -0
  55. data/lib/nuggets/env/user_encoding_mixin.rb +54 -0
  56. data/lib/nuggets/env/user_home.rb +3 -0
  57. data/lib/nuggets/env/user_home_mixin.rb +56 -0
  58. data/lib/nuggets/file/ext.rb +5 -0
  59. data/lib/nuggets/file/ext_mixin.rb +87 -0
  60. data/lib/nuggets/file/replace.rb +5 -0
  61. data/lib/nuggets/file/replace_mixin.rb +55 -0
  62. data/lib/nuggets/file/sub.rb +5 -0
  63. data/lib/nuggets/file/sub_mixin.rb +95 -0
  64. data/lib/nuggets/file/which.rb +5 -0
  65. data/lib/nuggets/file/which_mixin.rb +72 -0
  66. data/lib/nuggets/hash/at.rb +68 -0
  67. data/lib/nuggets/hash/deep_fetch.rb +5 -0
  68. data/lib/nuggets/hash/deep_fetch_mixin.rb +74 -0
  69. data/lib/nuggets/hash/deep_merge.rb +5 -0
  70. data/lib/nuggets/hash/deep_merge_mixin.rb +53 -0
  71. data/lib/nuggets/hash/idmap.rb +5 -0
  72. data/lib/nuggets/hash/idmap_mixin.rb +41 -0
  73. data/lib/nuggets/hash/in_order.rb +43 -0
  74. data/lib/nuggets/hash/insert.rb +53 -0
  75. data/lib/nuggets/hash/nest.rb +5 -0
  76. data/lib/nuggets/hash/nest_mixin.rb +78 -0
  77. data/lib/nuggets/hash/only.rb +52 -0
  78. data/lib/nuggets/hash/seen.rb +5 -0
  79. data/lib/nuggets/hash/seen_mixin.rb +58 -0
  80. data/lib/nuggets/hash/unroll.rb +5 -0
  81. data/lib/nuggets/hash/unroll_mixin.rb +88 -0
  82. data/lib/nuggets/hash/zip.rb +5 -0
  83. data/lib/nuggets/hash/zip_mixin.rb +159 -0
  84. data/lib/nuggets/i18n.rb +155 -0
  85. data/lib/nuggets/integer/factorial.rb +56 -0
  86. data/lib/nuggets/integer/length.rb +5 -0
  87. data/lib/nuggets/integer/length_mixin.rb +49 -0
  88. data/lib/nuggets/integer/map.rb +5 -0
  89. data/lib/nuggets/integer/map_mixin.rb +42 -0
  90. data/lib/nuggets/integer/to_binary_s.rb +38 -0
  91. data/lib/nuggets/io/agrep.rb +43 -0
  92. data/lib/nuggets/io/interact.rb +5 -0
  93. data/lib/nuggets/io/interact_mixin.rb +159 -0
  94. data/lib/nuggets/io/modes.rb +121 -0
  95. data/lib/nuggets/io/null.rb +5 -0
  96. data/lib/nuggets/io/null_mixin.rb +40 -0
  97. data/lib/nuggets/io/redirect.rb +5 -0
  98. data/lib/nuggets/io/redirect_mixin.rb +50 -0
  99. data/lib/nuggets/lazy_attr.rb +44 -0
  100. data/lib/nuggets/log_parser.rb +70 -0
  101. data/lib/nuggets/log_parser/apache.rb +101 -0
  102. data/lib/nuggets/log_parser/rails.rb +219 -0
  103. data/lib/nuggets/net/success.rb +59 -0
  104. data/lib/nuggets/numeric/between.rb +2 -0
  105. data/lib/nuggets/numeric/duration.rb +100 -0
  106. data/lib/nuggets/numeric/limit.rb +62 -0
  107. data/lib/nuggets/numeric/signum.rb +52 -0
  108. data/lib/nuggets/numeric/to_multiple.rb +61 -0
  109. data/lib/nuggets/object/blank.rb +20 -0
  110. data/lib/nuggets/object/blank_mixin.rb +99 -0
  111. data/lib/nuggets/object/boolean.rb +5 -0
  112. data/lib/nuggets/object/boolean_mixin.rb +61 -0
  113. data/lib/nuggets/object/eigenclass.rb +2 -0
  114. data/lib/nuggets/object/ghost_class.rb +2 -0
  115. data/lib/nuggets/object/metaclass.rb +2 -0
  116. data/lib/nuggets/object/msend.rb +5 -0
  117. data/lib/nuggets/object/msend_mixin.rb +43 -0
  118. data/lib/nuggets/object/silence.rb +5 -0
  119. data/lib/nuggets/object/silence_mixin.rb +44 -0
  120. data/lib/nuggets/object/singleton_class.rb +5 -0
  121. data/lib/nuggets/object/singleton_class_mixin.rb +95 -0
  122. data/lib/nuggets/object/uniclass.rb +2 -0
  123. data/lib/nuggets/object/virtual_class.rb +2 -0
  124. data/lib/nuggets/pluggable.rb +91 -0
  125. data/lib/nuggets/proc/bind.rb +5 -0
  126. data/lib/nuggets/proc/bind_mixin.rb +51 -0
  127. data/lib/nuggets/range/quantile.rb +5 -0
  128. data/lib/nuggets/range/quantile_mixin.rb +42 -0
  129. data/lib/nuggets/ruby.rb +235 -0
  130. data/lib/nuggets/statistics.rb +12 -0
  131. data/lib/nuggets/statistics_mixins.rb +12 -0
  132. data/lib/nuggets/string/camelscore.rb +5 -0
  133. data/lib/nuggets/string/camelscore_mixin.rb +116 -0
  134. data/lib/nuggets/string/capitalize_first.rb +46 -0
  135. data/lib/nuggets/string/case.rb +81 -0
  136. data/lib/nuggets/string/evaluate.rb +5 -0
  137. data/lib/nuggets/string/evaluate_mixin.rb +47 -0
  138. data/lib/nuggets/string/msub.rb +84 -0
  139. data/lib/nuggets/string/nsub.rb +65 -0
  140. data/lib/nuggets/string/sub_with_md.rb +111 -0
  141. data/lib/nuggets/string/wc.rb +5 -0
  142. data/lib/nuggets/string/wc_mixin.rb +95 -0
  143. data/lib/nuggets/string/word_wrap.rb +76 -0
  144. data/lib/nuggets/string/xor.rb +5 -0
  145. data/lib/nuggets/string/xor_mixin.rb +59 -0
  146. data/lib/nuggets/tempfile/open.rb +57 -0
  147. data/lib/nuggets/uri/content_type.rb +5 -0
  148. data/lib/nuggets/uri/content_type_mixin.rb +47 -0
  149. data/lib/nuggets/uri/exist.rb +5 -0
  150. data/lib/nuggets/uri/exist_mixin.rb +56 -0
  151. data/lib/nuggets/uri/redirect.rb +5 -0
  152. data/lib/nuggets/uri/redirect_mixin.rb +101 -0
  153. data/lib/nuggets/version.rb +27 -0
  154. data/spec/nuggets/array/boost_spec.rb +50 -0
  155. data/spec/nuggets/array/combination_spec.rb +25 -0
  156. data/spec/nuggets/array/correlation_spec.rb +81 -0
  157. data/spec/nuggets/array/flatten_once_spec.rb +16 -0
  158. data/spec/nuggets/array/flush_spec.rb +43 -0
  159. data/spec/nuggets/array/format_spec.rb +52 -0
  160. data/spec/nuggets/array/hashify_spec.rb +41 -0
  161. data/spec/nuggets/array/histogram_spec.rb +87 -0
  162. data/spec/nuggets/array/in_order_spec.rb +13 -0
  163. data/spec/nuggets/array/limit_spec.rb +62 -0
  164. data/spec/nuggets/array/mean_spec.rb +203 -0
  165. data/spec/nuggets/array/median_spec.rb +77 -0
  166. data/spec/nuggets/array/mode_spec.rb +57 -0
  167. data/spec/nuggets/array/monotone_spec.rb +30 -0
  168. data/spec/nuggets/array/only_spec.rb +26 -0
  169. data/spec/nuggets/array/regression_spec.rb +54 -0
  170. data/spec/nuggets/array/runiq_spec.rb +25 -0
  171. data/spec/nuggets/array/standard_deviation_spec.rb +33 -0
  172. data/spec/nuggets/array/to_hash_spec.rb +28 -0
  173. data/spec/nuggets/array/variance_spec.rb +106 -0
  174. data/spec/nuggets/dotted_decimal_spec.rb +27 -0
  175. data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
  176. data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
  177. data/spec/nuggets/env/set_spec.rb +29 -0
  178. data/spec/nuggets/env/user_encoding_spec.rb +38 -0
  179. data/spec/nuggets/env/user_home_spec.rb +42 -0
  180. data/spec/nuggets/file/ext_spec.rb +38 -0
  181. data/spec/nuggets/file/replace_spec.rb +95 -0
  182. data/spec/nuggets/file/sub_spec.rb +149 -0
  183. data/spec/nuggets/file/which_spec.rb +22 -0
  184. data/spec/nuggets/hash/at_spec.rb +19 -0
  185. data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
  186. data/spec/nuggets/hash/deep_merge_spec.rb +78 -0
  187. data/spec/nuggets/hash/in_order_spec.rb +12 -0
  188. data/spec/nuggets/hash/insert_spec.rb +13 -0
  189. data/spec/nuggets/hash/nest_spec.rb +102 -0
  190. data/spec/nuggets/hash/only_spec.rb +29 -0
  191. data/spec/nuggets/hash/seen_spec.rb +36 -0
  192. data/spec/nuggets/hash/unroll_spec.rb +68 -0
  193. data/spec/nuggets/i18n_spec.rb +13 -0
  194. data/spec/nuggets/integer/factorial_spec.rb +10 -0
  195. data/spec/nuggets/integer/length_spec.rb +18 -0
  196. data/spec/nuggets/integer/map_spec.rb +19 -0
  197. data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
  198. data/spec/nuggets/numeric/duration_spec.rb +25 -0
  199. data/spec/nuggets/numeric/limit_spec.rb +16 -0
  200. data/spec/nuggets/numeric/signum_spec.rb +16 -0
  201. data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
  202. data/spec/nuggets/object/blank_spec.rb +34 -0
  203. data/spec/nuggets/object/boolean_spec.rb +23 -0
  204. data/spec/nuggets/object/msend_spec.rb +25 -0
  205. data/spec/nuggets/object/silence_spec.rb +36 -0
  206. data/spec/nuggets/object/singleton_class_spec.rb +51 -0
  207. data/spec/nuggets/proc/bind_spec.rb +28 -0
  208. data/spec/nuggets/range/quantile_spec.rb +33 -0
  209. data/spec/nuggets/string/camelscore_spec.rb +114 -0
  210. data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
  211. data/spec/nuggets/string/case_spec.rb +31 -0
  212. data/spec/nuggets/string/evaluate_spec.rb +24 -0
  213. data/spec/nuggets/string/msub_spec.rb +20 -0
  214. data/spec/nuggets/string/nsub_spec.rb +13 -0
  215. data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
  216. data/spec/nuggets/string/wc_spec.rb +73 -0
  217. data/spec/nuggets/string/word_wrap_spec.rb +81 -0
  218. data/spec/nuggets/string/xor_spec.rb +57 -0
  219. data/spec/nuggets/uri/content_type_spec.rb +42 -0
  220. data/spec/nuggets/uri/exist_spec.rb +49 -0
  221. data/spec/spec_helper.rb +36 -0
  222. metadata +309 -17
@@ -0,0 +1,149 @@
1
+ require 'nuggets/file/sub'
2
+ require 'nuggets/tempfile/open'
3
+
4
+ describe_extended File, Nuggets::File::SubMixin, true do
5
+
6
+ before :each do
7
+ @txt = <<-EOT.freeze
8
+ The fox went out on a chilly night
9
+ Prayed for the moon to give him light
10
+ For he had many a mile to go that night
11
+ Before he reached the town o
12
+ EOT
13
+
14
+ @res1 = <<-EOT.freeze
15
+ The fox went out on a chilly nXIGHTX
16
+ Prayed for the moon to give him light
17
+ For he had many a mile to go that night
18
+ Before he reached the town o
19
+ EOT
20
+
21
+ @res2 = <<-EOT.freeze
22
+ The fox went out on a chilly nXIGHTX
23
+ Prayed for the moon to give him lXIGHTX
24
+ For he had many a mile to go that nXIGHTX
25
+ Before he reached the town o
26
+ EOT
27
+ end
28
+
29
+ example do
30
+ tempfile { |path|
31
+ res = File.sub(path, /ight/, 'XIGHTX')
32
+ res.should == @res1
33
+ @txt.should_not == @res1
34
+ File.read(path).should == @txt
35
+ }
36
+ end
37
+
38
+ example do
39
+ tempfile { |path|
40
+ res = File.sub(path, /XightX/, 'xIGHTx')
41
+ res.should == @txt
42
+ File.read(path).should == @txt
43
+ }
44
+ end
45
+
46
+ example do
47
+ tempfile { |path|
48
+ res = File.sub(path, /ight/) { |m| "X#{m.upcase}X" }
49
+ res.should == @res1
50
+ @txt.should_not == @res1
51
+ File.read(path).should == @txt
52
+ }
53
+ end
54
+
55
+ example do
56
+ tempfile { |path|
57
+ res = File.sub(path, /XightX/) { |m| "x#{m.upcase}x" }
58
+ res.should == @txt
59
+ File.read(path).should == @txt
60
+ }
61
+ end
62
+
63
+ example do
64
+ tempfile { |path|
65
+ res = File.gsub(path, /ight/, 'XIGHTX')
66
+ res.should == @res2
67
+ @txt.should_not == @res2
68
+ File.read(path).should == @txt
69
+ }
70
+ end
71
+
72
+ example do
73
+ tempfile { |path|
74
+ res = File.gsub(path, /ight/) { |m| "X#{m.upcase}X" }
75
+ res.should == @res2
76
+ @txt.should_not == @res2
77
+ File.read(path).should == @txt
78
+ }
79
+ end
80
+
81
+ example do
82
+ tempfile { |path|
83
+ res = File.sub!(path, /ight/, 'XIGHTX')
84
+ res.should == @res1
85
+ @txt.should_not == @res1
86
+ File.read(path).should == @res1
87
+ }
88
+ end
89
+
90
+ example do
91
+ tempfile { |path|
92
+ res = File.sub!(path, /XightX/, 'xIGHTx')
93
+ res.should be_nil
94
+ File.read(path).should == @txt
95
+ }
96
+ end
97
+
98
+ example do
99
+ tempfile { |path|
100
+ res = File.sub!(path, /ight/) { |m| "X#{m.upcase}X" }
101
+ res.should == @res1
102
+ @txt.should_not == @res1
103
+ File.read(path).should == @res1
104
+ }
105
+ end
106
+
107
+ example do
108
+ tempfile { |path|
109
+ res = File.sub!(path, /XightX/) { |m| "x#{m.upcase}x" }
110
+ res.should be_nil
111
+ File.read(path).should == @txt
112
+ }
113
+ end
114
+
115
+ example do
116
+ tempfile { |path|
117
+ res = File.gsub!(path, /ight/, 'XIGHTX')
118
+ res.should == @res2
119
+ @txt.should_not == @res2
120
+ File.read(path).should == @res2
121
+ }
122
+ end
123
+
124
+ example do
125
+ tempfile { |path|
126
+ res = File.gsub!(path, /ight/) { |m| "X#{m.upcase}X" }
127
+ res.should == @res2
128
+ @txt.should_not == @res2
129
+ File.read(path).should == @res2
130
+ }
131
+ end
132
+
133
+ example do
134
+ lambda { File.sub(tempfile, /ight/, 'XIGHTX') }.should raise_error(Errno::ENOENT)
135
+ end
136
+
137
+ example do
138
+ lambda { File.sub!(tempfile, /ight/, 'XIGHTX') }.should raise_error(Errno::ENOENT)
139
+ end
140
+
141
+ example do
142
+ lambda { File.gsub(tempfile, /ight/, 'XIGHTX') }.should raise_error(Errno::ENOENT)
143
+ end
144
+
145
+ example do
146
+ lambda { File.gsub!(tempfile, /ight/, 'XIGHTX') }.should raise_error(Errno::ENOENT)
147
+ end
148
+
149
+ end
@@ -0,0 +1,22 @@
1
+ require 'nuggets/file/which'
2
+
3
+ describe_extended File, Nuggets::File::WhichMixin, true do
4
+
5
+ %w[cat dog rat gcc /usr/bin/X11/gcc].each { |c|
6
+ example do
7
+ r = %x{which #{c}}.chomp
8
+ File.which(c).should == (r.empty? ? nil : r)
9
+ end
10
+ }
11
+
12
+ example do
13
+ c = [
14
+ 'unison --args source target',
15
+ 'rsync --args source target',
16
+ 'scp --args source target'
17
+ ]
18
+ r = c.find { |s| !%x{which #{s[/\S+/]}}.chomp.empty? }
19
+ File.which_command(c).should == r
20
+ end
21
+
22
+ end
@@ -0,0 +1,19 @@
1
+ require 'nuggets/hash/at'
2
+
3
+ describe Hash, 'at' do
4
+
5
+ let(:h) { { :a => 1, 2 => 3, nil => nil, 'foo' => %w[b a r] } }
6
+
7
+ example { h.first.should == { a: 1 } }
8
+
9
+ example { h.last.should == { 'foo' => %w[b a r] } }
10
+
11
+ example { h.rand.should be_a(Hash) }
12
+
13
+ example { h.at(0).should == { a: 1 } }
14
+
15
+ example { h.at(1).should == { 2 => 3 } }
16
+
17
+ example { h.at(-1).should == { 'foo' => %w[b a r] } }
18
+
19
+ end
@@ -0,0 +1,159 @@
1
+ require 'nuggets/hash/deep_fetch'
2
+
3
+ describe_extended Hash, Nuggets::Hash::DeepFetchMixin do
4
+
5
+ example do
6
+ lambda { {}.deep_fetch('') }.should raise_error(ArgumentError, 'no keys given')
7
+ end
8
+
9
+ context do
10
+
11
+ let(:hash) { { 'foo' => { 'bar' => { 'baz' => 42 }, 'bay' => 23 } } }
12
+
13
+ example do
14
+ hash.deep_fetch('foo/bar/baz').should == 42
15
+ end
16
+
17
+ example do
18
+ hash.deep_fetch('foo/bar/bax').should == nil
19
+ end
20
+
21
+ example do
22
+ lambda { hash.deep_fetch('foo/bax/baz') }.should raise_error(KeyError)
23
+ end
24
+
25
+ example do
26
+ lambda { hash.deep_fetch('foo/bay/baz') }.should raise_error(TypeError)
27
+ end
28
+
29
+ example do
30
+ hash.deep_fetch('foo:bar:baz').should == nil
31
+ end
32
+
33
+ example do
34
+ hash.deep_fetch('foo:bar:baz', ':').should == 42
35
+ end
36
+
37
+ example do
38
+ hash.deep_fetch(%w[foo bar baz]).should == 42
39
+ end
40
+
41
+ example do
42
+ lambda { hash.deep_fetch([:foo, :bar, :baz]) }.should raise_error(KeyError)
43
+ end
44
+
45
+ example do
46
+ (hash % 'foo/bar/baz').should == 42
47
+ end
48
+
49
+ example do
50
+ (hash % %w[foo bar baz]).should == 42
51
+ end
52
+
53
+ end
54
+
55
+ context do
56
+
57
+ let(:hash) { { :foo => { :bar => { :baz => 42 } } } }
58
+
59
+ example do
60
+ lambda { hash.deep_fetch('foo/bar/baz') }.should raise_error(KeyError)
61
+ end
62
+
63
+ example do
64
+ hash.deep_fetch('foo/bar/baz', &:to_sym).should == 42
65
+ end
66
+
67
+ example do
68
+ hash.deep_fetch('foo:bar:baz', &:to_sym).should == nil
69
+ end
70
+
71
+ example do
72
+ hash.deep_fetch('foo:bar:baz', ':', &:to_sym).should == 42
73
+ end
74
+
75
+ example do
76
+ lambda { hash.deep_fetch(%w[foo bar baz]) }.should raise_error(KeyError)
77
+ end
78
+
79
+ example do
80
+ hash.deep_fetch([:foo, :bar, :baz]).should == 42
81
+ end
82
+
83
+ example do
84
+ lambda { hash % 'foo/bar/baz' }.should raise_error(KeyError)
85
+ end
86
+
87
+ example do
88
+ (hash % [:foo, :bar, :baz]).should == 42
89
+ end
90
+
91
+ end
92
+
93
+ context do
94
+
95
+ let(:hash) { { 1 => { 2 => { 3 => 42 } } } }
96
+
97
+ example do
98
+ lambda { hash.deep_fetch('1/2/3') }.should raise_error(KeyError)
99
+ end
100
+
101
+ example do
102
+ hash.deep_fetch('1/2/3', &:to_i).should == 42
103
+ end
104
+
105
+ example do
106
+ hash.deep_fetch('1:2:3', &:to_i).should == { 2 => { 3 => 42 } }
107
+ end
108
+
109
+ example do
110
+ hash.deep_fetch('1:2:3', ':', &:to_i).should == 42
111
+ end
112
+
113
+ example do
114
+ lambda { hash.deep_fetch(%w[1 2 3]) }.should raise_error(KeyError)
115
+ end
116
+
117
+ example do
118
+ hash.deep_fetch([1, 2, 3]).should == 42
119
+ end
120
+
121
+ example do
122
+ lambda { hash % '1/2/3' }.should raise_error(KeyError)
123
+ end
124
+
125
+ example do
126
+ (hash % [1, 2, 3]).should == 42
127
+ end
128
+
129
+ end
130
+
131
+ context do
132
+
133
+ let(:hash) { { 'foo' => Hash.new(0) } }
134
+
135
+ example do
136
+ hash.deep_fetch('foo/bar').should == 0
137
+ end
138
+
139
+ example do
140
+ lambda { hash.deep_fetch('foo/bar/baz') }.should raise_error(KeyError)
141
+ end
142
+
143
+ end
144
+
145
+ context do
146
+
147
+ let(:hash) { { 'foo' => Hash.new { |h, k| h[k] = [] } } }
148
+
149
+ example do
150
+ hash.deep_fetch('foo/bar').should == []
151
+ end
152
+
153
+ example do
154
+ lambda { hash.deep_fetch('foo/bar/baz') }.should raise_error(KeyError)
155
+ end
156
+
157
+ end
158
+
159
+ end
@@ -0,0 +1,78 @@
1
+ require 'nuggets/hash/deep_merge'
2
+
3
+ describe_extended Hash, Nuggets::Hash::DeepMergeMixin do
4
+
5
+ example do
6
+ { :a => 1 }.deep_merge(:a => 2).should == { :a => 2 }
7
+ end
8
+
9
+ example do
10
+ { :a => 1 }.deep_merge(:b => 2).should == { :a => 1, :b => 2 }
11
+ end
12
+
13
+ example do
14
+ { :a => { :b => 1 } }.deep_merge(:b => 2).should == { :a => { :b => 1 }, :b => 2 }
15
+ end
16
+
17
+ example do
18
+ { :a => { :b => 1 } }.deep_merge(:a => { :b => 2 }).should == { :a => { :b => 2 } }
19
+ end
20
+
21
+ example do
22
+ lambda { { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => 2 }) }.should raise_error(TypeError)
23
+ end
24
+
25
+ example do
26
+ { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => { :c => 2 } }).should == { :a => { :b => { :c => 2 } } }
27
+ end
28
+
29
+ example do
30
+ { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => { :d => 2 } }).should == { :a => { :b => { :c => 1, :d => 2 } } }
31
+ end
32
+
33
+ example do
34
+ { :a => { :b => { :c => 1 } } }.deep_merge(:a => { :b => { :c => 2 }, :d => 3 }).should == { :a => { :b => { :c => 2 }, :d => 3 } }
35
+ end
36
+
37
+ example do
38
+ { :a => { :b => { :c => 1 }, :d => 2 } }.deep_merge(:a => { :b => { :c => 2 }, :d => 3 }).should == { :a => { :b => { :c => 2 }, :d => 3 } }
39
+ end
40
+
41
+ example do
42
+ { :a => { :b => { :c => 1 }, :d => 2 }, :e => 3 }.deep_merge(:a => { :b => { :c => 2 }, :d => 3 }).should == { :a => { :b => { :c => 2 }, :d => 3 }, :e => 3 }
43
+ end
44
+
45
+ example do
46
+ { :a => { :b => { :c => 1 } }, :d => { :e => 3 } }.deep_merge(:a => { :b => { :c => 2 } }, :d => { :e => 4 }).should == { :a => { :b => { :c => 2 } }, :d => { :e => 4 } }
47
+ end
48
+
49
+ context do
50
+
51
+ before :each do
52
+ @sub_hash1 = { :b => 1 }
53
+ @sub_hash2 = { :b => 2 }
54
+
55
+ @hash1 = { :a => @sub_hash1 }
56
+ @hash2 = { :a => @sub_hash2 }
57
+ @hash3 = { :a => { :b => 2} }
58
+ end
59
+
60
+ example do
61
+ @hash1.deep_merge(@hash2).should == @hash3
62
+ @hash1.should_not == @hash3
63
+
64
+ @sub_hash1[:b].should == 1
65
+ @sub_hash2[:b].should == 2
66
+ end
67
+
68
+ example do
69
+ @hash1.deep_merge!(@hash2).should == @hash3
70
+ @hash1.should == @hash3
71
+
72
+ @sub_hash1[:b].should == 1
73
+ @sub_hash2[:b].should == 2
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,12 @@
1
+ require 'nuggets/hash/in_order'
2
+
3
+ describe Hash, 'in_order' do
4
+
5
+ example {
6
+ h = { a: 1, b: 2, c: 3 }
7
+
8
+ h.in_order(:b, :c).should == [[:b, 2], [:c, 3], [:a, 1]]
9
+ h.in_order(:b, :d).should == [[:b, 2], [:a, 1], [:c, 3]]
10
+ }
11
+
12
+ end
@@ -0,0 +1,13 @@
1
+ require 'nuggets/hash/insert'
2
+
3
+ describe Hash, 'insert' do
4
+
5
+ let(:h) { { a: 0, b: { b1: 1, b2: 2 } } }
6
+
7
+ example { h.insert(a: -1).should == { a: -1, b: { b1: 1, b2: 2 } } }
8
+
9
+ example { h.insert(b: { b3: 3 }).should == { a: 0, b: { b1: 1, b2: 2, b3: 3 } } }
10
+
11
+ example { h.insert!(b: { b0: 0 }); h.should == { a: 0, b: { b1: 1, b2: 2, b0: 0 } } }
12
+
13
+ end