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,5 @@
1
+ require 'nuggets/hash/unroll_mixin'
2
+
3
+ class Hash
4
+ include Nuggets::Hash::UnrollMixin
5
+ end
@@ -0,0 +1,88 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ class Hash
29
+ module UnrollMixin
30
+
31
+ # call-seq:
32
+ # hash.unroll(*value_keys) => anArray
33
+ # hash.unroll(*value_keys, :sort_by => ...) => anArray
34
+ # hash.unroll(*value_keys) { |value_hash| ... } => anArray
35
+ #
36
+ # "Unrolls" a nested hash, so that each path through _hash_ results in a
37
+ # row that is, e.g., suitable for use with CSV.
38
+ #
39
+ # Note that from the final hash ("value hash") only the values are used,
40
+ # namely, if +value_keys+ are given, the values at those keys are returned.
41
+ # If a block is given, the +value_hash+ is passed to that block for any
42
+ # additional processing or sanitization.
43
+ #
44
+ # If +sort_by+ is given, all hashes are passed through that block for
45
+ # sorting before being put into the result array.
46
+ #
47
+ # Examples:
48
+ #
49
+ # { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll
50
+ # #=> [[:foo, :bar, :b, 3, 0], [:foo, :bar, :a, 2, 1]]
51
+ #
52
+ # { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll(:sort_by => :to_s)
53
+ # #=> [[:foo, :bar, :a, 1, 2], [:foo, :bar, :b, 0, 3]]
54
+ #
55
+ # { :foo => { :bar => { :a => { :x => 1, :y => 2 }, :b => { :x => 0, :y => 3 } } } }.unroll { |data| data[:x] = nil; data[:y] *= 2 }
56
+ # #=> [[:foo, :bar, :b, 6, nil], [:foo, :bar, :a, 4, nil]]
57
+ def unroll(*value_keys, &block)
58
+ args = value_keys.dup
59
+ options = value_keys.last.is_a?(::Hash) ? value_keys.pop : {}
60
+
61
+ sort_proc = if options.key?(:sort_by)
62
+ lambda { sort_by(&options[:sort_by]) }
63
+ elsif options.key?(:sort)
64
+ options[:sort] == true ? lambda { sort } : lambda { sort(&options[:sort]) }
65
+ end
66
+
67
+ rows = []
68
+
69
+ if values.first.is_a?(self.class) # if any is, then all are
70
+ (sort_proc ? sort_proc.call : self).each { |key, value|
71
+ value.unroll(*args, &block).each { |row| rows << [key, *row] }
72
+ }
73
+ else
74
+ block[self] if block
75
+
76
+ rows << if value_keys.empty?
77
+ sort_proc ? sort_proc.call.map { |key, value| value } : values
78
+ else
79
+ values_at(*value_keys)
80
+ end
81
+ end
82
+
83
+ rows
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ require 'nuggets/hash/zip_mixin'
2
+
3
+ class Hash
4
+ extend Nuggets::Hash::ZipMixin
5
+ end
@@ -0,0 +1,159 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2014 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ require 'zlib'
28
+
29
+ module Nuggets
30
+ class Hash
31
+ module ZipMixin
32
+
33
+ def zip(*args, &block)
34
+ ZipHash.new(*args, &block)
35
+ end
36
+
37
+ def zipval(*args, &block)
38
+ ZipValHash.new(*args, &block)
39
+ end
40
+
41
+ def zipkey(*args, &block)
42
+ ZipKeyHash.new(*args, &block)
43
+ end
44
+
45
+ class ZipHash < ::Hash
46
+
47
+ def [](key)
48
+ unzipval(super(zipkey(key)))
49
+ end
50
+
51
+ def []=(key, value)
52
+ super(zipkey(key), zipval(value))
53
+ end
54
+
55
+ def fetch(key, *args)
56
+ unzipval(super(zipkey(key), *args))
57
+ end
58
+
59
+ def store(key, value)
60
+ super(zipkey(key), zipval(value))
61
+ end
62
+
63
+ private
64
+
65
+ def zipval(value)
66
+ value.is_a?(ZipVal) ? value : ZipVal.new(value)
67
+ end
68
+
69
+ def unzipval(value)
70
+ value.is_a?(ZipVal) ? value.to_s : value
71
+ end
72
+
73
+ def zipkey(key)
74
+ key.is_a?(ZipKey) ? key : ZipKey.new(key)
75
+ end
76
+
77
+ def unzipkey(key)
78
+ key.is_a?(ZipKey) ? key.to_s : key
79
+ end
80
+
81
+ end
82
+
83
+ class ZipValHash < ZipHash
84
+
85
+ private
86
+
87
+ def zipkey(key)
88
+ key
89
+ end
90
+
91
+ def unzipkey(key)
92
+ key
93
+ end
94
+
95
+ end
96
+
97
+ class ZipKeyHash < ZipHash
98
+
99
+ private
100
+
101
+ def zipval(value)
102
+ value
103
+ end
104
+
105
+ def unzipval(value)
106
+ value
107
+ end
108
+
109
+ end
110
+
111
+ class ZipVal
112
+
113
+ include Comparable
114
+
115
+ def initialize(value)
116
+ @value = zip(value)
117
+ end
118
+
119
+ def <=>(other)
120
+ to_s <=> other.to_s if self.class.equal?(other.class)
121
+ end
122
+
123
+ def to_s
124
+ unzip(@value)
125
+ end
126
+
127
+ def inspect
128
+ !((s = to_s).length > 64 || s.include?($/)) ? to_s :
129
+ '#<%s:0x%x length=%p>' % [self.class, object_id, @value.length]
130
+ end
131
+
132
+ def hash
133
+ to_s.hash
134
+ end
135
+
136
+ def eql?(other)
137
+ (self <=> other) == 0
138
+ end
139
+
140
+ alias_method :==, :eql?
141
+
142
+ private
143
+
144
+ def zip(string)
145
+ Zlib::Deflate.deflate(string)
146
+ end
147
+
148
+ def unzip(string)
149
+ Zlib::Inflate.inflate(string)
150
+ end
151
+
152
+ end
153
+
154
+ class ZipKey < ZipVal
155
+ end
156
+
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,155 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ ###############################################################################
5
+ # #
6
+ # nuggets -- Extending Ruby #
7
+ # #
8
+ # Copyright (C) 2007-2011 Jens Wille #
9
+ # #
10
+ # Authors: #
11
+ # Jens Wille <jens.wille@gmail.com> #
12
+ # #
13
+ # nuggets is free software; you can redistribute it and/or modify it under #
14
+ # the terms of the GNU Affero General Public License as published by the Free #
15
+ # Software Foundation; either version 3 of the License, or (at your option) #
16
+ # any later version. #
17
+ # #
18
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
19
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
20
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
21
+ # more details. #
22
+ # #
23
+ # You should have received a copy of the GNU Affero General Public License #
24
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ module Nuggets
30
+ module I18n
31
+
32
+ DIACRITICS = {
33
+ 'À' => 'A', # LATIN CAPITAL LETTER A WITH GRAVE
34
+ 'Á' => 'A', # LATIN CAPITAL LETTER A WITH ACUTE
35
+ 'Â' => 'A', # LATIN CAPITAL LETTER A WITH CIRCUMFLEX
36
+ 'Ã' => 'A', # LATIN CAPITAL LETTER A WITH TILDE
37
+ 'Ä' => 'AE', # LATIN CAPITAL LETTER A WITH DIAERESIS
38
+ 'Å' => 'A', # LATIN CAPITAL LETTER A WITH RING ABOVE
39
+ 'Æ' => 'AE', # LATIN CAPITAL LETTER AE
40
+ 'Ç' => 'C', # LATIN CAPITAL LETTER C WITH CEDILLA
41
+ 'È' => 'E', # LATIN CAPITAL LETTER E WITH GRAVE
42
+ 'É' => 'E', # LATIN CAPITAL LETTER E WITH ACUTE
43
+ 'Ê' => 'E', # LATIN CAPITAL LETTER E WITH CIRCUMFLEX
44
+ 'Ë' => 'E', # LATIN CAPITAL LETTER E WITH DIAERESIS
45
+ 'Ì' => 'I', # LATIN CAPITAL LETTER I WITH GRAVE
46
+ 'Í' => 'I', # LATIN CAPITAL LETTER I WITH ACUTE
47
+ 'Î' => 'I', # LATIN CAPITAL LETTER I WITH CIRCUMFLEX
48
+ 'Ï' => 'I', # LATIN CAPITAL LETTER I WITH DIAERESIS
49
+ 'Ð' => 'DH', # LATIN CAPITAL LETTER ETH
50
+ 'Ñ' => 'N', # LATIN CAPITAL LETTER N WITH TILDE
51
+ 'Ò' => 'O', # LATIN CAPITAL LETTER O WITH GRAVE
52
+ 'Ó' => 'O', # LATIN CAPITAL LETTER O WITH ACUTE
53
+ 'Ô' => 'O', # LATIN CAPITAL LETTER O WITH CIRCUMFLEX
54
+ 'Õ' => 'O', # LATIN CAPITAL LETTER O WITH TILDE
55
+ 'Ö' => 'OE', # LATIN CAPITAL LETTER O WITH DIAERESIS
56
+ 'Ø' => 'O', # LATIN CAPITAL LETTER O WITH STROKE
57
+ 'Ù' => 'U', # LATIN CAPITAL LETTER U WITH GRAVE
58
+ 'Ú' => 'U', # LATIN CAPITAL LETTER U WITH ACUTE
59
+ 'Û' => 'U', # LATIN CAPITAL LETTER U WITH CIRCUMFLEX
60
+ 'Ü' => 'UE', # LATIN CAPITAL LETTER U WITH DIAERESIS
61
+ 'Ý' => 'Y', # LATIN CAPITAL LETTER Y WITH ACUTE
62
+ 'Þ' => 'TH', # LATIN CAPITAL LETTER THORN
63
+ 'ß' => 'ss', # LATIN SMALL LETTER SHARP S
64
+ 'à' => 'a', # LATIN SMALL LETTER A WITH GRAVE
65
+ 'á' => 'a', # LATIN SMALL LETTER A WITH ACUTE
66
+ 'â' => 'a', # LATIN SMALL LETTER A WITH CIRCUMFLEX
67
+ 'ã' => 'a', # LATIN SMALL LETTER A WITH TILDE
68
+ 'ä' => 'ae', # LATIN SMALL LETTER A WITH DIAERESIS
69
+ 'å' => 'a', # LATIN SMALL LETTER A WITH RING ABOVE
70
+ 'æ' => 'ae', # LATIN SMALL LETTER AE
71
+ 'ç' => 'c', # LATIN SMALL LETTER C WITH CEDILLA
72
+ 'è' => 'e', # LATIN SMALL LETTER E WITH GRAVE
73
+ 'é' => 'e', # LATIN SMALL LETTER E WITH ACUTE
74
+ 'ê' => 'e', # LATIN SMALL LETTER E WITH CIRCUMFLEX
75
+ 'ë' => 'e', # LATIN SMALL LETTER E WITH DIAERESIS
76
+ 'ì' => 'i', # LATIN SMALL LETTER I WITH GRAVE
77
+ 'í' => 'i', # LATIN SMALL LETTER I WITH ACUTE
78
+ 'î' => 'i', # LATIN SMALL LETTER I WITH CIRCUMFLEX
79
+ 'ï' => 'i', # LATIN SMALL LETTER I WITH DIAERESIS
80
+ 'ð' => 'dh', # LATIN SMALL LETTER ETH
81
+ 'ñ' => 'n', # LATIN SMALL LETTER N WITH TILDE
82
+ 'ò' => 'o', # LATIN SMALL LETTER O WITH GRAVE
83
+ 'ó' => 'o', # LATIN SMALL LETTER O WITH ACUTE
84
+ 'ô' => 'o', # LATIN SMALL LETTER O WITH CIRCUMFLEX
85
+ 'õ' => 'o', # LATIN SMALL LETTER O WITH TILDE
86
+ 'ö' => 'oe', # LATIN SMALL LETTER O WITH DIAERESIS
87
+ 'ø' => 'o', # LATIN SMALL LETTER O WITH STROKE
88
+ 'ù' => 'u', # LATIN SMALL LETTER U WITH GRAVE
89
+ 'ú' => 'u', # LATIN SMALL LETTER U WITH ACUTE
90
+ 'û' => 'u', # LATIN SMALL LETTER U WITH CIRCUMFLEX
91
+ 'ü' => 'ue', # LATIN SMALL LETTER U WITH DIAERESIS
92
+ 'ý' => 'y', # LATIN SMALL LETTER Y WITH ACUTE
93
+ 'þ' => 'th', # LATIN SMALL LETTER THORN
94
+ 'ÿ' => 'y' # LATIN SMALL LETTER Y WITH DIAERESIS
95
+ }
96
+
97
+ def self.args_for_map_diacritics
98
+ @args_for_map_diacritics ||= begin
99
+ map = ::Hash.new { |h, k| h[k] = [] }
100
+
101
+ DIACRITICS.each { |a| a.each { |i| map[i].concat(a) } }
102
+ map.each { |k, v| v.uniq!; map[k] = "(#{::Regexp.union(*v).source})" }
103
+
104
+ [::Regexp.union(*map.keys.sort_by { |k| -k.length }), map.method(:[])]
105
+ end
106
+ end
107
+
108
+ end
109
+ end
110
+
111
+ class String
112
+
113
+ # call-seq:
114
+ # str.replace_diacritics => new_str
115
+ #
116
+ # Substitutes any diacritics in _str_ with their replacements as per
117
+ # Nuggets::I18n::DIACRITICS.
118
+ def replace_diacritics
119
+ (_dup = dup).replace_diacritics! || _dup
120
+ end
121
+
122
+ # call-seq:
123
+ # str.replace_diacritics! => str or +nil+
124
+ #
125
+ # Destructive version of #replace_diacritics.
126
+ def replace_diacritics!
127
+ diacritics = ::Nuggets::I18n::DIACRITICS
128
+
129
+ gsub!(/#{::Regexp.union(*diacritics.keys)}/) { |m|
130
+ s = diacritics[m]
131
+
132
+ # Try to adjust case:
133
+ # 'Äh' => 'AEh' => 'Aeh'
134
+ #
135
+ # But:
136
+ # 'SÖS' => 'SOES' (not 'SOeS'!)
137
+ if s.length > 1
138
+ t = $'[0, 1]
139
+ s[1..-1] = s[1..-1].downcase if t == t.downcase
140
+ end
141
+
142
+ s
143
+ }
144
+ end
145
+
146
+ def map_diacritics
147
+ (_dup = dup).map_diacritics! || _dup
148
+ end
149
+
150
+ def map_diacritics!
151
+ re, block = ::Nuggets::I18n.args_for_map_diacritics
152
+ gsub!(re, &block)
153
+ end
154
+
155
+ end
@@ -0,0 +1,56 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2011 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ class Integer
28
+
29
+ # Memoization container: integer => factorial(integer)
30
+ FACTORIAL = { 0 => 1 }
31
+
32
+ # call-seq:
33
+ # int.factorial => anInteger
34
+ #
35
+ # Calculate the factorial of _int_. To use the memoized version:
36
+ # <tt>Integer.send(:alias_method, :factorial, :factorial_memoized)</tt>
37
+ def factorial
38
+ (1..self).inject { |f, i| f * i }
39
+ end
40
+
41
+ # call-seq:
42
+ # int.factorial_memoized => anInteger
43
+ #
44
+ # Calculate the factorial of _int_ with the help of memoization (Which gives
45
+ # a considerable speedup for repeated calculations -- at the cost of memory).
46
+ #
47
+ # WARNING: Don't try to calculate the factorial this way for "large"
48
+ # integers! This might well bring your system down to its knees... ;-)
49
+ def factorial_memoized
50
+ FACTORIAL[self] ||= (1..self).inject { |f, i| FACTORIAL[i] ||= f * i }
51
+ end
52
+
53
+ alias_method :fac, :factorial
54
+ alias_method :f!, :factorial
55
+
56
+ end