object-in-enumerable 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 +24 -0
 - data/lib/object-in-enumerable.rb +10 -0
 - data/spec/object-in-enumerable_spec.rb +64 -0
 - metadata +69 -0
 
    
        data/README
    ADDED
    
    | 
         @@ -0,0 +1,24 @@ 
     | 
|
| 
      
 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 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Examples:
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
              "abc".in?("abcdef")         # true
         
     | 
| 
      
 6 
     | 
    
         
            +
              "abc".not_in?("abcdef")     # false
         
     | 
| 
      
 7 
     | 
    
         
            +
              "ABC".in?("abcdef")         # false
         
     | 
| 
      
 8 
     | 
    
         
            +
              "ABC".not_in?("abcdef")     # true
         
     | 
| 
      
 9 
     | 
    
         
            +
              "a".in(%w(a b c d e))       # true
         
     | 
| 
      
 10 
     | 
    
         
            +
              "a".not_in(%w(a b c d e))   # false
         
     | 
| 
      
 11 
     | 
    
         
            +
              1.in?(0..10)                # true
         
     | 
| 
      
 12 
     | 
    
         
            +
              1.not_in?(0..10)            # false
         
     | 
| 
      
 13 
     | 
    
         
            +
              1.in?(10..20)               # false
         
     | 
| 
      
 14 
     | 
    
         
            +
              1.not_in?(10..20)           # true
         
     | 
| 
      
 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"
         
     | 
| 
         @@ -0,0 +1,10 @@ 
     | 
|
| 
      
 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 
     | 
    
         
            +
              
         
     | 
| 
      
 7 
     | 
    
         
            +
              def not_in?(enum)
         
     | 
| 
      
 8 
     | 
    
         
            +
                enum.respond_to?(:include?) ? !enum.include?(self) : nil
         
     | 
| 
      
 9 
     | 
    
         
            +
              end
         
     | 
| 
      
 10 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,64 @@ 
     | 
|
| 
      
 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 
     | 
    
         
            +
                  
         
     | 
| 
      
 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
         
     | 
| 
      
 22 
     | 
    
         
            +
                end
         
     | 
| 
      
 23 
     | 
    
         
            +
                
         
     | 
| 
      
 24 
     | 
    
         
            +
                it "should know when it is not in the given array" do
         
     | 
| 
      
 25 
     | 
    
         
            +
                  2.in?(@enum).should be_false
         
     | 
| 
      
 26 
     | 
    
         
            +
                  "C".in?(@enum).should be_false
         
     | 
| 
      
 27 
     | 
    
         
            +
                  Date.new(2000, 01, 01).in?(@enum).should be_false
         
     | 
| 
      
 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
         
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                it "should know when it is in a non-enumerable object" do
         
     | 
| 
      
 37 
     | 
    
         
            +
                  "a".in?("abc").should be_true
         
     | 
| 
      
 38 
     | 
    
         
            +
                  "ABC".in?("ABCDEFG").should be_true
         
     | 
| 
      
 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
         
     | 
| 
      
 44 
     | 
    
         
            +
                end
         
     | 
| 
      
 45 
     | 
    
         
            +
                
         
     | 
| 
      
 46 
     | 
    
         
            +
                it "should know when it is not in a non-enumerable object" do
         
     | 
| 
      
 47 
     | 
    
         
            +
                  "d".in?("abc").should be_false
         
     | 
| 
      
 48 
     | 
    
         
            +
                  "Z".in?("xyz").should be_false
         
     | 
| 
      
 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
         
     | 
| 
      
 54 
     | 
    
         
            +
                end
         
     | 
| 
      
 55 
     | 
    
         
            +
                
         
     | 
| 
      
 56 
     | 
    
         
            +
                it "should return nil if the given enumerable doesn't respond to include?" do
         
     | 
| 
      
 57 
     | 
    
         
            +
                  1.in?(1).should be_nil
         
     | 
| 
      
 58 
     | 
    
         
            +
                  "foo".in?(nil).should be_nil
         
     | 
| 
      
 59 
     | 
    
         
            +
                  
         
     | 
| 
      
 60 
     | 
    
         
            +
                  1.not_in?(1).should be_nil
         
     | 
| 
      
 61 
     | 
    
         
            +
                  "foo".not_in?(nil).should be_nil
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,69 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: object-in-enumerable
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              hash: 19
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 6 
     | 
    
         
            +
              segments: 
         
     | 
| 
      
 7 
     | 
    
         
            +
              - 1
         
     | 
| 
      
 8 
     | 
    
         
            +
              - 1
         
     | 
| 
      
 9 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 10 
     | 
    
         
            +
              version: 1.1.0
         
     | 
| 
      
 11 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 12 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 13 
     | 
    
         
            +
            - Patrick Byrne
         
     | 
| 
      
 14 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 15 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 16 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            date: 2010-10-21 00:00:00 -05:00
         
     | 
| 
      
 19 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 20 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            description: 
         
     | 
| 
      
 23 
     | 
    
         
            +
            email: code@patrickbyrne.net
         
     | 
| 
      
 24 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
            extra_rdoc_files: 
         
     | 
| 
      
 29 
     | 
    
         
            +
            - README
         
     | 
| 
      
 30 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 31 
     | 
    
         
            +
            - lib/object-in-enumerable.rb
         
     | 
| 
      
 32 
     | 
    
         
            +
            - README
         
     | 
| 
      
 33 
     | 
    
         
            +
            - spec/object-in-enumerable_spec.rb
         
     | 
| 
      
 34 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 35 
     | 
    
         
            +
            homepage: http://github.com/pbyrne/object-in-enumerable/
         
     | 
| 
      
 36 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 39 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 42 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 43 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 44 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 45 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 46 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 47 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 48 
     | 
    
         
            +
                  hash: 3
         
     | 
| 
      
 49 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 50 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 51 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 52 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 53 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 54 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 55 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 56 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 57 
     | 
    
         
            +
                  hash: 3
         
     | 
| 
      
 58 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 59 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 60 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 61 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 64 
     | 
    
         
            +
            rubygems_version: 1.3.7
         
     | 
| 
      
 65 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 66 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 67 
     | 
    
         
            +
            summary: Gives the more-readable `foo.in?(some_list)` rather than having to revert to the more backward `some_list.include?(foo)`.
         
     | 
| 
      
 68 
     | 
    
         
            +
            test_files: 
         
     | 
| 
      
 69 
     | 
    
         
            +
            - spec/object-in-enumerable_spec.rb
         
     |