rearmed 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- array_enabled = Rearmed.enabled_patches[:array] == true
1
+ array_enabled = Rearmed.enabled_patches == :all || Rearmed.enabled_patches[:array] == true
2
2
 
3
3
  Array.module_eval do
4
4
  if array_enabled || Rearmed.dig(Rearmed.enabled_patches, :array, :not_empty)
@@ -1,4 +1,4 @@
1
- if Rearmed.enabled_patches[:date] == true || Rearmed.dig(Rearmed.enabled_patches, :date, :now)
1
+ if Rearmed.enabled_patches == :all || Rearmed.enabled_patches[:date] == true || Rearmed.dig(Rearmed.enabled_patches, :date, :now)
2
2
 
3
3
  require 'date'
4
4
 
@@ -1,18 +1,18 @@
1
- enumerable_enabled = Rearmed.enabled_patches[:enumerable] == true
1
+ enumerable_enabled = Rearmed.enabled_patches == :all || Rearmed.enabled_patches[:enumerable] == true
2
2
 
3
3
  Enumerable.module_eval do
4
4
  if enumerable_enabled || Rearmed.dig(Rearmed.enabled_patches, :enumerable, :natural_sort_by)
5
5
  def natural_sort_by
6
- Rearmed.natural_sort_by(self){|x| yield(x)}
6
+ Rearmed::Enumerable.natural_sort_by(self){|x| yield(x)}
7
7
  end
8
8
  end
9
9
 
10
10
  if enumerable_enabled || Rearmed.dig(Rearmed.enabled_patches, :enumerable, :natural_sort)
11
11
  def natural_sort(options={})
12
12
  if block_given?
13
- Rearmed.natural_sort(self, options){|x| yield(x)}
13
+ Rearmed::Enumerable.natural_sort(self, options){|x| yield(x)}
14
14
  else
15
- Rearmed.natural_sort(self, options)
15
+ Rearmed::Enumerable.natural_sort(self, options)
16
16
  end
17
17
  end
18
18
  end
@@ -20,9 +20,9 @@ Enumerable.module_eval do
20
20
  if enumerable_enabled || Rearmed.dig(Rearmed.enabled_patches, :enumerable, :select_map)
21
21
  def select_map
22
22
  if block_given?
23
- Rearmed.select_map(self){|x| yield(x)}
23
+ Rearmed::Enumerable.select_map(self){|x| yield(x)}
24
24
  else
25
- Rearmed.select_map(self)
25
+ Rearmed::Enumerable.select_map(self)
26
26
  end
27
27
  end
28
28
  end
@@ -1,9 +1,9 @@
1
- hash_enabled = Rearmed.enabled_patches[:hash] == true
1
+ hash_enabled = Rearmed.enabled_patches == :all || Rearmed.enabled_patches[:hash] == true
2
2
 
3
3
  Hash.class_eval do
4
4
  if !{}.respond_to?(:compact) && (hash_enabled || Rearmed.dig(Rearmed.enabled_patches, :hash, :compact))
5
5
  def compact
6
- Rearmed.hash_compact(self)
6
+ Rearmed::Hash.compact(self)
7
7
  end
8
8
 
9
9
  def compact!
@@ -20,16 +20,16 @@ Hash.class_eval do
20
20
  if hash_enabled || Rearmed.dig(Rearmed.enabled_patches, :hash, :join)
21
21
  def join(delimiter=', ', &block)
22
22
  if block_given?
23
- Rearmed.hash_join(self, delimiter, &block)
23
+ Rearmed::Hash.join(self, delimiter, &block)
24
24
  else
25
- Rearmed.hash_join(self, delimiter)
25
+ Rearmed::Hash.join(self, delimiter)
26
26
  end
27
27
  end
28
28
  end
29
29
 
30
30
  if hash_enabled || Rearmed.dig(Rearmed.enabled_patches, :hash, :only)
31
31
  def only(*keys)
32
- Rearmed.hash_only(self, *keys)
32
+ Rearmed::Hash.only(self, *keys)
33
33
  end
34
34
 
35
35
  def only!(*keys)
@@ -43,7 +43,7 @@ Hash.class_eval do
43
43
 
