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,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
|
+
module Nuggets
|
28
|
+
module Env
|
29
|
+
module UserHomeMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# ENV.user_home([default]) => aString
|
33
|
+
#
|
34
|
+
# Returns the user's home directory, or +default+ if it could not be found.
|
35
|
+
def user_home(default = ::File::ALT_SEPARATOR ? 'C:/' : '/')
|
36
|
+
begin
|
37
|
+
return ::Dir.home
|
38
|
+
rescue ::ArgumentError
|
39
|
+
# "couldn't find HOME environment -- expanding `~'"
|
40
|
+
end if ::Dir.respond_to?(:home)
|
41
|
+
|
42
|
+
%w[HOME HOMEDRIVE:HOMEPATH USERPROFILE APPDATA].each { |key|
|
43
|
+
home = values_at(*key.split(':')).join
|
44
|
+
return home.gsub(/\\/, '/') if home && !home.empty?
|
45
|
+
}
|
46
|
+
|
47
|
+
begin
|
48
|
+
::File.expand_path('~')
|
49
|
+
rescue ::ArgumentError
|
50
|
+
default
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2012 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 File
|
29
|
+
module ExtMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# File.chomp_ext(path) -> aString
|
33
|
+
#
|
34
|
+
# Returns a copy of +path+ with its file extension removed.
|
35
|
+
def chomp_ext(path, ext = extname(path))
|
36
|
+
path.chomp(ext)
|
37
|
+
end
|
38
|
+
|
39
|
+
# call-seq:
|
40
|
+
# File.chomp_ext!(path) -> aString | nil
|
41
|
+
#
|
42
|
+
# Modifies +path+ in place as described for #chomp_ext, returning
|
43
|
+
# +path+, or +nil+ if no modifications were made.
|
44
|
+
def chomp_ext!(path, ext = extname(path))
|
45
|
+
path.chomp!(ext)
|
46
|
+
end
|
47
|
+
|
48
|
+
# call-seq:
|
49
|
+
# File.sub_ext(path, new_ext) -> aString
|
50
|
+
#
|
51
|
+
# Returns a copy of +path+ with its file extension replaced
|
52
|
+
# with +new_ext+ (if present; see also #set_ext).
|
53
|
+
def sub_ext(path, new_ext, ext = extname(path))
|
54
|
+
sub_ext!(_path = path.dup, new_ext, ext); _path
|
55
|
+
end
|
56
|
+
|
57
|
+
# call-seq:
|
58
|
+
# File.sub_ext!(path, new_ext) -> aString | nil
|
59
|
+
#
|
60
|
+
# Modifies +path+ in place as described for #sub_ext, returning
|
61
|
+
# +path+, or +nil+ if no modifications were made.
|
62
|
+
def sub_ext!(path, new_ext, ext = extname(path))
|
63
|
+
path << new_ext if chomp_ext!(path, ext)
|
64
|
+
end
|
65
|
+
|
66
|
+
# call-seq:
|
67
|
+
# File.set_ext(path, new_ext) -> aString
|
68
|
+
#
|
69
|
+
# Returns a copy of +path+ with its file extension removed
|
70
|
+
# and +new_ext+ appended. Like #sub_ext, but also applies
|
71
|
+
# when file extension is not present.
|
72
|
+
def set_ext(path, new_ext, ext = extname(path))
|
73
|
+
chomp_ext(path, ext) << new_ext
|
74
|
+
end
|
75
|
+
|
76
|
+
# call-seq:
|
77
|
+
# File.set_ext!(path, new_ext) -> aString
|
78
|
+
#
|
79
|
+
# Modifies +path+ in place as described for #set_ext, returning
|
80
|
+
# +path+.
|
81
|
+
def set_ext!(path, new_ext, ext = extname(path))
|
82
|
+
chomp_ext!(path, ext); path << new_ext
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,55 @@
|
|
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 File
|
29
|
+
module ReplaceMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# File.replace(name[, create_if_missing]) { ... } => aString
|
33
|
+
# File.replace(name[, create_if_missing]) { |content| ... } => aString
|
34
|
+
#
|
35
|
+
# Replaces the contents of file +name+ with the result of the block. Yields
|
36
|
+
# the file's contents to the block if requested. Returns the new content.
|
37
|
+
#
|
38
|
+
# If +create_if_missing+ is +true+ and the file does not exist, it will be
|
39
|
+
# created.
|
40
|
+
def replace(name, create_if_missing = false, &block)
|
41
|
+
open(name, create_if_missing && !exist?(name) ? 'w+' : 'r+') { |f|
|
42
|
+
content = block.arity != 0 ? yield(f.read) : yield
|
43
|
+
|
44
|
+
f.truncate(0)
|
45
|
+
f.rewind
|
46
|
+
|
47
|
+
f.print content
|
48
|
+
|
49
|
+
content
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,95 @@
|
|
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 'nuggets/file/replace_mixin'
|
28
|
+
|
29
|
+
module Nuggets
|
30
|
+
class File
|
31
|
+
module SubMixin
|
32
|
+
|
33
|
+
def self.extended(base)
|
34
|
+
base.extend Nuggets::File::ReplaceMixin
|
35
|
+
end
|
36
|
+
|
37
|
+
# call-seq:
|
38
|
+
# File.sub(name, *args, &block) => aString
|
39
|
+
#
|
40
|
+
# Calls String#sub! on file +name+'s contents with +args+ and (optional)
|
41
|
+
# +block+ and returns the new content.
|
42
|
+
def sub(name, *args)
|
43
|
+
content = read(name)
|
44
|
+
content.sub!(*args, &block_given? ? ::Proc.new : nil)
|
45
|
+
content
|
46
|
+
end
|
47
|
+
|
48
|
+
# call-seq:
|
49
|
+
# File.sub!(name, *args, &block) => aString or +nil+
|
50
|
+
#
|
51
|
+
# Calls String#sub! on file +name+'s contents with +args+ and (optional)
|
52
|
+
# +block+ and replaces the file with the new content. Returns the result
|
53
|
+
# of the String#sub! call.
|
54
|
+
def sub!(name, *args)
|
55
|
+
res = nil
|
56
|
+
|
57
|
+
replace(name) { |content|
|
58
|
+
res = content.sub!(*args, &block_given? ? ::Proc.new : nil)
|
59
|
+
content
|
60
|
+
}
|
61
|
+
|
62
|
+
res
|
63
|
+
end
|
64
|
+
|
65
|
+
# call-seq:
|
66
|
+
# File.gsub(name, *args, &block) => aString
|
67
|
+
#
|
68
|
+
# Calls String#gsub! on file +name+'s contents with +args+ and (optional)
|
69
|
+
# +block+ and returns the new content.
|
70
|
+
def gsub(name, *args)
|
71
|
+
content = read(name)
|
72
|
+
content.gsub!(*args, &block_given? ? ::Proc.new : nil)
|
73
|
+
content
|
74
|
+
end
|
75
|
+
|
76
|
+
# call-seq:
|
77
|
+
# File.gsub!(name, *args, &block) => aString or +nil+
|
78
|
+
#
|
79
|
+
# Calls String#gsub! on file +name+'s contents with +args+ and (optional)
|
80
|
+
# +block+ and replaces the file with the new content. Returns the result
|
81
|
+
# of the String#gsub! call.
|
82
|
+
def gsub!(name, *args)
|
83
|
+
res = nil
|
84
|
+
|
85
|
+
replace(name) { |content|
|
86
|
+
res = content.gsub!(*args, &block_given? ? ::Proc.new : nil)
|
87
|
+
content
|
88
|
+
}
|
89
|
+
|
90
|
+
res
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,72 @@
|
|
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 'rbconfig'
|
28
|
+
|
29
|
+
module Nuggets
|
30
|
+
class File
|
31
|
+
module WhichMixin
|
32
|
+
|
33
|
+
DEFAULT_EXTENSIONS = [::RbConfig::CONFIG['EXEEXT']]
|
34
|
+
|
35
|
+
# call-seq:
|
36
|
+
# File.which(executable[, extensions]) => aString or +nil+
|
37
|
+
#
|
38
|
+
# Returns +executable+ if it's executable, or the full path to +executable+
|
39
|
+
# found in PATH, or +nil+ otherwise. Checks +executable+ with each extension
|
40
|
+
# in +extensions+ appended in turn.
|
41
|
+
#
|
42
|
+
# Inspired by Gnuplot.which -- thx, Gordon!
|
43
|
+
def which(executable, extensions = DEFAULT_EXTENSIONS)
|
44
|
+
extensions |= ['']
|
45
|
+
|
46
|
+
if env = ::ENV['PATH']
|
47
|
+
dirs = env.split(self::PATH_SEPARATOR)
|
48
|
+
dirs.map! { |dir| expand_path(dir) }
|
49
|
+
end
|
50
|
+
|
51
|
+
extensions.find { |extension|
|
52
|
+
file = "#{executable}#{extension}"
|
53
|
+
return file if file?(file) && executable?(file)
|
54
|
+
|
55
|
+
dirs.find { |dir|
|
56
|
+
path = join(dir, file)
|
57
|
+
return path if file?(path) && executable?(path)
|
58
|
+
} if dirs
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
# call-seq:
|
63
|
+
# File.which_command(commands) => aString or +nil+
|
64
|
+
#
|
65
|
+
# Returns the first of +commands+ that is executable (according to #which).
|
66
|
+
def which_command(commands, extensions = DEFAULT_EXTENSIONS)
|
67
|
+
commands.find { |command| which(command.to_s[/\S+/], extensions) }
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,68 @@
|
|
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 'nuggets/array/rand'
|
28
|
+
|
29
|
+
class Hash
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# hash.at(what) => aHash
|
33
|
+
#
|
34
|
+
# Returns the key/value pair of _hash_ at key position +what+. Remember that
|
35
|
+
# hashes might not have the intended (or expected) order in pre-1.9 Ruby.
|
36
|
+
def at(what, &block)
|
37
|
+
return {} if empty?
|
38
|
+
|
39
|
+
key = what.is_a?(::Integer) ? keys[what] : keys.send(*what, &block)
|
40
|
+
|
41
|
+
{ key => self[key] }
|
42
|
+
end
|
43
|
+
|
44
|
+
# call-seq:
|
45
|
+
# hash.first => aHash
|
46
|
+
#
|
47
|
+
# Returns the "first" key/value pair of _hash_.
|
48
|
+
def first
|
49
|
+
at(:first)
|
50
|
+
end
|
51
|
+
|
52
|
+
# call-seq:
|
53
|
+
# hash.last => aHash
|
54
|
+
#
|
55
|
+
# Returns the "last" key/value pair of _hash_.
|
56
|
+
def last
|
57
|
+
at(:last)
|
58
|
+
end
|
59
|
+
|
60
|
+
# call-seq:
|
61
|
+
# hash.rand => aHash
|
62
|
+
#
|
63
|
+
# Returns a random key/value pair of _hash_.
|
64
|
+
def rand
|
65
|
+
at(:rand)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|