pbyrne-object-in-enumerable 1.0.0

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.
data/README ADDED
@@ -0,0 +1,10 @@
1
+ Gives the more-readable `foo.in?(some_list)` rather than having to revert to the more backward `some_list.include?(foo)`. This leads to more-readable conditions, like: `if foo.is_awesome? and foo.in?(allowed_values)`.
2
+
3
+ Examples:
4
+
5
+ "abc".in?("abcdef") # true
6
+ "ABC".in?("abcdef") # false
7
+ "a".in(%w(a b c d e)) # true
8
+ 1.in?(0..10) # true
9
+ 1.in?(10..20) # false
10
+ 1.in?(10) # nil, as 10 doesn't respond to :include?
@@ -0,0 +1,6 @@
1
+ class Object
2
+ # Uses the given enum's include? method to determine whether the object is included in it. If the given method doesn't respond to include?, returns nil.
3
+ def in?(enum)
4
+ enum.respond_to?(:include?) ? enum.include?(self) : nil
5
+ end
6
+ end
@@ -0,0 +1,42 @@
1
+ require File.join(File.dirname(__FILE__), "/spec_helper")
2
+
3
+ describe Object do
4
+ context "knowing whether it is in the given object" do
5
+ before(:each) do
6
+ @enum = [1, "B", Date.today, {:foo => "bar"}, nil]
7
+ @string = "abcdefg"
8
+ end
9
+
10
+ it "should know when it is in the given array" do
11
+ 1.in?(@enum).should be_true
12
+ "B".in?(@enum).should be_true
13
+ Date.today.in?(@enum).should be_true
14
+ {:foo => "bar"}.in?(@enum).should be_true
15
+ nil.in?(@enum).should be_true
16
+ end
17
+
18
+ it "should know when it is not in the given array" do
19
+ 2.in?(@enum).should be_false
20
+ "C".in?(@enum).should be_false
21
+ Date.new(2000, 01, 01).in?(@enum).should be_false
22
+ {:bing => "bang"}.in?(@enum).should be_false
23
+ end
24
+
25
+ it "should know when it is in a non-enumerable object" do
26
+ "a".in?("abc").should be_true
27
+ "ABC".in?("ABCDEFG").should be_true
28
+ 1.in?(1..5).should be_true
29
+ end
30
+
31
+ it "should know when it is not in a non-enumerable object" do
32
+ "d".in?("abc").should be_false
33
+ "Z".in?("xyz").should be_false
34
+ 1.in?(5..10).should be_false
35
+ end
36
+
37
+ it "should return nil if the given enumerable doesn't respond to include?" do
38
+ 1.in?(1).should be_nil
39
+ "foo".in?(nil).should be_nil
40
+ end
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pbyrne-object-in-enumerable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Byrne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: code@patrickbyrne.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/object-in-enumerable.rb
26
+ - README
27
+ has_rdoc: true
28
+ homepage: http://github.com/pbyrne/object-in-enumerable/
29
+ licenses:
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Gives the more-readable `foo.in?(some_list)` rather than having to revert to the more backward `some_list.include?(foo)`.
54
+ test_files:
55
+ - spec/object-in-enumerable_spec.rb