44
44
  if hash_enabled || Rearmed.dig(Rearmed.enabled_patches, :hash, :to_struct)
45
45
  def to_struct
46
- Rearmed.hash_to_struct(self)
46
+ Rearmed::Hash.to_struct(self)
47
47
  end
48
48
  end
49
49
  end
@@ -0,0 +1,11 @@
1
+ integer_enabled = Rearmed.enabled_patches == :all || Rearmed.enabled_patches[:integer] == true
2
+
3
+ Integer.class_eval do
4
+
5
+ if integer_enabled || Rearmed.dig(Rearmed.enabled_patches, :integer, :length)
6
+ def length
7
+ Math.log10(self.abs).to_i + 1
8
+ end
9
+ end
10
+
11
+ end
@@ -1,9 +1,9 @@
1
- object_enabled = Rearmed.enabled_patches[:object] == true
1
+ object_enabled = Rearmed.enabled_patches == :all || Rearmed.enabled_patches[:object] == true
2
2
 
3
3
  Object.class_eval do
4
4
  if object_enabled || Rearmed.dig(Rearmed.enabled_patches, :object, :not_nil)
5
5
  def not_nil?
6
- !nil?
6
+ !self.nil?
7
7
  end
8
8
  end
9
9
 
@@ -15,4 +15,22 @@ Object.class_eval do
15
15
  array.include?(self)
16
16
  end
17
17
  end
18
+
19
+ if object_enabled || Rearmed.dig(Rearmed.enabled_patches, :object, :bool?)
20
+ def bool?
21
+ self.is_a?(TrueClass) || self.is_a?(FalseClass)
22
+ end
23
+ end
24
+
25
+ if object_enabled || Rearmed.dig(Rearmed.enabled_patches, :object, :false?)
26
+ def false?
27
+ self.is_a?(FalseClass)
28
+ end
29
+ end
30
+
31
+ if object_enabled || Rearmed.dig(Rearmed.enabled_patches, :object, :true?)
32
+ def true?
33
+ self.is_a?(TrueClass)
34
+ end
35
+ end
18
36
  end
@@ -1,27 +1,33 @@
1
- string_enabled = Rearmed.enabled_patches[:string] == true
1
+ string_enabled = Rearmed.enabled_patches == :all || Rearmed.enabled_patches[:string] == true
2
2
 
3
3
  String.class_eval do
4
4
  if !''.respond_to?(:casecmp?) && (string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :casecmp?))
5
5
  def casecmp?(str)
6
- Rearmed.casecmp?(self, str)
6
+ Rearmed::String.casecmp?(self, str)
7
7
  end
8
8
  end
9
9
 
10
10
  if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :to_bool)
11
11
  def to_bool
12
- Rearmed.to_bool(self)
12
+ Rearmed::String.to_bool(self)
13
+ end
14
+ end
15
+
16
+ if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :match?)
17
+ def match?(pattern, pos=0)
18
+ match(pattern, pos).not_nil?
13
19
  end
14
20
  end
15
21
 
16
22
  if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :valid_integer)
17
23
  def valid_integer?
18
- Rearmed.valid_integer?(self)
24
+ Rearmed::String.valid_integer?(self)
19
25
  end
20
26
  end
21
27
 
22
28
  if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :valid_float)
23
29
  def valid_float?
24
- Rearmed.valid_float?(self)
30
+ Rearmed::String.valid_float?(self)
25
31
  end
26
32
  end
27
33
 
@@ -1,3 +1,3 @@
1
1
  module Rearmed
