epitools 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.5.4
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "epitools"
8
- s.version = "0.5.3"
8
+ s.version = "0.5.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["epitron"]
12
- s.date = "2012-04-09"
12
+ s.date = "2012-04-28"
13
13
  s.description = "Miscellaneous utility libraries to make my life easier."
14
14
  s.email = "chris@ill-logic.com"
15
15
  s.extra_rdoc_files = [
@@ -1,4 +1,4 @@
1
- #require 'epitools'
1
+ require 'epitools/colored'
2
2
 
3
3
  class String
4
4
 
@@ -8,9 +8,14 @@ class String
8
8
  #
9
9
  # The pattern can be a string or a regular expression.
10
10
  #
11
- def highlight(pattern, color=:light_yellow)
11
+ def highlight(pattern, color=:light_yellow, &block)
12
12
  pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a? String
13
- gsub(pattern) { |match| match.send(color) }
13
+
14
+ if block_given?
15
+ gsub(pattern, &block)
16
+ else
17
+ gsub(pattern) { |match| match.send(color) }
18
+ end
14
19
  end
15
20
 
16
21
  end
@@ -91,12 +91,14 @@ module Colored
91
91
  15 => :light_white,
92
92
  }
93
93
 
94
- VALID_COLORS = Set.new(
95
- COLORS.keys +
96
- COLORS.map { |k,v| "light_#{k}" } +
97
- COLORS.map { |k,v| "bright_#{k}" } +
98
- ["grey", "gray"]
99
- )
94
+ VALID_COLORS = begin
95
+ normal = COLORS.keys
96
+ lights = normal.map { |fore| "light_#{fore}" }
97
+ brights = normal.map { |fore| "bright_#{fore}" }
98
+ on_backgrounds = normal.map { |fore| normal.map { |back| "#{fore}_on_#{back}" } }.flatten
99
+
100
+ Set.new(normal + lights + brights + on_backgrounds + ["grey", "gray"])
101
+ end
100
102
 
101
103
  COLORS.each do |color, value|
102
104
  define_method(color) do
@@ -248,10 +250,7 @@ module Colored
248
250
  #
249
251
  def valid_tag?(tag)
250
252
  VALID_COLORS.include?(tag) or
251
- (
252
- tag =~ /^\d+$/ and
253
- BBS_COLOR_TABLE.include?(tag.to_i)
254
- )
253
+ (tag =~ /^\d+$/ and BBS_COLOR_TABLE.include?(tag.to_i) )
255
254
  end
256
255
 
257
256
  #
@@ -169,9 +169,7 @@ class Range
169
169
  # Pick a random number from the range.
170
170
  #
171
171
  def rand
172
- magnitude = last-first
173
- magnitude += 1 if not exclude_end?
174
- Kernel.rand(magnitude)+first
172
+ Kernel.rand(self)
175
173
  end
176
174
 
177
175
  end
@@ -178,7 +178,7 @@ module Enumerable
178
178
 
179
179
  end
180
180
  end
181
-
181
+
182
182
  alias_method :recursive_map, :deep_map
183
183
  alias_method :map_recursively, :deep_map
184
184
  alias_method :map_recursive, :deep_map
@@ -188,16 +188,26 @@ module Enumerable
188
188
  # recursively on that element.
189
189
  #
190
190
  # Example:
191
- # [ [1,2], [3,4] ].deep_map{|e| e ** 2 } #=> [ [1,4], [9,16] ]
191
+ # [ [1,2], [3,4] ].deep_select{|e| e % 2 == 0 } #=> [ [2], [4] ]
192
192
  #
193
193
  def deep_select(depth=nil, &block)
194
- select do |e|
195
- if (e.is_a? Array or e.is_a? Enumerable) and (depth && depth > 0)
196
- e.select(depth-1, &block)
194
+ map do |*args|
195
+
196
+ if depth.nil? or depth > 0
197
+
198
+ case obj
199
+ when Hash
200
+
201
+ when Array, Enumerable
202
+ result = obj.deep_select(depth ? depth-1 : nil, &block)
203
+ result.any? ? result : nil
204
+ end
205
+
197
206
  else
198
- block.call(e)
207
+ obj if block.call(obj)
199
208
  end
200
- end
209
+
210
+ end.compact
201
211
  end
202
212
 
203
213
  alias_method :recursive_select, :deep_select
@@ -41,7 +41,7 @@ class Numeric
41
41
  if block_given?
42
42
  Array.new(self, &block)
43
43
  else
44
- Array.new(self)
44
+ (0...self).to_a
45
45
  end
46
46
  end
47
47
 
@@ -11,6 +11,11 @@ describe String do
11
11
  "xxxmatchzzz".highlight(/m.+h/, color).should == highlighted
12
12
  "xxxmatchzzz".highlight(/MATCH/i, color).should == highlighted
13
13
  end
14
+
15
+ it "highlights with a block" do
16
+ result = "xxxmatchxxx".highlight(/match/) { |match| "<8>#{match}</8>" }
17
+ result.should == "xxx<8>match</8>xxx"
18
+ end
14
19
 
15
20
  it "cmds" do
16
21
  cmd( ['test -f ?', __FILE__] ).should == true
@@ -23,7 +23,8 @@ describe "Colored strings" do
23
23
  "<magenta>hello".colorize.should == "<purple>hello".colorize
24
24
  "<gray>hello".colorize.should == "<light_black>hello".colorize
25
25
  lambda { "</blue>".colorize }.should raise_error
26
- end
26
+ "<black_on_yellow>hello".colorize.should == "hello".black_on_yellow
27
+ end
27
28
 
28
29
  end
29
30
 
@@ -388,7 +388,7 @@ describe Enumerable do
388
388
  end
389
389
 
390
390
  it "selects deeply" do
391
- [[1,2],[3,4]].deep_select {|e| e % 2 == 0 }.should == [2,4]
391
+ [[1,2],[3,4]].deep_select {|e| e % 2 == 0 }.should == [[2],[4]]
392
392
  {1=>2, 3=>{4=>5, 6=>7}}.deep_select {|k,v| k == 1 }.should == {1=>2}
393
393
  #[1,2,3,4].deep_select {|e| e ** 2}.should == [1,4,9,16]
394
394
  #[[],[],1,2,3,4].deep_select {|e| e ** 2}.should == [[], [], 1, 4, 9, 16]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-09 00:00:00.000000000 Z
12
+ date: 2012-04-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec