epitools 0.5.136 → 0.5.137

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c080c53832ae049dd0ac7abedc41844d515b759fd081f4541855359a21eb75a
4
- data.tar.gz: f6871df3e1da9c2cdb315ab49a5c76cea1eb6dfc8f776d6a3f44b3e6d59844fd
3
+ metadata.gz: 8b483b258d704fbf993bbab386612c813ae695444da9badcd8797c4fcebdc877
4
+ data.tar.gz: dbdf278b2e463cf45803cd0278243ee1e2bf7737879bdd2fd403eb340a7a5613
5
5
  SHA512:
6
- metadata.gz: d93c4452eb2e0537265eb95497bf6a0ca9d9022f2ab099972a64904465eccabbe0d3f1901ff2b711940e4603bb6e7afd7c56f7b9b93eea4d9d7762f6453ed3ee
7
- data.tar.gz: 75a2d4e10cf13071f4a2cab7239f6a2db7c3b0bb66a68ce603c75bbe5051c15d742308d9f370cc9fb045e6db2492942f8cd37a65db40ba235655e1ea4d429ac8
6
+ metadata.gz: '0585860f4e91323260513ed1c3e9cc6c49be6ceef4345a184d14b734ff61529c0bce4be6ffd2520e019d51cd9dc3f34a7895792c3c16da1cebd7b5b1ef2907b6'
7
+ data.tar.gz: 6459b4b90495b0c1f274bfa0053d99c386beba31beb0ce649c157cba7777155e96bed02255b108f096210062f95270bdba124207015cb8d62814ae1f863db0c9
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.136
1
+ 0.5.137
@@ -1,3 +1,4 @@
1
+ require 'epitools/minimal'
1
2
 
2
3
  class Array
3
4
 
@@ -0,0 +1,84 @@
1
+ require 'epitools/minimal'
2
+
3
+ class Date
4
+
5
+ def self.find(direction=:backward, &block)
6
+ Date.today.find(direction, &block)
7
+ end
8
+ alias_class_method :search, :find
9
+
10
+ def self.find(direction=:backward, &block)
11
+ Date.today.find(direction, &block)
12
+ end
13
+ alias_class_method :search, :find
14
+
15
+ def self.find_bidirectional(&block)
16
+ Date.today.find_bidirectional(&block)
17
+ end
18
+ alias_class_method :find_both, :find_bidirectional
19
+ alias_class_method :search_both, :find_bidirectional
20
+ alias_class_method :search_bidirectional, :find_bidirectional
21
+
22
+
23
+ #
24
+ # Returns an enumerator which searches through the calendar a day at a time, returning every date that match the block.
25
+ #
26
+ # Examples:
27
+ # >> Date.find(:forward) { year % 42 == 0 }
28
+ # >> Date.find(:backward) { month == 1 }
29
+ # >> Date.parse("1900-01-01").find_bidirectional { wednesday? }
30
+ # >> Date.today.find_forward { wednesday? and day == 4 }.take(10)
31
+ #
32
+ # (Note: the block doesn't need a parameter; every method you call is sent directly to the date object that's being evaluated)
33
+ #
34
+ def find(direction=:backward, &block)
35
+ raise ArgumentError.new("This method requires a block") unless block_given?
36
+ Enumerator.new do |y|
37
+ p = self
38
+
39
+ loop do
40
+ y << p if p.instance_eval(&block)
41
+
42
+ case direction
43
+ when :forward, :forwards
44
+ p = p.next_day
45
+ when :backward, :backwards
46
+ p = p.prev_day
47
+ else
48
+ raise "Error: Unknown date search direction #{direction.inspect}"
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ alias_method :search, :find
55
+
56
+
57
+ def find_forwards(&block)
58
+ find(:forwards, &block)
59
+ end
60
+
61
+ def find_backwards(&block)
62
+ find(:backwards, &block)
63
+ end
64
+
65
+ def find_bidirectional(&block)
66
+ f = find(:forward, &block)
67
+ b = find(:backward, &block)
68
+
69
+ Enumerator.new do |y|
70
+ loop do
71
+ y << b.next
72
+ y << f.next
73
+ end
74
+ end
75
+ end
76
+
77
+ alias_method :find_backward, :find_backwards
78
+ alias_method :find_forward, :find_forwards
79
+
80
+ alias_method :find_both, :find_bidirectional
81
+ alias_method :search_both, :find_bidirectional
82
+ alias_method :search_bidirectional, :find_bidirectional
83
+
84
+ end
@@ -27,6 +27,14 @@ class IO
27
27
  end
28
28
  end
29
29
 
30
+ #
31
+ # Unmarshal the IO (transform it into Ruby datatypes).
32
+ #
33
+ def unmarshal
34
+ Marshal.load self
35
+ end
36
+ alias_method :from_marshal, :unmarshal
37
+
30
38
  end
31
39
 
32
40
 
@@ -172,6 +172,11 @@ class DateTime
172
172
  def to_f; to_time.to_f; end
173
173
  end
174
174
 