2
- VERSION = "1.3.1"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -0,0 +1,353 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'minitest'
7
+ require 'minitest/autorun'
8
+
9
+ Minitest::Assertions.module_eval do
10
+ alias_method :eql, :assert_equal
11
+ end
12
+
13
+ require 'rearmed'
14
+
15
+ Rearmed.enabled_patches = :all
16
+ Rearmed.apply_patches!
17
+
18
+ class RearmedTest < MiniTest::Test
19
+ def setup
20
+ end
21
+
22
+ def teardown
23
+ end
24
+
25
+ def test_string_valid_integer?
26
+ str = '32'
27
+ eql(str.valid_integer?, true)
28
+
29
+ str = '32.2'
30
+ eql(str.valid_integer?, false)
31
+
32
+ str = '32a'
33
+ eql(str.valid_integer?, false)
34
+
35
+ str = '12'
36
+ eql(Rearmed::String.valid_integer?(str), true)
37
+
38
+ assert_raises TypeError do
39
+ Rearmed::String.valid_integer?(1)
40
+ end
41
+ end
42
+
43
+ def test_string_valid_integer?
44
+ str = '132.2'
45
+ eql(str.valid_float?, true)
46
+
47
+ str = '132.2.2'
48
+ eql(str.valid_float?, false)
49
+
50
+ str = '12.1a'
51
+ eql(str.valid_float?, false)
52
+
53
+ str = '1.2'
54
+ eql(Rearmed::String.valid_float?(str), true)
55
+
56
+ assert_raises TypeError do
57
+ Rearmed::String.valid_float?(1)
58
+ end
59
+ end
60
+
61
+ def test_string_to_bool
62
+ str = 'true'
63
+ eql(str.to_bool, true)
64
+
65
+ str = 'false'
66
+ eql(str.to_bool, false)
67
+
68
+ str = 'not true'
69
+ assert_nil(str.to_bool)
70
+
71
+ str = 'true'
72
+ eql(Rearmed::String.to_bool(str), true)
73
+
74
+ assert_raises TypeError do
75
+ Rearmed::String.to_bool(1)
76
+ end
77
+ end
78
+
79
+ def test_string_casecmp?
80
+ str1 = 'foo'
81
+
82
+ str2 = 'foo'
83
+ eql(str1.casecmp?(str2), true)
84
+
85
+ str2 = 'FOO'
86
+ eql(str1.casecmp?(str2), true)
87
+
88
+ str2 = 'foobar'
89
+ eql(str1.casecmp?(str2), false)
90
+
91
+ str2 = 'fo'
92
+ eql(str1.casecmp?(str2), false)
93
+
94
+ str2 = true
95
+ eql(str1.casecmp?(str2), nil)
96
+
97
+ str2 = 'foo'
98
+ eql(Rearmed::String.casecmp?(str1, str2), true)
99
+
100
+ assert_raises TypeError do
101
+ Rearmed::String.casecmp?(1, 'foo')
102
+ end
103
+ end
104
+
105
+ def test_string_match?
106
+ assert_equal 'hello'.match?('he'), true
107
+ assert_equal 'hello'.match?('he', 1), false
108
+ assert_equal 'hello'.match?('o'), true
109
+ assert_equal 'hello'.match?('ol'), false
110
+ assert_equal 'hello'.match?('(.)'), true
111
+ assert_equal 'hello'.match?(/(.)/), true
112
+ assert_equal 'hello'.match?('xx'), false
113
+ end
114
+
115
+ def test_date_now
116
+ assert_equal Date.now, Date.today
117
+ end
118
+
119
+ def test_enumerable_natural_sort
120
+ items = ['1.1', '1.11', '1.2']
121
+ eql(items.natural_sort, ['1.1','1.2','1.11'])
122
+ eql(items.natural_sort(reverse: true), ['1.11','1.2','1.1'])
123
+ eql(Rearmed::Enumerable.natural_sort(items), ['1.1','1.2','1.11'])
124
+ eql(Rearmed::Enumerable.natural_sort(items, reverse: true), ['1.11','1.2','1.1'])
125
+
126
+ assert_raises TypeError do
127
+ Rearmed::Enumerable.natural_sort(1)
128
+ end
129
+ end
130
+
131
+ def test_enumerable_natural_sort_by
132
+ items = [{version: "1.1"}, {version: "1.11"}, {version: "1.2"}]
133
+ eql(items.natural_sort_by{|x| x[:version]}, [{version: "1.1"}, {version: "1.2"}, {version: "1.11"}])
134
+ eql(Rearmed::Enumerable.natural_sort_by(items){|x| x[:version]}, [{version: "1.1"}, {version: "1.2"}, {version: "1.11"}])
135
+
136
+ assert_raises TypeError do
137
+ Rearmed::Enumerable.natural_sort_by(1)
138
+ end
139
+ end
140
+
141
+ def test_enumerable_select_map
142
+ items = [0, 1, 2, 3, nil, false]
143
+ eql(items.select_map{|x| x}, [0,1,2,3])
144
+ eql(Rearmed::Enumerable.select_map(items){|x| x}, [0,1,2,3])
145
+
146
+ assert items.select_map.is_a?(Enumerator)
147
+ assert Rearmed::Enumerable.select_map(items).is_a?(Enumerator)
148
+
149
+ assert_raises TypeError do
150
+ Rearmed::Enumerable.select_map(1)
151
+ end
152
+ end
153
+
154
+ def test_array_delete_first
155
+ array = [1,3,2,1,3,4,1]
156
+ array.delete_first(3)
157
+ eql(array, [1,2,1,3,4,1])
158
+
159
+ array = [1,3,2,1,3,4,1]
160
+ array.delete_first
161
+ eql(array, [3,2,1,3,4,1])
162
+
163
+ array = [1,3,2,1,3,4,1]
164
+ array.delete_first{|x| x != 1}
165
+ eql(array, [1,2,1,3,4,1])
166
+ end
167
+
168
+ def test_array_not_empty?
169
+ eql([nil].not_empty?, true)
170
+ eql([].not_empty?, false)
171
+ end
172
+
173
+ def test_array_dig
174
+ array = [{foo: ['foo','bar']}, {test: 'thing'}]
175
+ eql(array.dig(0, :foo, 1), 'bar')
176
+ assert_nil(array.dig(0, :foo, 2))
177
+ eql(Rearmed.dig(array, 1, :test), 'thing')
178
+ assert_nil(Rearmed.dig(array, 1, :bar))
179
+
180
+ assert_raises TypeError do
181
+ Rearmed.dig(1)
182
+ end
183
+ end
184
+
185
+ def test_hash_only
186
+ hash = {foo: 'foo', bar: 'bar', other: 'other'}
187
+
188
+ eql(hash.only(:foo, :bar), {foo: 'foo', bar: 'bar'})
189
+
190
+ eql(Rearmed::Hash.only(hash, :foo, :bar), {foo: 'foo', bar: 'bar'})
191
+
192
+ assert_raises TypeError do
193
+ Rearmed::Hash.only(1)
194
+ end
195
+
196
+ hash.only!(:foo, :bar)
197
+ eql(hash, {foo: 'foo', bar: 'bar'})
198
+ end
199
+
200
+ def test_has_compact
201
+ hash = {foo: nil, bar: nil, other: 'other'}
202
+
203
+ eql(hash.compact, {other: 'other'})
204
+ eql(Rearmed::Hash.compact(hash), {other: 'other'})
205
+
206
+ assert_raises TypeError do
207
+ Rearmed::Hash.compact(1)
208
+ end
209
+
210
+ hash.compact!
211
+ eql(hash, {other: 'other'})
212
+ end
213
+
214
+ def test_hash_join
215
+ hash = {foo: :bar, bar: :foo}
216
+
217
+ eql(hash.join, "foo: bar, bar: foo")
218
+ eql(hash.join('___'), "foo: bar___bar: foo")
219
+ eql(hash.join{|k,v| v}, "bar, foo")
220
+ eql(hash.join('___'){|k,v| v}, "bar___foo")
221
+ eql(Rearmed::Hash.join(hash), "foo: bar, bar: foo")
222
+ eql(Rearmed::Hash.join(hash, '___'), "foo: bar___bar: foo")
223
+ eql(Rearmed::Hash.join(hash){|k,v| v}, "bar, foo")
224
+ eql(Rearmed::Hash.join(hash, '___'){|k,v| v}, "bar___foo")
225
+
226
+ assert_raises TypeError do
227
+ Rearmed::Hash.join(1)
228
+ end
229
+ end
230
+
231
+ def test_hash_to_struct
232
+ hash = {foo: :bar, bar: :foo}
233
+
234
+ struct = hash.to_struct
235
+ assert(struct.is_a?(Struct))
236
+ eql(struct.foo, :bar)
237
+ eql(struct.bar, :foo)
238
+
239
+ struct = Rearmed::Hash.to_struct(hash)
240
+ assert(struct.is_a?(Struct))
241
+ eql(struct.foo, :bar)
242
+ eql(struct.bar, :foo)
243
+
244
+ assert_raises TypeError do
245
+ Rearmed::Hash.to_struct(1)
246
+ end
247
+ end
248
+
249
+ def test_hash_dig
250
+ hash = {a: {foo: ['bar']}, b: {c: 'c'}}
251
+ eql(hash.dig(:a, :foo, 0), 'bar')
252
+ eql(Rearmed.dig(hash, :b, :c), 'c')
253
+
254
+ assert_raises TypeError do
255
+ Rearmed.dig(1)
256
+ end
257
+ end
258
+
259
+ def test_object_not_nil?
260
+ str = nil
261
+ eql(str.not_nil?, false)
262
+
263
+ str = false
264
+ eql(str.not_nil?, true)
265
+
266
+ str = 'test'
267
+
268
+ eql(str.not_nil?, true)
269
+ end
270
+
271
+ def test_object_in?
272
+ str = 'test'
273
+
274
+ eql(str.in?(['test','abc']), true)
275
+ eql(str.in?(['abc','def']), false)
276
+ eql(str.in?('test','abc'), true)
277
+ eql(str.in?('abc','def'), false)
278
+ eql(str.in?('a test string'), true)
279
+ eql(str.in?('a real string'), false)
280
+ end
281
+
282
+ def test_object_true?
283
+ assert_equal true.true?, true
284
+ assert_equal false.true?, false
285
+ assert_equal nil.true?, false
286
+ assert_equal 'string'.true?, false
287
+ assert_equal :symbol.true?, false
288
+ assert_equal [].true?, false
289
+ hash = {}
290
+ assert_equal hash.true?, false
291
+ end
292
+
293
+ def test_object_false?
294
+ assert_equal false.false?, true
295
+ assert_equal true.false?, false
296
+ assert_equal nil.false?, false
297
+ assert_equal 'string'.false?, false
298
+ assert_equal :symbol.false?, false
299
+ assert_equal [].false?, false
300
+ hash = {}
301
+ assert_equal hash.true?, false
302
+ end
303
+
304
+ def test_object_bool?
305
+ assert_equal true.bool?, true
306
+ assert_equal false.bool?, true
307
+ assert_equal nil.bool?, false
308
+ assert_equal 'string'.bool?, false
309
+ assert_equal :symbol.bool?, false
310
+ assert_equal 1.bool?, false
311
+ assert_equal 0.bool?, false
312
+ end
313
+
314
+ def test_integer_length
315
+ assert_equal 1000.length, 4
316
+ assert_equal (-1000).length, 4
317
+
318
+ assert_raises NoMethodError do
319
+ 1000.0.length
320
+ end
321
+ end
322
+
323
+ def test_enabled_patches
324
+ Rearmed.instance_variable_set(:@applied, false)
325
+
326
+ default = Rearmed.const_get(:DEFAULT_PATCHES)
327
+
328
+ Rearmed.enabled_patches = nil
329
+ assert_equal Rearmed.enabled_patches, default
330
+
331
+ Rearmed.enabled_patches = {}
332
+ assert_equal Rearmed.enabled_patches, default
333
+
334
+ Rearmed.enabled_patches = :all
335
+ assert_equal Rearmed.enabled_patches, :all
336
+
337
+ Rearmed.enabled_patches = {array: true, foo: :foo, hash: nil, enumerable: false, date: {test: :test}}
338
+ assert_equal Rearmed.enabled_patches, default.merge({array: true, date: {test: :test}})
339
+
340
+ [true, false, [], '', 1, :foo, Rearmed].each do |x|
341
+ assert_raises TypeError do
342
+ Rearmed.enabled_patches = x
343
+ end
344
+
345
+ unless x.bool?
346
+ assert_raises TypeError do
347
+ Rearmed.enabled_patches = {array: x}
348
+ end
349
+ end
350
+ end
351
+ end
352
+
353
+ end