quality_extensions 1.3.0 → 1.3.1

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.
@@ -0,0 +1,66 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2012, Tyler Rick
4
+ # License:: Ruby License
5
+ # Submit to Facets?:: Yes
6
+ # Developer notes::
7
+ # History::
8
+ #++
9
+
10
+
11
+ module Enumerable
12
+ # Returns the indexes of the elements that match +regexp+.
13
+ #
14
+ # %w[a ab abc].grep_indexes(/a$/) => [0]
15
+ # %w[a ab abc].grep_indexes(/b/) => [1, 2]
16
+ #
17
+ def grep_indexes(regexp)
18
+ indexes = []
19
+ each_with_index {|el, i|
20
+ indexes << i if el =~ regexp
21
+ }
22
+ indexes
23
+ end
24
+ end
25
+
26
+ # _____ _
27
+ # |_ _|__ ___| |_
28
+ # | |/ _ \/ __| __|
29
+ # | | __/\__ \ |_
30
+ # |_|\___||___/\__|
31
+ #
32
+ =begin test
33
+ require 'rspec/autorun'
34
+
35
+ describe 'Enumerable#grep_indexes' do
36
+
37
+ describe %w[a ab abc] do
38
+ it { subject.grep_indexes(/a$/).should == [0 ] }
39
+ it { subject.grep_indexes(/a/). should == [0, 1, 2] }
40
+ it { subject.grep_indexes(/b/). should == [ 1, 2] }
41
+ it { subject.grep_indexes(/c/). should == [ 2] }
42
+ end
43
+
44
+ describe [
45
+ "gems/ruby-debug-base19x-0.11.30.pre10/lib/ruby-debug-base.rb:55:in `at_line'",
46
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:52:in `rescue in synchronize'",
47
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:44:in `synchronize'",
48
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/finders.rb:29:in `find'",
49
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/element.rb:177:in `block in find'",
50
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:45:in `synchronize'",
51
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/element.rb:177:in `find'",
52
+ "features/step_definitions/web_steps.rb:163:in `select'",
53
+ "bundler/gems/capybara-555008c74751/lib/capybara/session.rb:291:in `select'",
54
+ "bundler/gems/capybara-555008c74751/lib/capybara/dsl.rb:43:in `select'",
55
+ "features/step_definitions/javascript_steps.rb:87:in `block (2 levels) in <top (required)>'",
56
+ ] do
57
+ it do
58
+ subject.grep(/in `synchronize'/).should == [
59
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:44:in `synchronize'",
60
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:45:in `synchronize'",
61
+ ]
62
+ end
63
+ it { subject.grep_indexes(/in `synchronize'/).should == [2, 5] }
64
+ end
65
+ end
66
+ =end
@@ -0,0 +1,81 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2012, Tyler Rick
4
+ # License:: Ruby License
5
+ # Submit to Facets?:: Yes
6
+ # Developer notes::
7
+ # History::
8
+ #++
9
+
10
+
11
+ require 'facets/enumerable/map_send'
12
+ require 'facets/kernel/require_relative'
13
+ require_relative 'grep_indexes'
14
+
15
+ module Enumerable
16
+ # For each element that matches +regexp+ return the element that is +offset+ elements forward.
17
+ #
18
+ # Examples:
19
+ #
20
+ # %w[1 2 3].grep_plus_offset(/1/, -1) => [nil]
21
+ # %w[1 2 3].grep_plus_offset(/1/, 0) => ['1']
22
+ # %w[1 2 3].grep_plus_offset(/1/, 1) => ['2']
23
+ # %w[1 2 3].grep_plus_offset(/1/, 2) => ['3']
24
+ # %w[1 2 3].grep_plus_offset(/1/, 3) => [nil]
25
+ #
26
+ # caller(0).grep_plus_offset(/in `synchronize'/, 1) => the line that *called* synchronize
27
+ #
28
+ def grep_plus_offset(regexp, offset, wrap_around = false)
29
+ indexes = grep_indexes(regexp).map_send(:+, offset)
30
+ # If any indexes are negative, replace with (maximum index + 1) so that values_at will return
31
+ # nil for that element (instead of returning an element from the end -- values_at(-1) returns
32
+ # the last element, for example), the same as how providing a positive that results in an offset
33
+ # > maximum_index (length - 1) results in a nil being returned for that index.
34
+ indexes.map! {|_| _ < 0 ? length : _ } unless wrap_around
35
+ values_at *indexes
36
+ end
37
+ end
38
+
39
+
40
+ # _____ _
41
+ # |_ _|__ ___| |_
42
+ # | |/ _ \/ __| __|
43
+ # | | __/\__ \ |_
44
+ # |_|\___||___/\__|
45
+ #
46
+ =begin test
47
+ require 'rspec/autorun'
48
+
49
+ describe 'Enumerable#grep_indexes' do
50
+
51
+ describe %w[1 2 3] do
52
+ it { subject.grep_plus_offset(/1/, -1, true).should == ['3'] }
53
+ it { subject.grep_plus_offset(/1/, -1).should == [nil] }
54
+ it { subject.grep_plus_offset(/1/, 0).should == ['1'] }
55
+ it { subject.grep_plus_offset(/1/, 1).should == ['2'] }
56
+ it { subject.grep_plus_offset(/1/, 2).should == ['3'] }
57
+ it { subject.grep_plus_offset(/1/, 3).should == [nil] }
58
+ end
59
+
60
+ describe [
61
+ "gems/ruby-debug-base19x-0.11.30.pre10/lib/ruby-debug-base.rb:55:in `at_line'",
62
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:52:in `rescue in synchronize'",
63
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:44:in `synchronize'",
64
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/finders.rb:29:in `find'",
65
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/element.rb:177:in `block in find'",
66
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/base.rb:45:in `synchronize'",
67
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/element.rb:177:in `find'",
68
+ "features/step_definitions/web_steps.rb:163:in `select'",
69
+ "bundler/gems/capybara-555008c74751/lib/capybara/session.rb:291:in `select'",
70
+ "bundler/gems/capybara-555008c74751/lib/capybara/dsl.rb:43:in `select'",
71
+ "features/step_definitions/javascript_steps.rb:87:in `block (2 levels) in <top (required)>'",
72
+ ] do
73
+ it do
74
+ subject.grep_plus_offset(/in `synchronize'/, 1).should == [
75
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/finders.rb:29:in `find'",
76
+ "bundler/gems/capybara-555008c74751/lib/capybara/node/element.rb:177:in `find'",
77
+ ]
78
+ end
79
+ end
80
+ end
81
+ =end
@@ -203,6 +203,40 @@ module Enumerable
203
203
  end
204
204
  end
205
205
  end
206
+
207
+
208
+ # Returns all elements of +enum+ starting at the first element for which +block+ is truthy, and continuing until the end.
209
+ #
210
+ # Examples:
211
+ #
212
+ # (0..3).select_from {|v| v == 1} # => [1, 2, 3]
213
+ # (0..3).select_from(false) {|v| v == 1} # => [2, 3]
214
+ #
215
+ # {'a'=>1, 'b'=>2, 'c'=>3, 'd'=>1}.select_from {|k, v| v == 2} ) # => {"b"=>2, "c"=>3, "d"=>1}
216
+ # {'a'=>1, 'b'=>2, 'c'=>3, 'd'=>1}.select_from(false) {|k, v| v == 2} ) # => {"c"=>3, "d"=>1}
217
+ #
218
+ # Compare to Array#from in ActiveSupport.
219
+ # (maybe should rename to #from?)
220
+ #
221
+ def select_from(inclusive = true)
222
+ return self unless block_given?
223
+
224
+ selecting = false
225
+ select do |*args|
226
+ # Once we hit the first truthy result, selecting will be true to the end
227
+ select_this_el = !!yield(*args)
228
+ this_is_first_true = !selecting && select_this_el
229
+ selecting = selecting || select_this_el
230
+ if inclusive
231
+ selecting
232
+ else
233
+ selecting && !this_is_first_true
234
+ end
235
+ end
236
+ end
237
+ alias_method :select_all_starting_with, :select_from
238
+ alias_method :reject_until, :select_from
239
+
206
240
  end
207
241
 
208
242
 
@@ -394,6 +428,37 @@ class SelectUntilLastTest < Test::Unit::TestCase
394
428
  end
395
429
 
396
430
  end
397
- =end
398
431
 
432
+ class SelectFromTest < Test::Unit::TestCase
433
+ def test_simplest_case
434
+ assert_equal [1], [1].select_from {|v| v == 1}
435
+ assert_equal [], [1].select_from(false) {|v| v == 1} # if exclusive, we won't even return the one and only element
436
+ end
399
437
 
438
+ # So basic we could have just used select {|v| v >= 2}
439
+ def test_basic_usage
440
+ assert_equal [2, 3, 4], [1, 2, 3, 4].select_from {|v| v == 2}
441
+ assert_equal [ 3, 4], [1, 2, 3, 4].select_from(false) {|v| v == 2}
442
+ end
443
+
444
+ def test_with_duplicate_values
445
+ assert_equal [2, 1, 2], [1, 2, 1, 2].select_from {|v| v == 2}
446
+ assert_equal [ 1, 2], [1, 2, 1, 2].select_from(false) {|v| v == 2}
447
+ end
448
+ end
449
+
450
+ # This was an idea -- probably a bad idea...
451
+ #class SelectFromUntilTest < Test::Unit::TestCase
452
+ # def test_simplest_case
453
+ # assert_equal [1], [1].select_from_until {|v| v == 1, v == 1}
454
+ # assert_equal [], [1].select_from_until(false) {|v| v == 1, v == 1} # if exclusive, we won't even return the one and only element
455
+ # end
456
+ #
457
+ # def test_basic_usage
458
+ # # So basic we could have just used:
459
+ # assert_equal [2, 3, 4], [1, 2, 3, 4].select {|v| (2..4).include? v}
460
+ # assert_equal [2, 3, 4], [1, 2, 3, 4].select_from_until {|v| v == 2, v == 4}
461
+ # assert_equal [ 3, 4], [1, 2, 3, 4].select_from_until(false) {|v| v == 2, v == 4}
462
+ # end
463
+ #end
464
+ =end
@@ -11,11 +11,9 @@ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
11
11
  require 'rubygems'
12
12
  require 'facets/module/alias_method_chain'
13
13
 
14
- unless defined?(Symbol::HasSymbolSupport)
14
+ unless :a =~ /a/
15
15
 
16
16
  class Symbol
17
- HasSymbolSupport = true
18
-
19
17
  def match(regexp)
20
18
  to_s.match(regexp)
21
19
  end
@@ -92,10 +90,10 @@ module Enumerable
92
90
  }.grep_without_regexp_support_for_symbols(pattern, &block)
