gonzo_array_extensions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ lib/gonzo_array_extensions.rb
5
+ lib/gonzo_array_extensions/array.rb
6
+ spec/custom_matchers.rb
7
+ spec/gonzo_array_extensions/array_spec.rb
8
+ spec/gonzo_array_extensions_spec.rb
9
+ spec/spec_helper.rb
10
+ tasks/rspec.rake
data/README.rdoc ADDED
@@ -0,0 +1,73 @@
1
+ = Gonzo Array Extensions
2
+
3
+ Extends the Ruby core Array class, and augments some of its methods with additional functionality.
4
+
5
+ == Install
6
+
7
+ gem install gonzo_array_extensions --source http://gemcutter.org
8
+
9
+ == Usage
10
+
11
+ Gonzo Array Extensions augments the Array methods first, last and include? so you can pass an optional block to each so you can check for additional information.
12
+
13
+ To use the library just include the gem in your code...
14
+
15
+ include 'rubygems'
16
+ include 'gonzo_array_extensions'
17
+
18
+ === Array#first
19
+
20
+ As well as supporting the original functionality of Array#first you can also pass an additional block and the first element(s) matching the block will be returned.
21
+
22
+ a = [ 1, 2, 3, 4, 5, 6 ]
23
+ a.first { |x| x > 3 } #=> 4
24
+ a.first(1) { |x| x > 3 } #=> [4]
25
+ a.first(3) { |x| x > 3 } #=> [4, 5, 6]
26
+ a.first { |x| x > 6 } #=> nil
27
+
28
+ === Array#last
29
+
30
+ As well as supporting the original functionality of Array#last you can also pass an additional block and the last element(s) matching the block will be returned.
31
+
32
+ a = [ 1, 2, 3, 4, 5, 6 ]
33
+ a.last { |x| x < 3 } #=> 2
34
+ a.last(1) { |x| x < 3 } #=> [2]
35
+ a.last(3) { |x| x < 5 } #=> [2, 3, 4]
36
+ a.first { |x| x > 6 } #=> nil
37
+
38
+ === Array#include?
39
+
40
+ As well as supporting the original functionality of Array#include? you can also pass an additional block and the method will return true if any element(s) match.
41
+
42
+ a = [ 1, 2, 3, 4, 5, 6 ]
43
+ a.include? { |x| x < 3 } #=> true
44
+ a.include? { |x| x > 6 } #=> false
45
+
46
+ == Thanks
47
+
48
+ Thats it really!
49
+
50
+ Love Rob.
51
+
52
+ == License
53
+
54
+ Copyright (c) 2010 Robert Oles
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining
57
+ a copy of this software and associated documentation files (the
58
+ "Software"), to deal in the Software without restriction, including
59
+ without limitation the rights to use, copy, modify, merge, publish,
60
+ distribute, sublicense, and/or sell copies of the Software, and to
61
+ permit persons to whom the Software is furnished to do so, subject to
62
+ the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be
65
+ included in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
68
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
69
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
70
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
71
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
72
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
73
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Dir['tasks/**/*.rake'].each { |t| load t }
6
+
7
+ Echoe.new('gonzo_array_extensions', '0.1.0') do |p|
8
+ p.description = "Extends standard library Array with some common gonzo needs"
9
+ p.url = "http://github.com/robertoles/gonzo_array_extensions"
10
+ p.author = "Robert Oles"
11
+ p.email = "robertoles@me.com"
12
+ p.ignore_pattern = ["tmp/*", "script/*"]
13
+ p.development_dependencies = []
14
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{gonzo_array_extensions}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Robert Oles"]
9
+ s.date = %q{2010-02-21}
10
+ s.description = %q{Extends standard library Array with some common gonzo needs}
11
+ s.email = %q{robertoles@me.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/gonzo_array_extensions.rb", "lib/gonzo_array_extensions/array.rb", "tasks/rspec.rake"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/gonzo_array_extensions.rb", "lib/gonzo_array_extensions/array.rb", "spec/custom_matchers.rb", "spec/gonzo_array_extensions/array_spec.rb", "spec/gonzo_array_extensions_spec.rb", "spec/spec_helper.rb", "tasks/rspec.rake", "gonzo_array_extensions.gemspec"]
14
+ s.homepage = %q{http://github.com/robertoles/gonzo_array_extensions}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Gonzo_array_extensions", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{gonzo_array_extensions}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Extends standard library Array with some common gonzo needs}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ module Gonzo
2
+ module ArrayExtensions
3
+ def self.included(base)
4
+ base.instance_eval do
5
+ alias_method_chain :first, :block
6
+ alias_method_chain :last, :block
7
+ alias_method_chain :include?, :block
8
+ end
9
+ end
10
+
11
+ def first_with_block(*args, &block)
12
+ return first_without_block(*args) unless block_given?
13
+ select(&block).first_without_block(*args)
14
+ end
15
+
16
+ def last_with_block(*args, &block)
17
+ return last_without_block(*args) unless block_given?
18
+ select(&block).last_without_block(*args)
19
+ end
20
+
21
+ def include_with_block?(*args, &block)
22
+ return include_without_block?(*args) unless block_given?
23
+ !select(&block).empty?
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'activesupport'
3
+ require 'gonzo_array_extensions/array'
4
+
5
+ if defined? Array
6
+ Array.instance_eval do
7
+ include ActiveSupport::CoreExtensions::Module
8
+ include Gonzo::ArrayExtensions
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ module CustomMatchers
2
+ class IncludeModule
3
+ def initialize(expected_module)
4
+ @expected_module = expected_module
5
+ end
6
+
7
+ def matches?(clazz)
8
+ @clazz = clazz
9
+ @clazz.include?(@expected_module)
10
+ end
11
+
12
+ def failure_message_for_should
13
+ "expected #{@clazz.inspect} to include the module #{@expected_module.inspect}"
14
+ end
15
+
16
+ def failure_message_for_should_not
17
+ "expected #{@clazz.inspect} not to include the module #{@expected_module.inspect}"
18
+ end
19
+ end
20
+
21
+ def include_module(expected_module)
22
+ IncludeModule.new(expected_module)
23
+ end
24
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+ require File.join(ROOT, 'lib', 'gonzo_array_extensions')
3
+
4
+ describe Array do
5
+ describe "#first" do
6
+ describe "maintain its original functionality" do
7
+ it "should return nil if the array is empty" do
8
+ [].first.should be_nil
9
+ end
10
+
11
+ it "should still return the first item" do
12
+ [1,2,3].first.should == 1
13
+ end
14
+
15
+ it "should still return a single item in an array when n is one" do
16
+ [1,2,3].first(1).should == [1]
17
+ end
18
+
19
+ it "should still return an array with the amount of items for n" do
20
+ [1,2,3,4,5].first(3).should == [1,2,3]
21
+ end
22
+ end
23
+
24
+ describe "using a block" do
25
+ it "should return nil if no match is found" do
26
+ ["some", "word", "foobar"].first{ |x| x == "blah" }.should be_nil
27
+ end
28
+
29
+ it "should return the first item which matches the block" do
30
+ ["some", "word", "foobar"].first{ |x| x == "foobar" }.should == "foobar"
31
+ end
32
+
33
+ it "should return the a first item matching the block in an array when n is one" do
34
+ ["some", "word", "foobar"].first(1){ |x| x == "foobar"}.should == ["foobar"]
35
+ end
36
+
37
+ it "should return an array with the first n elements matching the block" do
38
+ [1, "foobar", 2, "foo", "bar"].first(2){ |x| x.instance_of?(String) }.should == ["foobar", "foo"]
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "#last" do
44
+ describe "maintain its original functionality" do
45
+ it "should return nil if the array is empty" do
46
+ [].last.should be_nil
47
+ end
48
+
49
+ it "should still return the last item" do
50
+ [1,2,3].last.should == 3
51
+ end
52
+
53
+ it "should still return a single item in an array when n is one" do
54
+ [1,2,3].last(1).should == [3]
55
+ end
56
+
57
+ it "should still return an array with the amount of items for n" do
58
+ [1,2,3,4,5].last(3).should == [3,4,5]
59
+ end
60
+ end
61
+
62
+ describe "using a block" do
63
+ it "should return nil if no match is found" do
64
+ ["some", "word", "foobar"].last{ |x| x == "blah" }.should be_nil
65
+ end
66
+
67
+ it "should return the first item which matches the block" do
68
+ ["some", "word", "foobar"].last{ |x| x == "some" }.should == "some"
69
+ end
70
+
71
+ it "should return the a first item matching the block in an array when n is one" do
72
+ ["some", "word", "foobar"].last(1){ |x| x == "some"}.should == ["some"]
73
+ end
74
+
75
+ it "should return an array with the first n elements matching the block" do
76
+ [1, "foobar", 2, "foo", "bar"].last(2){ |x| x.instance_of?(String) }.should == ["foo", "bar"]
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#include?" do
82
+ describe "maintain the original functionality" do
83
+ it "should still return true if the item is included in the array" do
84
+ [1,2,3].should include(2)
85
+ end
86
+
87
+ it "should still return false if the item is not included in the array" do
88
+ [1,2,3].should_not include(4)
89
+ end
90
+ end
91
+
92
+ describe "using a block" do
93
+ it "should return true if a matching item if found in the array" do
94
+ ["some", "word", "foobar"].include?{ |x| x.length == 6 }.should be_true
95
+ end
96
+
97
+ it "should return false if a macthing item is not found in the array" do
98
+ ["some", "word", "foobar"].include?{ |x| x.length == 2 }.should be_false
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Loading gonzo array extensions' do
4
+
5
+ before do
6
+ require File.join(ROOT, 'lib', 'gonzo_array_extensions')
7
+ end
8
+
9
+ it "should include the Gonzo::ArayExtensions module in the core array class" do
10
+ Array.should include_module(Gonzo::ArrayExtensions)
11
+ end
12
+
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ ROOT = File.join(File.dirname(__FILE__), '..')
5
+
6
+ require File.join(ROOT, 'lib', 'gonzo_array_extensions', 'array.rb')
7
+
8
+ require File.dirname(__FILE__) + "/custom_matchers"
9
+ Spec::Runner.configure do |config|
10
+ config.include(CustomMatchers)
11
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ["-c"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gonzo_array_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Oles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-21 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Extends standard library Array with some common gonzo needs
17
+ email: robertoles@me.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/gonzo_array_extensions.rb
25
+ - lib/gonzo_array_extensions/array.rb
26
+ - tasks/rspec.rake
27
+ files:
28
+ - Manifest
29
+ - README.rdoc
30
+ - Rakefile
31
+ - lib/gonzo_array_extensions.rb
32
+ - lib/gonzo_array_extensions/array.rb
33
+ - spec/custom_matchers.rb
34
+ - spec/gonzo_array_extensions/array_spec.rb
35
+ - spec/gonzo_array_extensions_spec.rb
36
+ - spec/spec_helper.rb
37
+ - tasks/rspec.rake
38
+ - gonzo_array_extensions.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/robertoles/gonzo_array_extensions
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ - --title
48
+ - Gonzo_array_extensions
49
+ - --main
50
+ - README.rdoc
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "1.2"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: gonzo_array_extensions
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Extends standard library Array with some common gonzo needs
72
+ test_files: []
73
+