175
+ class Date
176
+ def inspect
177
+ strftime("#<Date: %Y-%m-%d (%a %b %d, %Y)>")
178
+ end
179
+ end
175
180
 
176
181
  class NilClass
177
182
 
@@ -33,17 +33,28 @@ class Numeric
33
33
  # 12.clamp(0..100) #=> 12
34
34
  # -38817112.clamp(0..100) #=> 0
35
35
  #
36
- def clamp(range)
37
- if self < range.first
38
- range.first
39
- elsif self >= range.last
40
- if range.exclude_end?
41
- range.last - 1
36
+ unless method_defined? :clamp
37
+ def clamp(*args)
38
+ case args.size
39
+ when 0
40
+ raise "Error: clamp takes either a Range object or 2 arguments"
41
+ when 1
42
+ range = args.first
43
+ when 2
44
+ range = (args.first..args.second)
45
+ end
46
+
47
+ if self < range.first
48
+ range.first
49
+ elsif self >= range.last
50
+ if range.exclude_end?
51
+ range.last - 1
52
+ else
53
+ range.last
54
+ end
42
55
  else
43
- range.last
56
+ self
44
57
  end
45
- else
46
- self
47
58
  end
48
59
  end
49
60
 
@@ -28,6 +28,27 @@ class String
28
28
  gsub("\r\n", "\n")
29
29
  end
30
30
 
31
+ #
32
+ # Convert the string from one encoding into another (eg: convert_encoding('cp437', 'utf-8'))
33
+ #
34
+ def convert_encoding(from, to)
35
+ force_encoding(from).encode(to)
36
+ end
37
+
38
+ #
39
+ # Convert the string to utf-8, forcing another encoding
40
+ #
41
+ def to_utf8(from="cp437")
42
+ convert_encoding(from, "utf-8")
43
+ end
44
+
45
+ #
46
+ # Convert a string from cp437 into utf-8
47
+ #
48
+ def from_cp437
49
+ to_utf8
50
+ end
51
+
31
52
  #
32
53
  # Escape shell characters (globs, quotes, parens, etc.)
33
54
  #
@@ -278,12 +299,29 @@ class String
278
299
  enumerable :each_slice
279
300
 
280
301
  #
281
- # The Infamous Caesar-Cipher. Unbreakable to this day.
302
+ # Implements the Infamous Caesar-Cipher. Unbreakable to this day.
282
303
  #
283
304
  def rot13
284
305
  tr('n-za-mN-ZA-M', 'a-zA-Z')
285
306
  end
286
307
 
308
+ #
309
+ # Implements the unfamous non-Caesar ciphers. Want to `rot` your string more or less than 13? You've come to the right place!
310
+ # (NOTE: Currently only works on American letters. No fancy accents or cyrillic letters please!)
311
+ #
312
+ def rot(amount=1)
313
+ chars.map do |c|
314
+ case c
315
+ when ?a..?z
316
+ (((c.ord - ?a.ord + amount) % 26) + ?a.ord).chr
317
+ when ?A..?Z
318
+ (((c.ord - ?A.ord + amount) % 26) + ?A.ord).chr
319
+ else
320
+ c
321
+ end
322
+ end.join
323
+ end
324
+
287
325
  #
288
326
  # Cache an `URI::RFC2396_Parser` instance, because it's slowwww to initialize
289
327
  #
@@ -351,6 +389,13 @@ class String
351
389
  bs.with_index.inject(0) { |sum,(b,i)| (b << (8*i)) + sum }
352
390
  end
353
391
 
392
+ #
393
+ # Convert the bytes of this string to hexadecimcal digits
394
+ #
395
+ def to_hex
396
+ unpack("H*").first
397
+ end
398
+
354
399
  #
355
400
  # Cached constants for base conversion.
356
401
  #
@@ -405,6 +450,14 @@ class String
405
450
  BEncode.load(self)
406
451
  end
407
452
 
453
+ #
454
+ # CRC32 the string
455
+ #
456
+ def crc32
457
+ Zlib.crc32(self).to_hex
458
+ end
459
+ alias_method :crc, :crc32
460
+
408
461
  #
409
462
  # MD5 the string
410
463
  #
@@ -426,6 +479,8 @@ class String
426
479
  Digest::SHA256.hexdigest self
427
480
  end
428
481
 
482
+
483
+
429
484
  #
430
485
  # gzip the string
431
486
  #
@@ -490,7 +545,7 @@ class String
490
545
  # Unmarshal the string (transform it into Ruby datatypes).
491
546
  #
492
547
  def unmarshal
493
- Marshal.restore self
548
+ Marshal.load self
494
549
  end
495
550
  alias_method :from_marshal, :unmarshal
496
551
 
@@ -15,7 +15,9 @@ require 'epitools/core_ext/hash'
15
15
  require 'epitools/core_ext/numbers'
16
16
  require 'epitools/core_ext/truthiness'
17
17
  require 'epitools/core_ext/range'
18
+
18
19
  require 'epitools/core_ext/time'
20
+ require 'epitools/core_ext/date'
19
21
 
20
22
  require 'epitools/core_ext/argv'
21
23
  require 'epitools/core_ext/file'