93
91
  end
94
92
  alias_method_chain :grep, :regexp_support_for_symbols
95
-
93
+
96
94
  end
97
95
 
98
- end # unless defined?(Symbol::HasSymbolSupport)
96
+ end # unless :a =~ /a/
99
97
 
100
98
  # _____ _
101
99
  # |_ _|__ ___| |_
@@ -1,3 +1,3 @@
1
1
  module QualityExtensions
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
metadata CHANGED
@@ -1,30 +1,24 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: quality_extensions
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.1
4
5
  prerelease:
5
- version: 1.3.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Tyler Rick
9
9
  - and others
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
-
14
- date: 2011-02-13 00:00:00 -08:00
15
- default_executable:
13
+ date: 2012-06-08 00:00:00.000000000 Z
16
14
  dependencies: []
17
-
18
15
  description: A collection of reusable Ruby methods which are not (yet) in Facets.
19
- email:
16
+ email:
20
17
  - github.com@tylerrick.com
21
18
  executables: []
22
-
23
19
  extensions: []
24
-
25
20
  extra_rdoc_files: []
26
-
27
- files:
21
+ files:
28
22
  - .gitignore
29
23
  - Gemfile
30
24
  - License
@@ -154,6 +148,8 @@ files:
154
148
  - lib/quality_extensions/dir/each_child.rb
