pbyrne-object-in-enumerable 1.0.0 → 1.1.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 +15 -1
- data/lib/object-in-enumerable.rb +4 -0
- data/spec/object-in-enumerable_spec.rb +22 -0
- metadata +1 -1
data/README
CHANGED
@@ -1,10 +1,24 @@
|
|
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)`.
|
1
|
+
Gives the more-readable `foo.in?(some_list)` and `foo.not_in?(some_list)` rather than having to revert to the more backward `some_list.include?(foo)` and `!some_list.include?(foo)`. This leads to more-readable conditions, like: `if foo.is_awesome? and foo.in?(allowed_values)`.
|
2
2
|
|
3
3
|
Examples:
|
4
4
|
|
5
5
|
"abc".in?("abcdef") # true
|
6
|
+
"abc".not_in?("abcdef") # false
|
6
7
|
"ABC".in?("abcdef") # false
|
8
|
+
"ABC".not_in?("abcdef") # true
|
7
9
|
"a".in(%w(a b c d e)) # true
|
10
|
+
"a".not_in(%w(a b c d e)) # false
|
8
11
|
1.in?(0..10) # true
|
12
|
+
1.not_in?(0..10) # false
|
9
13
|
1.in?(10..20) # false
|
14
|
+
1.not_in?(10..20) # true
|
10
15
|
1.in?(10) # nil, as 10 doesn't respond to :include?
|
16
|
+
1.not_in?(10) # nil, as 10 doesn't respond to :include?
|
17
|
+
|
18
|
+
Install:
|
19
|
+
|
20
|
+
# From the command line
|
21
|
+
gem install pbyrne-object-in-enumerable --source=http://gems.github.com
|
22
|
+
|
23
|
+
# In your Rails project's environment.rb
|
24
|
+
config.gem "pbyrne-object-in-enumerable", :source => "http://gems.github.com", :lib => "object-in-enumerable"
|
data/lib/object-in-enumerable.rb
CHANGED
@@ -13,6 +13,12 @@ describe Object do
|
|
13
13
|
Date.today.in?(@enum).should be_true
|
14
14
|
{:foo => "bar"}.in?(@enum).should be_true
|
15
15
|
nil.in?(@enum).should be_true
|
16
|
+
|
17
|
+
1.not_in?(@enum).should be_false
|
18
|
+
"B".not_in?(@enum).should be_false
|
19
|
+
Date.today.not_in?(@enum).should be_false
|
20
|
+
{:foo => "bar"}.not_in?(@enum).should be_false
|
21
|
+
nil.not_in?(@enum).should be_false
|
16
22
|
end
|
17
23
|
|
18
24
|
it "should know when it is not in the given array" do
|
@@ -20,23 +26,39 @@ describe Object do
|
|
20
26
|
"C".in?(@enum).should be_false
|
21
27
|
Date.new(2000, 01, 01).in?(@enum).should be_false
|
22
28
|
{:bing => "bang"}.in?(@enum).should be_false
|
29
|
+
|
30
|
+
2.not_in?(@enum).should be_true
|
31
|
+
"C".not_in?(@enum).should be_true
|
32
|
+
Date.new(2000, 01, 01).not_in?(@enum).should be_true
|
33
|
+
{:bing => "bang"}.not_in?(@enum).should be_true
|
23
34
|
end
|
24
35
|
|
25
36
|
it "should know when it is in a non-enumerable object" do
|
26
37
|
"a".in?("abc").should be_true
|
27
38
|
"ABC".in?("ABCDEFG").should be_true
|
28
39
|
1.in?(1..5).should be_true
|
40
|
+
|
41
|
+
"a".not_in?("abc").should be_false
|
42
|
+
"ABC".not_in?("ABCDEFG").should be_false
|
43
|
+
1.not_in?(1..5).should be_false
|
29
44
|
end
|
30
45
|
|
31
46
|
it "should know when it is not in a non-enumerable object" do
|
32
47
|
"d".in?("abc").should be_false
|
33
48
|
"Z".in?("xyz").should be_false
|
34
49
|
1.in?(5..10).should be_false
|
50
|
+
|
51
|
+
"d".not_in?("abc").should be_true
|
52
|
+
"Z".not_in?("xyz").should be_true
|
53
|
+
1.not_in?(5..10).should be_true
|
35
54
|
end
|
36
55
|
|
37
56
|
it "should return nil if the given enumerable doesn't respond to include?" do
|
38
57
|
1.in?(1).should be_nil
|
39
58
|
"foo".in?(nil).should be_nil
|
59
|
+
|
60
|
+
1.not_in?(1).should be_nil
|
61
|
+
"foo".not_in?(nil).should be_nil
|
40
62
|
end
|
41
63
|
end
|
42
64
|
end
|