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,25 @@
1
+ require 'nuggets/array/runiq'
2
+
3
+ describe_extended Array, Nuggets::Array::RuniqMixin do
4
+
5
+ example do
6
+ [1, 2, 3, 4, 3, 2].runiq.should == [1, 4, 3, 2]
7
+ end
8
+
9
+ example do
10
+ ary = [1, 2, 3, 4, 3, 2]
11
+ ary.runiq!.should_not be_nil
12
+ ary.should == [1, 4, 3, 2]
13
+ end
14
+
15
+ example do
16
+ [1, 2, 3, 4].runiq.should == [1, 2, 3, 4]
17
+ end
18
+
19
+ example do
20
+ ary = [1, 2, 3, 4]
21
+ ary.runiq!.should be_nil
22
+ ary.should == [1, 2, 3, 4]
23
+ end
24
+
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'nuggets/array/standard_deviation'
2
+
3
+ describe_extended Array, Nuggets::Array::StandardDeviationMixin do
4
+
5
+ example do
6
+ [].standard_deviation.should == 0.0
7
+ end
8
+
9
+ example do
10
+ [1].standard_deviation.should == 0.0
11
+ end
12
+
13
+ example do
14
+ [1, 1, 1].standard_deviation.should == 0.0
15
+ end
16
+
17
+ example do
18
+ [1, 2, 3].standard_deviation.should equal_float(0.816496580927726)
19
+ end
20
+
21
+ example do
22
+ [3, 2, 1].standard_deviation.should equal_float(0.816496580927726)
23
+ end
24
+
25
+ example do
26
+ [-3, -2, -1].standard_deviation.should == [3, 2, 1].standard_deviation
27
+ end
28
+
29
+ example do
30
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].standard_deviation.should equal_float(6.48804088737217)
31
+ end
32
+
33
+ end
@@ -0,0 +1,28 @@
1
+ require 'nuggets/array/to_hash'
2
+
3
+ describe Array, 'to_hash' do
4
+
5
+ example {
6
+ a = [[:a, 1], [:b, 2], [:c, 3]]
7
+ a.to_h.should == { a: 1, b: 2, c: 3 }
8
+ }
9
+
10
+ example {
11
+ a = [[:a, [1, 2]], [:b, 3], [[:c, :d], [4, [5, 6]]]]
12
+ a.to_h.should == { a: [1, 2], b: 3, [:c, :d] => [4, [5, 6]] }
13
+ }
14
+
15
+ example {
16
+ a = %w[a b c d]
17
+
18
+ a.to_h.should == { 'a' => 'b', 'c' => 'd' }
19
+ a.to_h(1).should == { 'a' => 1, 'b' => 1, 'c' => 1, 'd' => 1 }
20
+ a.to_h { nil }.should == { 'a' => nil, 'b' => nil, 'c' => nil, 'd' => nil }
21
+ }
22
+
23
+ example {
24
+ h = { a: 1, b: [2, 3], c: { d: 4 } }
25
+ h.to_a.to_h.should == h
26
+ }
27
+
28
+ end
@@ -0,0 +1,106 @@
1
+ require 'nuggets/array/variance'
2
+
3
+ describe_extended Array, Nuggets::Array::VarianceMixin do
4
+
5
+ example do
6
+ [].variance.should == 0.0
7
+ end
8
+
9
+ example do
10
+ [1].variance.should == 0.0
11
+ end
12
+
13
+ example do
14
+ [1, 1, 1].variance.should == 0.0
15
+ end
16
+
17
+ example do
18
+ [1, 2, 3].variance.should == 2 / 3.0
19
+ end
20
+
21
+ example do
22
+ [3, 2, 1].variance.should == 2 / 3.0
23
+ end
24
+
25
+ example do
26
+ [-3, -2, -1].variance.should == [3, 2, 1].variance
27
+ end
28
+
29
+ example do
30
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].variance.should equal_float(42.0946745562130218)
31
+ end
32
+
33
+ example do
34
+ [1, 2, 4, 5, 8].variance.should == 6.0
35
+ end
36
+
37
+ example do
38
+ [1, 2, 4, 5, 8].variance { |i| i + 1e7 }.should == 6.0
39
+ end
40
+
41
+ xexample do
42
+ [1, 2, 4, 5, 8].variance { |i| i + 1e12 }.should == 6.0
43
+ end
44
+
45
+ example do
46
+ ([1, 2, 4, 5, 8] * 100).variance.should == 6.0
47
+ end
48
+
49
+ example do
50
+ [].covariance.should == 0.0
51
+ end
52
+
53
+ example do
54
+ [1].covariance.should == 0.0
55
+ end
56
+
57
+ example do
58
+ [1, 1, 1].covariance.should == 0.0
59
+ end
60
+
61
+ example do
62
+ [1, 2, 3].covariance.should == 2 / 3.0
63
+ end
64
+
65
+ example do
66
+ [3, 2, 1].covariance.should == -2 / 3.0
67
+ end
68
+
69
+ example do
70
+ [-3, -2, -1].covariance.should == -[3, 2, 1].covariance
71
+ end
72
+
73
+ example do
74
+ [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].covariance.should equal_float(11.615384615384615)
75
+ end
76
+
77
+ example do
78
+ a = [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24]
79
+ a.zip(Array.new(a.size) { 42 }).covariance.should == 0.0
80
+ end
81
+
82
+ example do
83
+ a = [1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24]
84
+ a.zip(a).covariance.should == a.variance
85
+ end
86
+
87
+ example do
88
+ a, b = [1, -2, 1, 2, 3, -4], [3, 1, 2, 1, 0, 24]
89
+ a.zip(b).covariance.should == b.zip(a).covariance
90
+ end
91
+
92
+ example do
93
+ a, b = [1, -2, 1, 2, 3, -4], [3, 1, 2, 1, 0, 24]
94
+ m, n = 23, 42
95
+
96
+ a.map { |i| m * i }.zip(b.map { |i| n * i }).covariance.should == m * n * a.zip(b).covariance
97
+ end
98
+
99
+ example do
100
+ a, b = [1, -2, 1, 2, 3, -4], [3, 1, 2, 1, 0, 24]
101
+ m, n = 23, 42
102
+
103
+ a.map { |i| i + m }.zip(b.map { |i| i + n }).covariance.should equal_float(a.zip(b).covariance, 1.0e-13)
104
+ end
105
+
106
+ end
@@ -0,0 +1,27 @@
1
+ require 'nuggets/dotted_decimal'
2
+
3
+ addr = {
4
+ '77.47.161.3' => 1294967043,
5
+ '77.47.161.11' => 1294967051,
6
+ '136.202.107.2' => 2294967042,
7
+ '196.101.53.1' => 3294967041,
8
+ '255.255.255.0' => 4294967040
9
+ }
10
+
11
+ describe Integer, 'dotted_decimal' do
12
+
13
+ addr.each { |s, i| example { i.to_dotted_decimal.should == s } }
14
+
15
+ end
16
+
17
+ describe String, 'dotted_decimal' do
18
+
19
+ addr.each { |s, i| example { s.from_dotted_decimal.should == i } }
20
+
21
+ end
22
+
23
+ describe Array, 'dotted_decimal' do
24
+
25
+ example { addr.keys.sort_by_dotted_decimal.should == addr.sort_by { |k, v| v }.map(&:first) }
26
+
27
+ end
@@ -0,0 +1,31 @@
1
+ require 'nuggets/enumerable/all_any_extended'
2
+
3
+ describe Enumerable, 'all_any_extended' do
4
+
5
+ example {
6
+ e = %w[quux quuux quix]
7
+
8
+ e.all?(String).should == true
9
+ e.any?(Numeric).should == false
10
+ }
11
+
12
+ example {
13
+ e = [:one, 'c', nil, 88]
14
+
15
+ e.all?(Object).should == true
16
+ e.any?(NilClass).should == true
17
+ }
18
+
19
+ example {
20
+ e = [0, 10, 20]
21
+
22
+ e.any?(9..99).should == true
23
+ e.any?(9, :<).should == true
24
+ e.any? { |i| i < 9 }.should == true
25
+ }
26
+
27
+ example {
28
+ lambda { [].any?(NilClass, &:nil?) }.should raise_error(ArgumentError, 'both block and object argument given')
29
+ }
30
+
31
+ end
@@ -0,0 +1,21 @@
1
+ require 'nuggets/enumerable/minmax'
2
+
3
+ describe Enumerable, 'minmax' do
4
+
5
+ example {
6
+ e = %w[quux quuux quix]
7
+
8
+ e.max.should == 'quux'
9
+ e.max_by(:length).should == 'quuux'
10
+ e.max(:length).should == 5
11
+ }
12
+
13
+ example {
14
+ e, b = [3, 222, 45], lambda { |i| i % 10 }
15
+
16
+ e.max.should == 222
17
+ e.max_by(b).should == 45
18
+ e.max(b).should == 5
19
+ }
20
+
21
+ end
@@ -0,0 +1,29 @@
1
+ require 'nuggets/env/set'
2
+
3
+ describe_extended ENV, Nuggets::Env::SetMixin, true do
4
+
5
+ before do
6
+ @original = ENV.to_hash
7
+ end
8
+
9
+ after do
10
+ ENV.clear
11
+ ENV.update(@original)
12
+ end
13
+
14
+ example do
15
+ ENV.with(:lang => 'C') { ENV['LANG'].should == 'C' }
16
+ ENV['LANG'].should == @original['LANG']
17
+ end
18
+
19
+ example do
20
+ ENV.set(:lang => 'C').to_hash.should == @original
21
+ ENV.to_hash.should == { 'LANG' => 'C' }
22
+ end
23
+
24
+ example do
25
+ ENV.set(@original)
26
+ ENV.to_hash.should == @original
27
+ end
28
+
29
+ end
@@ -0,0 +1,38 @@
1
+ require 'nuggets/env/user_encoding'
2
+
3
+ describe_extended ENV, Nuggets::Env::UserEncodingMixin, true do
4
+
5
+ before do
6
+ @old_env = ENV.to_hash
7
+ end
8
+
9
+ after do
10
+ ENV.clear
11
+ ENV.update(@old_env)
12
+ end
13
+
14
+ example do
15
+ ENV.user_encoding.should be_an_instance_of(String)
16
+ end
17
+
18
+ example do
19
+ ENV.clear
20
+ ENV.user_encoding.should be_an_instance_of(String)
21
+ end
22
+
23
+ example do
24
+ ENV['ENCODING'] = 'foo'
25
+ ENV.user_encoding.should == 'foo'
26
+ end
27
+
28
+ example do
29
+ ENV.clear
30
+ ENV.user_encoding('bar').should == 'bar'
31
+ end
32
+
33
+ example do
34
+ ENV.clear
35
+ ENV.user_encoding(nil).should be_nil
36
+ end
37
+
38
+ end
@@ -0,0 +1,42 @@
1
+ require 'nuggets/env/user_home'
2
+
3
+ describe_extended ENV, Nuggets::Env::UserHomeMixin, true do
4
+
5
+ before do
6
+ @old_env = ENV.to_hash
7
+ end
8
+
9
+ after do
10
+ ENV.clear
11
+ ENV.update(@old_env)
12
+ end
13
+
14
+ example do
15
+ ENV.user_home.should be_an_instance_of(String)
16
+ end
17
+
18
+ example do
19
+ ENV.clear
20
+ ENV.user_home.should be_an_instance_of(String)
21
+ end
22
+
23
+ example do
24
+ ENV['HOME'] = 'foo'
25
+ ENV.user_home.should == 'foo'
26
+ end
27
+
28
+ unless RUBY_PLATFORM == 'java'
29
+
30
+ example do
31
+ ENV.clear
32
+ ENV.user_home('bar').should == 'bar'
33
+ end
34
+
35
+ example do
36
+ ENV.clear
37
+ ENV.user_home(nil).should be_nil
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,38 @@
1
+ require 'nuggets/file/ext'
2
+
3
+ describe_extended File, Nuggets::File::ExtMixin, true do
4
+
5
+ [
6
+ ['foo', nil, '.baz'],
7
+ %w[foo.bar foo .baz],
8
+ %w[foo.bar.baz foo.bar .baz]
9
+ ].each { |path, new_path, new_ext|
10
+ example {
11
+ File.chomp_ext(path).should == (new_path || path)
12
+ }
13
+
14
+ example {
15
+ File.chomp_ext!(_path = path.dup).should == new_path
16
+ _path.should == (new_path || path)
17
+ }
18
+
19
+ example {
20
+ File.sub_ext(path, new_ext).should == (new_path ? "#{new_path}#{new_ext}" : path)
21
+ }
22
+
23
+ example {
24
+ File.sub_ext!(_path = path.dup, new_ext).should == (new_path && "#{new_path}#{new_ext}")
25
+ _path.should == (new_path ? "#{new_path}#{new_ext}" : path)
26
+ }
27
+
28
+ example {
29
+ File.set_ext(path, new_ext).should == "#{new_path || path}#{new_ext}"
30
+ }
31
+
32
+ example {
33
+ File.set_ext!(_path = path.dup, new_ext).should == "#{new_path || path}#{new_ext}"
34
+ _path.should == "#{new_path || path}#{new_ext}"
35
+ }
36
+ }
37
+
38
+ end
@@ -0,0 +1,95 @@
1
+ require 'nuggets/file/replace'
2
+ require 'nuggets/tempfile/open'
3
+
4
+ describe_extended File, Nuggets::File::ReplaceMixin, 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
+ He ran til he came to a great big bin
16
+ Where the ducks and the geese were kept therein
17
+ Said, a couple of you are going to grease my chin
18
+ Before I leave this town o
19
+ EOT
20
+
21
+ @res2 = <<-EOT.freeze
22
+ THE FOX WENT OUT ON A CHILLY NIGHT
23
+ PRAYED FOR THE MOON TO GIVE HIM LIGHT
24
+ FOR HE HAD MANY A MILE TO GO THAT NIGHT
25
+ BEFORE HE REACHED THE TOWN O
26
+ EOT
27
+ end
28
+
29
+ example do
30
+ tempfile { |path|
31
+ res = File.replace(path) { @res1 }
32
+ res.should == @res1
33
+ File.read(path).should == @res1
34
+ }
35
+ end
36
+
37
+ example do
38
+ tempfile { |path|
39
+ res = File.replace(path) { |content| content }
40
+ res.should == @txt
41
+ File.read(path).should == @txt
42
+ }
43
+ end
44
+
45
+ example do
46
+ tempfile { |path|
47
+ res = File.replace(path) { |content| @res1 }
48
+ res.should == @res1
49
+ File.read(path).should == @res1
50
+ }
51
+ end
52
+
53
+ example do
54
+ tempfile { |path|
55
+ res = File.replace(path) { |content| content.upcase }
56
+ res.should == @res2
57
+ File.read(path).should == @res2
58
+ }
59
+ end
60
+
61
+ example do
62
+ lambda { File.replace(tempfile) { @res1 } }.should raise_error(Errno::ENOENT)
63
+ end
64
+
65
+ example do
66
+ begin
67
+ res = File.replace(path = tempfile, true) { @res1 }
68
+ res.should == @res1
69
+ File.read(path).should == @res1
70
+ ensure
71
+ File.unlink(path) if path
72
+ end
73
+ end
74
+
75
+ example do
76
+ begin
77
+ res = File.replace(path = tempfile, true) { |content| content }
78
+ res.should == ''
79
+ File.read(path).should == ''
80
+ ensure
81
+ File.unlink(path) if path
82
+ end
83
+ end
84
+
85
+ example do
86
+ begin
87
+ res = File.replace(path = tempfile, true) { |content| @res1 }
88
+ res.should == @res1
89
+ File.read(path).should == @res1
90
+ ensure
91
+ File.unlink(path) if path
92
+ end
93
+ end
94
+
95
+ end