hash-utils 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
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
- def io?
18
- true
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
@@ -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
- def get_module(name)
23
- names = name.split("::")
24
- mod = self
25
-
26
- while not names.empty?
27
- name = names.shift
28
- mod = mod.const_get(name)
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
@@ -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
- def to_boolean(*args)
19
- false
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
@@ -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
- def compare(number)
24
- self <=> number
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
- def positive?
35
- self > 0
36
+ if not self.__hash_utils_instance_respond_to? :positive?
37
+ def positive?
38
+ self > 0
39
+ end
36
40
  end
37
41
 
38
- alias :positive :"positive?"
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
- def negative?
48
- self < 0
53
+ if not self.__hash_utils_instance_respond_to? :negative?
54
+ def negative?
55
+ self < 0
56
+ end
49
57
  end
50
58
 
51
- alias :negative :"negative?"
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
- def negative!
65
- -self.positive!
74
+ if not self.__hash_utils_instance_respond_to? :negative!
75
+ def negative!
76
+ -self.positive!
77
+ end
66
78
  end
67
79
 
68
- alias :"negate!" :"negative!"
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
@@ -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
- def instance_of_any?(*classes)
20
- if classes.first.array?
21
- classes = classes.first
22
- end
23
-
24
- classes.each do |cls|
25
- if self.instance_of? cls
26
- return true
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
- def kind_of_any?(*classes)
44
- if classes.first.array?
45
- classes = classes.first
46
- end
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
- alias :"is_a_any?" :"kind_of_any?"
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
- def in?(range)
68
- range.include? self
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
- def to_b
79
- !!self
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
- def **(count)
91
- result = [ ]
92
- count.times { result << self.dup }
93
- return result
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
- def io?
104
- false
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
- def true?
115
- self.kind_of? TrueClass
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
- def false?
126
- self.kind_of? FalseClass
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
- def string?
137
- self.kind_of? String
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
- def symbol?
148
- self.kind_of? Symbol
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
- def proc?
159
- self.kind_of? Proc
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
- def number?
170
- self.kind_of? Numeric
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
- def boolean?
181
- self.true? or self.false?
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
- def array?
192
- self.kind_of? Array
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
- def hash?
203
- self.kind_of? Hash
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