hash-utils 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.txt +8 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -5
- data/LICENSE.txt +1 -1
- data/README.md +3 -3
- data/Rakefile +3 -4
- data/VERSION +1 -1
- data/hash-utils.gemspec +8 -8
- data/lib/hash-utils/array.rb +120 -92
- data/lib/hash-utils/boolean.rb +43 -21
- data/lib/hash-utils/file.rb +61 -43
- data/lib/hash-utils/gem.rb +16 -12
- data/lib/hash-utils/hash.rb +306 -264
- data/lib/hash-utils/io.rb +7 -3
- data/lib/hash-utils/module.rb +14 -10
- data/lib/hash-utils/nil.rb +7 -3
- data/lib/hash-utils/numeric.rb +26 -12
- data/lib/hash-utils/object.rb +165 -52
- data/lib/hash-utils/proc.rb +7 -3
- data/lib/hash-utils/string.rb +289 -173
- data/lib/hash-utils/stringio.rb +7 -3
- data/lib/hash-utils/symbol.rb +47 -24
- data/test +7 -1
- metadata +20 -22
data/lib/hash-utils/io.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "hash-utils/object"
|
3
5
|
|
4
6
|
##
|
5
7
|
# IO extension.
|
@@ -14,8 +16,10 @@ class IO
|
|
14
16
|
# @since 0.14.0
|
15
17
|
#
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
if not self.__hash_utils_instance_respond_to? :io?
|
20
|
+
def io?
|
21
|
+
true
|
22
|
+
end
|
19
23
|
end
|
20
24
|
|
21
25
|
end
|
data/lib/hash-utils/module.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "hash-utils/object"
|
3
5
|
|
4
6
|
##
|
5
7
|
# Module extension.
|
@@ -19,16 +21,18 @@ class Module
|
|
19
21
|
# @since 0.13.0
|
20
22
|
#
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
if not self.__hash_utils_instance_respond_to? :get_module
|
25
|
+
def get_module(name)
|
26
|
+
names = name.split("::")
|
27
|
+
mod = self
|
28
|
+
|
29
|
+
while not names.empty?
|
30
|
+
name = names.shift
|
31
|
+
mod = mod.const_get(name)
|
32
|
+
end
|
33
|
+
|
34
|
+
return mod
|
29
35
|
end
|
30
|
-
|
31
|
-
return mod
|
32
36
|
end
|
33
37
|
|
34
38
|
end
|
data/lib/hash-utils/nil.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "hash-utils/object"
|
3
5
|
|
4
6
|
##
|
5
7
|
# NilClass extension.
|
@@ -15,8 +17,10 @@ class NilClass
|
|
15
17
|
# @since 0.19.0
|
16
18
|
#
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
+
if not self.__hash_utils_instance_respond_to? :to_boolean
|
21
|
+
def to_boolean(*args)
|
22
|
+
false
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
end
|
data/lib/hash-utils/numeric.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
4
|
require "hash-utils/object"
|
5
5
|
|
@@ -20,8 +20,10 @@ class Numeric
|
|
20
20
|
# @since 0.16.0
|
21
21
|
#
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
if not self.__hash_utils_instance_respond_to? :compare
|
24
|
+
def compare(number)
|
25
|
+
self <=> number
|
26
|
+
end
|
25
27
|
end
|
26
28
|
|
27
29
|
##
|
@@ -31,11 +33,15 @@ class Numeric
|
|
31
33
|
# @since 0.16.0
|
32
34
|
#
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
+
if not self.__hash_utils_instance_respond_to? :positive?
|
37
|
+
def positive?
|
38
|
+
self > 0
|
39
|
+
end
|
36
40
|
end
|
37
41
|
|
38
|
-
|
42
|
+
if not self.__hash_utils_instance_respond_to? :positive
|
43
|
+
alias :positive :positive?
|
44
|
+
end
|
39
45
|
|
40
46
|
##
|
41
47
|
# Indicates, number is negative, so lower than 0.
|
@@ -44,11 +50,15 @@ class Numeric
|
|
44
50
|
# @since 0.16.0
|
45
51
|
#
|
46
52
|
|
47
|
-
|
48
|
-
|
53
|
+
if not self.__hash_utils_instance_respond_to? :negative?
|
54
|
+
def negative?
|
55
|
+
self < 0
|
56
|
+
end
|
49
57
|
end
|
50
58
|
|
51
|
-
|
59
|
+
if not self.__hash_utils_instance_respond_to? :negative
|
60
|
+
alias :negative :negative?
|
61
|
+
end
|
52
62
|
|
53
63
|
##
|
54
64
|
# Sets the positivity to +false+, so negates the number. +0+ will
|
@@ -61,11 +71,15 @@ class Numeric
|
|
61
71
|
# @since 0.16.0
|
62
72
|
#
|
63
73
|
|
64
|
-
|
65
|
-
|
74
|
+
if not self.__hash_utils_instance_respond_to? :negative!
|
75
|
+
def negative!
|
76
|
+
-self.positive!
|
77
|
+
end
|
66
78
|
end
|
67
79
|
|
68
|
-
|
80
|
+
if not self.__hash_utils_instance_respond_to? :negate!
|
81
|
+
alias :negate! :negative!
|
82
|
+
end
|
69
83
|
|
70
84
|
##
|
71
85
|
# Sets the positivity to +true+, so makes number positive. +0+ will
|
data/lib/hash-utils/object.rb
CHANGED
@@ -1,12 +1,79 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "ruby-version"
|
3
5
|
|
4
6
|
##
|
5
7
|
# Object extension.
|
6
8
|
#
|
7
9
|
|
8
10
|
class Object
|
9
|
-
|
11
|
+
|
12
|
+
@@__hash_utils_methods_index = { }
|
13
|
+
@@__hash_utils_object_index = [ ]
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
##
|
18
|
+
# Both indicates, instance of the class responds to some call
|
19
|
+
# and indicates patched calls in Object
|
20
|
+
#
|
21
|
+
# @param [Symbol] call a call name
|
22
|
+
# @return [Boolean] +true+ it it is, +false+ otherwise
|
23
|
+
# @since 2.0.0
|
24
|
+
#
|
25
|
+
|
26
|
+
def self.__hash_utils_object_respond_to?(call)\
|
27
|
+
@@__hash_utils_object_index << call
|
28
|
+
self.__hash_utils_instance_respond_to? call
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
public
|
33
|
+
|
34
|
+
##
|
35
|
+
# Indicates, instance of the class responds to some call.
|
36
|
+
#
|
37
|
+
# @param [Symbol] call a call name
|
38
|
+
# @return [Boolean] +true+ it it is, +false+ otherwise
|
39
|
+
# @private as it's intended for internal library use
|
40
|
+
# @since 2.0.0
|
41
|
+
#
|
42
|
+
|
43
|
+
if not self.methods.include? :__hash_utils_instance_respond_to?
|
44
|
+
def self.__hash_utils_instance_respond_to?(call)
|
45
|
+
if not @@__hash_utils_methods_index.has_key? self
|
46
|
+
instance_methods = self.instance_methods
|
47
|
+
|
48
|
+
if Ruby::Version >= [1, 9]
|
49
|
+
require "set"
|
50
|
+
@@__hash_utils_methods_index[self] = Set::new(instance_methods)
|
51
|
+
|
52
|
+
if self != Object
|
53
|
+
@@__hash_utils_methods_index[self] -= @@__hash_utils_object_index
|
54
|
+
end
|
55
|
+
else
|
56
|
+
@@__hash_utils_methods_index[self] = { }
|
57
|
+
_col = @@__hash_utils_methods_index[self]
|
58
|
+
|
59
|
+
instance_methods.each do |i|
|
60
|
+
_col[i] = true
|
61
|
+
end
|
62
|
+
|
63
|
+
if self != Object
|
64
|
+
@@__hash_utils_object_index.each do |i|
|
65
|
+
_col.delete(i)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
@@__hash_utils_methods_index[self].include? call
|
72
|
+
end
|
73
|
+
else
|
74
|
+
throw new Exception('__hash_utils_instance_respond_to? is already implemented. Hash Utils need this method for iternal defensive checking. Continuing is impossible.')
|
75
|
+
end
|
76
|
+
|
10
77
|
##
|
11
78
|
# Returns +true+ if object is an instance of the given classes.
|
12
79
|
#
|
@@ -16,18 +83,20 @@ class Object
|
|
16
83
|
# @since 0.16.0
|
17
84
|
#
|
18
85
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
86
|
+
if not __hash_utils_object_respond_to? :instance_of_any?
|
87
|
+
def instance_of_any?(*classes)
|
88
|
+
if classes.first.array?
|
89
|
+
classes = classes.first
|
90
|
+
end
|
91
|
+
|
92
|
+
classes.each do |cls|
|
93
|
+
if self.instance_of? cls
|
94
|
+
return true
|
95
|
+
end
|
27
96
|
end
|
97
|
+
|
98
|
+
return false
|
28
99
|
end
|
29
|
-
|
30
|
-
return false
|
31
100
|
end
|
32
101
|
|
33
102
|
##
|
@@ -40,21 +109,25 @@ class Object
|
|
40
109
|
# @since 0.16.0
|
41
110
|
#
|
42
111
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
classes.each do |cls|
|
49
|
-
if self.kind_of? cls
|
50
|
-
return true
|
112
|
+
if not __hash_utils_object_respond_to? :kind_of_any?
|
113
|
+
def kind_of_any?(*classes)
|
114
|
+
if classes.first.array?
|
115
|
+
classes = classes.first
|
51
116
|
end
|
117
|
+
|
118
|
+
classes.each do |cls|
|
119
|
+
if self.kind_of? cls
|
120
|
+
return true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
return false
|
52
125
|
end
|
53
|
-
|
54
|
-
return false
|
55
126
|
end
|
56
127
|
|
57
|
-
|
128
|
+
if not __hash_utils_object_respond_to? :is_a_any?
|
129
|
+
alias :is_a_any? :kind_of_any?
|
130
|
+
end
|
58
131
|
|
59
132
|
##
|
60
133
|
# Indicates object is in some object which supports +#include?+.
|
@@ -64,8 +137,10 @@ class Object
|
|
64
137
|
# @since 0.8.0
|
65
138
|
#
|
66
139
|
|
67
|
-
|
68
|
-
range
|
140
|
+
if not __hash_utils_object_respond_to? :in?
|
141
|
+
def in?(range)
|
142
|
+
range.include? self
|
143
|
+
end
|
69
144
|
end
|
70
145
|
|
71
146
|
##
|
@@ -75,8 +150,10 @@ class Object
|
|
75
150
|
# @since 0.7.0
|
76
151
|
#
|
77
152
|
|
78
|
-
|
79
|
-
|
153
|
+
if not __hash_utils_object_respond_to? :to_b
|
154
|
+
def to_b
|
155
|
+
!!self
|
156
|
+
end
|
80
157
|
end
|
81
158
|
|
82
159
|
##
|
@@ -87,10 +164,12 @@ class Object
|
|
87
164
|
# @since 0.12.0
|
88
165
|
#
|
89
166
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
167
|
+
if not __hash_utils_object_respond_to? :**
|
168
|
+
def **(count)
|
169
|
+
result = [ ]
|
170
|
+
count.times { result << self.dup }
|
171
|
+
return result
|
172
|
+
end
|
94
173
|
end
|
95
174
|
|
96
175
|
##
|
@@ -100,8 +179,10 @@ class Object
|
|
100
179
|
# @since 0.14.0
|
101
180
|
#
|
102
181
|
|
103
|
-
|
104
|
-
|
182
|
+
if not __hash_utils_object_respond_to? :io?
|
183
|
+
def io?
|
184
|
+
false
|
185
|
+
end
|
105
186
|
end
|
106
187
|
|
107
188
|
##
|
@@ -111,8 +192,10 @@ class Object
|
|
111
192
|
# @since 0.15.0
|
112
193
|
#
|
113
194
|
|
114
|
-
|
115
|
-
|
195
|
+
if not __hash_utils_object_respond_to? :true?
|
196
|
+
def true?
|
197
|
+
self.kind_of? TrueClass
|
198
|
+
end
|
116
199
|
end
|
117
200
|
|
118
201
|
##
|
@@ -122,8 +205,10 @@ class Object
|
|
122
205
|
# @since 0.15.0
|
123
206
|
#
|
124
207
|
|
125
|
-
|
126
|
-
|
208
|
+
if not __hash_utils_object_respond_to? :false?
|
209
|
+
def false?
|
210
|
+
self.kind_of? FalseClass
|
211
|
+
end
|
127
212
|
end
|
128
213
|
|
129
214
|
##
|
@@ -133,8 +218,10 @@ class Object
|
|
133
218
|
# @since 0.17.0
|
134
219
|
#
|
135
220
|
|
136
|
-
|
137
|
-
|
221
|
+
if not __hash_utils_object_respond_to? :string?
|
222
|
+
def string?
|
223
|
+
self.kind_of? String
|
224
|
+
end
|
138
225
|
end
|
139
226
|
|
140
227
|
##
|
@@ -144,8 +231,10 @@ class Object
|
|
144
231
|
# @since 0.17.0
|
145
232
|
#
|
146
233
|
|
147
|
-
|
148
|
-
|
234
|
+
if not __hash_utils_object_respond_to? :symbol?
|
235
|
+
def symbol?
|
236
|
+
self.kind_of? Symbol
|
237
|
+
end
|
149
238
|
end
|
150
239
|
|
151
240
|
##
|
@@ -155,8 +244,10 @@ class Object
|
|
155
244
|
# @since 0.18.0
|
156
245
|
#
|
157
246
|
|
158
|
-
|
159
|
-
|
247
|
+
if not __hash_utils_object_respond_to? :proc?
|
248
|
+
def proc?
|
249
|
+
self.kind_of? Proc
|
250
|
+
end
|
160
251
|
end
|
161
252
|
|
162
253
|
##
|
@@ -166,8 +257,10 @@ class Object
|
|
166
257
|
# @since 0.17.0
|
167
258
|
#
|
168
259
|
|
169
|
-
|
170
|
-
|
260
|
+
if not __hash_utils_object_respond_to? :number?
|
261
|
+
def number?
|
262
|
+
self.kind_of? Numeric
|
263
|
+
end
|
171
264
|
end
|
172
265
|
|
173
266
|
##
|
@@ -177,8 +270,10 @@ class Object
|
|
177
270
|
# @since 0.17.0
|
178
271
|
#
|
179
272
|
|
180
|
-
|
181
|
-
|
273
|
+
if not __hash_utils_object_respond_to? :boolean?
|
274
|
+
def boolean?
|
275
|
+
self.true? or self.false?
|
276
|
+
end
|
182
277
|
end
|
183
278
|
|
184
279
|
##
|
@@ -188,8 +283,10 @@ class Object
|
|
188
283
|
# @since 0.17.0
|
189
284
|
#
|
190
285
|
|
191
|
-
|
192
|
-
|
286
|
+
if not __hash_utils_object_respond_to? :array?
|
287
|
+
def array?
|
288
|
+
self.kind_of? Array
|
289
|
+
end
|
193
290
|
end
|
194
291
|
|
195
292
|
##
|
@@ -199,8 +296,24 @@ class Object
|
|
199
296
|
# @since 0.17.0
|
200
297
|
#
|
201
298
|
|
202
|
-
|
203
|
-
|
299
|
+
if not __hash_utils_object_respond_to? :hash?
|
300
|
+
def hash?
|
301
|
+
self.kind_of? Hash
|
302
|
+
end
|
204
303
|
end
|
205
|
-
|
304
|
+
|
305
|
+
##
|
306
|
+
# Converts object to +Symbol+. In fact, converts it to +String+
|
307
|
+
# and subsequently to +Symbol+ only.
|
308
|
+
#
|
309
|
+
# @return [Symbol] symbol representation of the object
|
310
|
+
# @since 1.1.0
|
311
|
+
#
|
312
|
+
|
313
|
+
if not __hash_utils_object_respond_to? :to_sym
|
314
|
+
def to_sym
|
315
|
+
self.to_s.to_sym
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
206
319
|
end
|