155
149
  - lib/quality_extensions/enumerable/all_same.rb
156
150
  - lib/quality_extensions/enumerable/every.rb
151
+ - lib/quality_extensions/enumerable/grep_indexes.rb
152
+ - lib/quality_extensions/enumerable/grep_plus_offset.rb
157
153
  - lib/quality_extensions/enumerable/grep_with_index.rb
158
154
  - lib/quality_extensions/enumerable/grepv.rb
159
155
  - lib/quality_extensions/enumerable/group_by_and_map.rb
@@ -281,33 +277,28 @@ files:
281
277
  - lib/quality_extensions/version.rb
282
278
  - quality_extensions.gemspec
283
279
  - test/all.rb
284
- has_rdoc: true
285
280
  homepage: http://github.com/TylerRick/quality_extensions
286
281
  licenses: []
287
-
288
282
  post_install_message:
289
283
  rdoc_options: []
290
-
291
- require_paths:
284
+ require_paths:
292
285
  - lib
293
- required_ruby_version: !ruby/object:Gem::Requirement
286
+ required_ruby_version: !ruby/object:Gem::Requirement
294
287
  none: false
295
- requirements:
296
- - - ">="
297
- - !ruby/object:Gem::Version
298
- version: "0"
299
- required_rubygems_version: !ruby/object:Gem::Requirement
288
+ requirements:
289
+ - - ! '>='
290
+ - !ruby/object:Gem::Version
291
+ version: '0'
292
+ required_rubygems_version: !ruby/object:Gem::Requirement
300
293
  none: false
301
- requirements:
302
- - - ">="
303
- - !ruby/object:Gem::Version
304
- version: "0"
294
+ requirements:
295
+ - - ! '>='
296
+ - !ruby/object:Gem::Version
297
+ version: '0'
305
298
  requirements: []
306
-
307
299
  rubyforge_project: quality_extensions
308
- rubygems_version: 1.5.2
300
+ rubygems_version: 1.8.15
309
301
  signing_key:
310
302
  specification_version: 3
311
303
  summary: A collection of reusable Ruby methods which are not (yet) in Facets.
312
304
  test_files: []
313
-