nuggets 0.9.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/COPYING +663 -0
- data/ChangeLog +7 -0
- data/README +74 -0
- data/Rakefile +50 -0
- data/lib/nuggets.rb +73 -0
- data/lib/nuggets/all.rb +35 -0
- data/lib/nuggets/all_mixins.rb +33 -0
- data/lib/nuggets/ansicolor2css.rb +124 -0
- data/lib/nuggets/argv/option.rb +3 -0
- data/lib/nuggets/argv/option_mixin.rb +84 -0
- data/lib/nuggets/array/boost.rb +5 -0
- data/lib/nuggets/array/boost_mixin.rb +77 -0
- data/lib/nuggets/array/combination.rb +62 -0
- data/lib/nuggets/array/correlation.rb +5 -0
- data/lib/nuggets/array/correlation_mixin.rb +63 -0
- data/lib/nuggets/array/flatten_once.rb +56 -0
- data/lib/nuggets/array/flush.rb +5 -0
- data/lib/nuggets/array/flush_mixin.rb +46 -0
- data/lib/nuggets/array/format.rb +65 -0
- data/lib/nuggets/array/hashify.rb +5 -0
- data/lib/nuggets/array/hashify_mixin.rb +47 -0
- data/lib/nuggets/array/histogram.rb +5 -0
- data/lib/nuggets/array/histogram_mixin.rb +169 -0
- data/lib/nuggets/array/in_order.rb +50 -0
- data/lib/nuggets/array/limit.rb +5 -0
- data/lib/nuggets/array/limit_mixin.rb +56 -0
- data/lib/nuggets/array/mean.rb +5 -0
- data/lib/nuggets/array/mean_mixin.rb +202 -0
- data/lib/nuggets/array/median.rb +5 -0
- data/lib/nuggets/array/median_mixin.rb +73 -0
- data/lib/nuggets/array/mode.rb +5 -0
- data/lib/nuggets/array/mode_mixin.rb +69 -0
- data/lib/nuggets/array/monotone.rb +83 -0
- data/lib/nuggets/array/only.rb +41 -0
- data/lib/nuggets/array/rand.rb +44 -0
- data/lib/nuggets/array/regression.rb +5 -0
- data/lib/nuggets/array/regression_mixin.rb +149 -0
- data/lib/nuggets/array/runiq.rb +5 -0
- data/lib/nuggets/array/runiq_mixin.rb +52 -0
- data/lib/nuggets/array/shuffle.rb +132 -0
- data/lib/nuggets/array/standard_deviation.rb +5 -0
- data/lib/nuggets/array/standard_deviation_mixin.rb +50 -0
- data/lib/nuggets/array/to_hash.rb +64 -0
- data/lib/nuggets/array/variance.rb +5 -0
- data/lib/nuggets/array/variance_mixin.rb +81 -0
- data/lib/nuggets/content_type.rb +97 -0
- data/lib/nuggets/dotted_decimal.rb +59 -0
- data/lib/nuggets/enumerable/agrep.rb +79 -0
- data/lib/nuggets/enumerable/all_any_extended.rb +66 -0
- data/lib/nuggets/enumerable/minmax.rb +102 -0
- data/lib/nuggets/env/set.rb +3 -0
- data/lib/nuggets/env/set_mixin.rb +67 -0
- data/lib/nuggets/env/user_encoding.rb +3 -0
- data/lib/nuggets/env/user_encoding_mixin.rb +54 -0
- data/lib/nuggets/env/user_home.rb +3 -0
- data/lib/nuggets/env/user_home_mixin.rb +56 -0
- data/lib/nuggets/file/ext.rb +5 -0
- data/lib/nuggets/file/ext_mixin.rb +87 -0
- data/lib/nuggets/file/replace.rb +5 -0
- data/lib/nuggets/file/replace_mixin.rb +55 -0
- data/lib/nuggets/file/sub.rb +5 -0
- data/lib/nuggets/file/sub_mixin.rb +95 -0
- data/lib/nuggets/file/which.rb +5 -0
- data/lib/nuggets/file/which_mixin.rb +72 -0
- data/lib/nuggets/hash/at.rb +68 -0
- data/lib/nuggets/hash/deep_fetch.rb +5 -0
- data/lib/nuggets/hash/deep_fetch_mixin.rb +74 -0
- data/lib/nuggets/hash/deep_merge.rb +5 -0
- data/lib/nuggets/hash/deep_merge_mixin.rb +53 -0
- data/lib/nuggets/hash/idmap.rb +5 -0
- data/lib/nuggets/hash/idmap_mixin.rb +41 -0
- data/lib/nuggets/hash/in_order.rb +43 -0
- data/lib/nuggets/hash/insert.rb +53 -0
- data/lib/nuggets/hash/nest.rb +5 -0
- data/lib/nuggets/hash/nest_mixin.rb +78 -0
- data/lib/nuggets/hash/only.rb +52 -0
- data/lib/nuggets/hash/seen.rb +5 -0
- data/lib/nuggets/hash/seen_mixin.rb +58 -0
- data/lib/nuggets/hash/unroll.rb +5 -0
- data/lib/nuggets/hash/unroll_mixin.rb +88 -0
- data/lib/nuggets/hash/zip.rb +5 -0
- data/lib/nuggets/hash/zip_mixin.rb +159 -0
- data/lib/nuggets/i18n.rb +155 -0
- data/lib/nuggets/integer/factorial.rb +56 -0
- data/lib/nuggets/integer/length.rb +5 -0
- data/lib/nuggets/integer/length_mixin.rb +49 -0
- data/lib/nuggets/integer/map.rb +5 -0
- data/lib/nuggets/integer/map_mixin.rb +42 -0
- data/lib/nuggets/integer/to_binary_s.rb +38 -0
- data/lib/nuggets/io/agrep.rb +43 -0
- data/lib/nuggets/io/interact.rb +5 -0
- data/lib/nuggets/io/interact_mixin.rb +159 -0
- data/lib/nuggets/io/modes.rb +121 -0
- data/lib/nuggets/io/null.rb +5 -0
- data/lib/nuggets/io/null_mixin.rb +40 -0
- data/lib/nuggets/io/redirect.rb +5 -0
- data/lib/nuggets/io/redirect_mixin.rb +50 -0
- data/lib/nuggets/lazy_attr.rb +44 -0
- data/lib/nuggets/log_parser.rb +70 -0
- data/lib/nuggets/log_parser/apache.rb +101 -0
- data/lib/nuggets/log_parser/rails.rb +219 -0
- data/lib/nuggets/net/success.rb +59 -0
- data/lib/nuggets/numeric/between.rb +2 -0
- data/lib/nuggets/numeric/duration.rb +100 -0
- data/lib/nuggets/numeric/limit.rb +62 -0
- data/lib/nuggets/numeric/signum.rb +52 -0
- data/lib/nuggets/numeric/to_multiple.rb +61 -0
- data/lib/nuggets/object/blank.rb +20 -0
- data/lib/nuggets/object/blank_mixin.rb +99 -0
- data/lib/nuggets/object/boolean.rb +5 -0
- data/lib/nuggets/object/boolean_mixin.rb +61 -0
- data/lib/nuggets/object/eigenclass.rb +2 -0
- data/lib/nuggets/object/ghost_class.rb +2 -0
- data/lib/nuggets/object/metaclass.rb +2 -0
- data/lib/nuggets/object/msend.rb +5 -0
- data/lib/nuggets/object/msend_mixin.rb +43 -0
- data/lib/nuggets/object/silence.rb +5 -0
- data/lib/nuggets/object/silence_mixin.rb +44 -0
- data/lib/nuggets/object/singleton_class.rb +5 -0
- data/lib/nuggets/object/singleton_class_mixin.rb +95 -0
- data/lib/nuggets/object/uniclass.rb +2 -0
- data/lib/nuggets/object/virtual_class.rb +2 -0
- data/lib/nuggets/pluggable.rb +91 -0
- data/lib/nuggets/proc/bind.rb +5 -0
- data/lib/nuggets/proc/bind_mixin.rb +51 -0
- data/lib/nuggets/range/quantile.rb +5 -0
- data/lib/nuggets/range/quantile_mixin.rb +42 -0
- data/lib/nuggets/ruby.rb +235 -0
- data/lib/nuggets/statistics.rb +12 -0
- data/lib/nuggets/statistics_mixins.rb +12 -0
- data/lib/nuggets/string/camelscore.rb +5 -0
- data/lib/nuggets/string/camelscore_mixin.rb +116 -0
- data/lib/nuggets/string/capitalize_first.rb +46 -0
- data/lib/nuggets/string/case.rb +81 -0
- data/lib/nuggets/string/evaluate.rb +5 -0
- data/lib/nuggets/string/evaluate_mixin.rb +47 -0
- data/lib/nuggets/string/msub.rb +84 -0
- data/lib/nuggets/string/nsub.rb +65 -0
- data/lib/nuggets/string/sub_with_md.rb +111 -0
- data/lib/nuggets/string/wc.rb +5 -0
- data/lib/nuggets/string/wc_mixin.rb +95 -0
- data/lib/nuggets/string/word_wrap.rb +76 -0
- data/lib/nuggets/string/xor.rb +5 -0
- data/lib/nuggets/string/xor_mixin.rb +59 -0
- data/lib/nuggets/tempfile/open.rb +57 -0
- data/lib/nuggets/uri/content_type.rb +5 -0
- data/lib/nuggets/uri/content_type_mixin.rb +47 -0
- data/lib/nuggets/uri/exist.rb +5 -0
- data/lib/nuggets/uri/exist_mixin.rb +56 -0
- data/lib/nuggets/uri/redirect.rb +5 -0
- data/lib/nuggets/uri/redirect_mixin.rb +101 -0
- data/lib/nuggets/version.rb +27 -0
- data/spec/nuggets/array/boost_spec.rb +50 -0
- data/spec/nuggets/array/combination_spec.rb +25 -0
- data/spec/nuggets/array/correlation_spec.rb +81 -0
- data/spec/nuggets/array/flatten_once_spec.rb +16 -0
- data/spec/nuggets/array/flush_spec.rb +43 -0
- data/spec/nuggets/array/format_spec.rb +52 -0
- data/spec/nuggets/array/hashify_spec.rb +41 -0
- data/spec/nuggets/array/histogram_spec.rb +87 -0
- data/spec/nuggets/array/in_order_spec.rb +13 -0
- data/spec/nuggets/array/limit_spec.rb +62 -0
- data/spec/nuggets/array/mean_spec.rb +203 -0
- data/spec/nuggets/array/median_spec.rb +77 -0
- data/spec/nuggets/array/mode_spec.rb +57 -0
- data/spec/nuggets/array/monotone_spec.rb +30 -0
- data/spec/nuggets/array/only_spec.rb +26 -0
- data/spec/nuggets/array/regression_spec.rb +54 -0
- data/spec/nuggets/array/runiq_spec.rb +25 -0
- data/spec/nuggets/array/standard_deviation_spec.rb +33 -0
- data/spec/nuggets/array/to_hash_spec.rb +28 -0
- data/spec/nuggets/array/variance_spec.rb +106 -0
- data/spec/nuggets/dotted_decimal_spec.rb +27 -0
- data/spec/nuggets/enumerable/all_any_extended_spec.rb +31 -0
- data/spec/nuggets/enumerable/minmax_spec.rb +21 -0
- data/spec/nuggets/env/set_spec.rb +29 -0
- data/spec/nuggets/env/user_encoding_spec.rb +38 -0
- data/spec/nuggets/env/user_home_spec.rb +42 -0
- data/spec/nuggets/file/ext_spec.rb +38 -0
- data/spec/nuggets/file/replace_spec.rb +95 -0
- data/spec/nuggets/file/sub_spec.rb +149 -0
- data/spec/nuggets/file/which_spec.rb +22 -0
- data/spec/nuggets/hash/at_spec.rb +19 -0
- data/spec/nuggets/hash/deep_fetch_spec.rb +159 -0
- data/spec/nuggets/hash/deep_merge_spec.rb +78 -0
- data/spec/nuggets/hash/in_order_spec.rb +12 -0
- data/spec/nuggets/hash/insert_spec.rb +13 -0
- data/spec/nuggets/hash/nest_spec.rb +102 -0
- data/spec/nuggets/hash/only_spec.rb +29 -0
- data/spec/nuggets/hash/seen_spec.rb +36 -0
- data/spec/nuggets/hash/unroll_spec.rb +68 -0
- data/spec/nuggets/i18n_spec.rb +13 -0
- data/spec/nuggets/integer/factorial_spec.rb +10 -0
- data/spec/nuggets/integer/length_spec.rb +18 -0
- data/spec/nuggets/integer/map_spec.rb +19 -0
- data/spec/nuggets/integer/to_binary_s_spec.rb +19 -0
- data/spec/nuggets/numeric/duration_spec.rb +25 -0
- data/spec/nuggets/numeric/limit_spec.rb +16 -0
- data/spec/nuggets/numeric/signum_spec.rb +16 -0
- data/spec/nuggets/numeric/to_multiple_spec.rb +16 -0
- data/spec/nuggets/object/blank_spec.rb +34 -0
- data/spec/nuggets/object/boolean_spec.rb +23 -0
- data/spec/nuggets/object/msend_spec.rb +25 -0
- data/spec/nuggets/object/silence_spec.rb +36 -0
- data/spec/nuggets/object/singleton_class_spec.rb +51 -0
- data/spec/nuggets/proc/bind_spec.rb +28 -0
- data/spec/nuggets/range/quantile_spec.rb +33 -0
- data/spec/nuggets/string/camelscore_spec.rb +114 -0
- data/spec/nuggets/string/capitalize_first_spec.rb +13 -0
- data/spec/nuggets/string/case_spec.rb +31 -0
- data/spec/nuggets/string/evaluate_spec.rb +24 -0
- data/spec/nuggets/string/msub_spec.rb +20 -0
- data/spec/nuggets/string/nsub_spec.rb +13 -0
- data/spec/nuggets/string/sub_with_md_spec.rb +25 -0
- data/spec/nuggets/string/wc_spec.rb +73 -0
- data/spec/nuggets/string/word_wrap_spec.rb +81 -0
- data/spec/nuggets/string/xor_spec.rb +57 -0
- data/spec/nuggets/uri/content_type_spec.rb +42 -0
- data/spec/nuggets/uri/exist_spec.rb +49 -0
- data/spec/spec_helper.rb +36 -0
- metadata +309 -17
@@ -0,0 +1,59 @@
|
|
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
|
+
require 'net/http'
|
28
|
+
|
29
|
+
module Net
|
30
|
+
|
31
|
+
class HTTPResponse
|
32
|
+
def success?; nil end
|
33
|
+
end
|
34
|
+
|
35
|
+
class HTTPInformation
|
36
|
+
# ???
|
37
|
+
end
|
38
|
+
|
39
|
+
class HTTPSuccess
|
40
|
+
def success?; true end
|
41
|
+
end
|
42
|
+
|
43
|
+
class HTTPRedirection
|
44
|
+
def success?; true end
|
45
|
+
end
|
46
|
+
|
47
|
+
class HTTPMultipleChoice
|
48
|
+
def success?; false end
|
49
|
+
end
|
50
|
+
|
51
|
+
class HTTPClientError
|
52
|
+
def success?; false end
|
53
|
+
end
|
54
|
+
|
55
|
+
class HTTPServerError
|
56
|
+
def success?; false end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,100 @@
|
|
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 Numeric
|
28
|
+
|
29
|
+
# call-seq:
|
30
|
+
# num.hms => anArray
|
31
|
+
#
|
32
|
+
# Converts _num_ into hour, minute, and second portions.
|
33
|
+
def hms
|
34
|
+
raise ::ArgumentError, "negative duration #{self}" if self < 0
|
35
|
+
|
36
|
+
one_minute = 60
|
37
|
+
one_hour = 60 * one_minute
|
38
|
+
|
39
|
+
[((h,) = divmod(one_hour)).last.divmod(one_minute), h].reverse.flatten # *SCNR* ;-)
|
40
|
+
end
|
41
|
+
|
42
|
+
# call-seq:
|
43
|
+
# num.ymd => anArray
|
44
|
+
#
|
45
|
+
# Converts _num_ into year, month, and day portions.
|
46
|
+
def ymd
|
47
|
+
raise ::ArgumentError, "negative duration #{self}" if self < 0
|
48
|
+
|
49
|
+
one_day = 24 * 60 * 60
|
50
|
+
one_month = 30 * one_day
|
51
|
+
one_year = 365.25 * one_day
|
52
|
+
|
53
|
+
y, m = divmod(one_year)
|
54
|
+
m, d = m.divmod(one_month)
|
55
|
+
|
56
|
+
[y, m, d / one_day]
|
57
|
+
end
|
58
|
+
|
59
|
+
# call-seq:
|
60
|
+
# num.to_hms([precision[, labels]]) => aString
|
61
|
+
#
|
62
|
+
# Produces a stringified version of _num_'s time portions (cf. #hms),
|
63
|
+
# with the specified +precision+ for the seconds (treated as floating
|
64
|
+
# point). The individual parts are labelled as specified in the +labels+
|
65
|
+
# parameter (hours, minutes, seconds in that order). Leading parts with
|
66
|
+
# a value of zero are omitted.
|
67
|
+
#
|
68
|
+
# Examples:
|
69
|
+
# 180.to_hms #=> "3m0s"
|
70
|
+
# 180.75.to_hms #=> "3m1s"
|
71
|
+
# 180.75.to_hms(2) #=> "3m0.75s"
|
72
|
+
# 8180.to_hms #=> "2h16m20s"
|
73
|
+
# 8180.to_hms(0, %w[: :]) #=> "2:16:20"
|
74
|
+
def to_hms(precision = 0, labels = %w[h m s], time = hms)
|
75
|
+
h, m, s = time
|
76
|
+
|
77
|
+
h.zero? ? m.zero? ?
|
78
|
+
"%0.#{precision}f#{labels[2]}" % [s] :
|
79
|
+
"%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [m, s] :
|
80
|
+
"%d#{labels[0]}%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [h, m, s]
|
81
|
+
end
|
82
|
+
|
83
|
+
# call-seq:
|
84
|
+
# num.to_ymd([include_hms[, labels]]) => aString
|
85
|
+
#
|
86
|
+
# Produces a stringified version of _num_'s date portions (cf. #ymd),
|
87
|
+
# analogous to #to_hms. Includes time portions (cf. #hms) if +include_hms+
|
88
|
+
# is +true+.
|
89
|
+
def to_ymd(include_hms = false, labels = %w[y m d])
|
90
|
+
unless include_hms
|
91
|
+
to_hms(0, labels, ymd)
|
92
|
+
else
|
93
|
+
y, m, d = ymd
|
94
|
+
e = d.truncate
|
95
|
+
|
96
|
+
"#{to_hms(0, labels, [y, m, e])} #{((d - e) * 24 * 60 * 60).to_hms}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
@@ -0,0 +1,62 @@
|
|
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 Numeric
|
28
|
+
|
29
|
+
# call-seq:
|
30
|
+
# num.limit(min, max) => aNumeric
|
31
|
+
#
|
32
|
+
# Returns +min+ if that's larger than _num_, or +max+ if that's smaller than
|
33
|
+
# _num_. Otherwise returns _num_.
|
34
|
+
def limit(min, max)
|
35
|
+
min, max = max, min if max < min
|
36
|
+
|
37
|
+
min(min).max(max)
|
38
|
+
end
|
39
|
+
|
40
|
+
alias_method :between, :limit
|
41
|
+
|
42
|
+
# call-seq:
|
43
|
+
# num.min(min) => aNumeric
|
44
|
+
#
|
45
|
+
# Returns _num_ or +min+, whatever is larger.
|
46
|
+
def min(min)
|
47
|
+
self < min ? min : self
|
48
|
+
end
|
49
|
+
|
50
|
+
alias_method :at_least, :min
|
51
|
+
|
52
|
+
# call-seq:
|
53
|
+
# num.max(max) => aNumeric
|
54
|
+
#
|
55
|
+
# Returns _num_ or +max+, whatever is smaller.
|
56
|
+
def max(max)
|
57
|
+
self > max ? max : self
|
58
|
+
end
|
59
|
+
|
60
|
+
alias_method :at_most, :max
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,52 @@
|
|
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 Numeric
|
28
|
+
|
29
|
+
def positive?
|
30
|
+
self > 0
|
31
|
+
end
|
32
|
+
|
33
|
+
def negative?
|
34
|
+
self < 0
|
35
|
+
end
|
36
|
+
|
37
|
+
def non_negative?
|
38
|
+
!negative?
|
39
|
+
end
|
40
|
+
|
41
|
+
# call-seq:
|
42
|
+
# num.signum => -1, 0, 1
|
43
|
+
#
|
44
|
+
# Returns the sign of _num_.
|
45
|
+
def signum
|
46
|
+
positive? ? 1 : negative? ? -1 : 0
|
47
|
+
end
|
48
|
+
|
49
|
+
alias_method :sign, :signum
|
50
|
+
alias_method :sgn, :signum
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,61 @@
|
|
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 Numeric
|
28
|
+
|
29
|
+
# call-seq:
|
30
|
+
# num.to_multiple_of(target, what) => aNumeric
|
31
|
+
#
|
32
|
+
# Returns the nearest multiple of +target+ according to +what+.
|
33
|
+
def to_multiple_of(target, what = :round)
|
34
|
+
target.zero? ? self : (to_f / target).send(what) * target
|
35
|
+
end
|
36
|
+
|
37
|
+
# call-seq:
|
38
|
+
# num.round_to(target) => aNumeric
|
39
|
+
#
|
40
|
+
# Rounds _num_ to the nearest multiple of +target+.
|
41
|
+
def round_to(target)
|
42
|
+
to_multiple_of(target, :round)
|
43
|
+
end
|
44
|
+
|
45
|
+
# call-seq:
|
46
|
+
# num.floor_to(target) => aNumeric
|
47
|
+
#
|
48
|
+
# Returns the largest multiple of +target+ less than or equal to _num_.
|
49
|
+
def floor_to(target)
|
50
|
+
to_multiple_of(target, :floor)
|
51
|
+
end
|
52
|
+
|
53
|
+
# call-seq:
|
54
|
+
# num.ceil_to(target) => aNumeric
|
55
|
+
#
|
56
|
+
# Returns the smallest multiple of +target+ greater than or equal to _num_.
|
57
|
+
def ceil_to(target)
|
58
|
+
to_multiple_of(target, :ceil)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'nuggets/object/blank_mixin'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
include Nuggets::Object::BlankMixin
|
5
|
+
end
|
6
|
+
|
7
|
+
class Array
|
8
|
+
include Nuggets::Array::BlankMixin
|
9
|
+
end
|
10
|
+
|
11
|
+
class Hash
|
12
|
+
include Nuggets::Hash::BlankMixin
|
13
|
+
end
|
14
|
+
|
15
|
+
#class String
|
16
|
+
# if public_instance_methods(false).include?(method = 'blank?')
|
17
|
+
# # remove incompatible implementation added by utility_belt/language_greps.rb
|
18
|
+
# remove_method method
|
19
|
+
# end
|
20
|
+
#end
|
@@ -0,0 +1,99 @@
|
|
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 Object
|
29
|
+
module BlankMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# object.blank? => +true+ or +false+
|
33
|
+
#
|
34
|
+
# Basically a short-cut to <tt>object.nil? || object.empty?</tt>.
|
35
|
+
def blank?(*modifiers)
|
36
|
+
if block_given?
|
37
|
+
return true if yield(dup).blank?
|
38
|
+
end
|
39
|
+
|
40
|
+
if modifiers.empty?
|
41
|
+
respond_to?(:empty?) ? empty? : !self
|
42
|
+
else
|
43
|
+
return true if blank?
|
44
|
+
|
45
|
+
modifiers.each { |modifier|
|
46
|
+
if respond_to?(modifier)
|
47
|
+
if modifier.to_s =~ /\?\z/
|
48
|
+
return true if send(modifier)
|
49
|
+
else
|
50
|
+
return true if send(modifier).blank?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
}
|
54
|
+
|
55
|
+
false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# call-seq:
|
60
|
+
# object.void? => +true+ or +false+
|
61
|
+
#
|
62
|
+
# Adds white-space strings, 0 and arrays of +nil+ objects to the list of
|
63
|
+
# blank objects.
|
64
|
+
def void?
|
65
|
+
blank?(:zero?, :strip, :compact)
|
66
|
+
end
|
67
|
+
alias_method :vain?, :void?
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class Array
|
73
|
+
module BlankMixin
|
74
|
+
|
75
|
+
# call-seq:
|
76
|
+
# array.vain? => +true+ or +false+
|
77
|
+
#
|
78
|
+
# Returns +true+ if all of _array_'s elements are themselves vain.
|
79
|
+
def vain?
|
80
|
+
blank? { |a| a.delete_if { |i| i.vain? } }
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class Hash
|
87
|
+
module BlankMixin
|
88
|
+
|
89
|
+
# call-seq:
|
90
|
+
# hash.vain? => +true+ or +false+
|
91
|
+
#
|
92
|
+
# Returns +true+ if all of _hash_'s values are themselves vain.
|
93
|
+
def vain?
|
94
|
+
blank? { |h| h.delete_if { |_, v| v.vain? } }
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|