unique_generator 1.0.0 → 1.0.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.
- checksums.yaml +7 -0
- data/.travis.yml +14 -0
- data/Gemfile +8 -1
- data/LICENSE +19 -0
- data/README.md +73 -0
- data/lib/unique_generator.rb +7 -7
- data/lib/unique_generator/version.rb +1 -1
- metadata +55 -36
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 74335b57c6d7ce26df0a01fd7490337474d3edef
         | 
| 4 | 
            +
              data.tar.gz: 056548ba888c27d79b2ecec9821626dc32f44599
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 500a65c0d1cf28c0f99f9044a91ccb2d78dcd5a3db274a1ad5b197f1f76099eca1495b6f94e1f7c077cb40a5a60c7b66b77fcdfa5a425b59d36fdb70a0e145f1
         | 
| 7 | 
            +
              data.tar.gz: 5e618b03ba2ba83f93941ab53e8a692ed2f58a69b8f901d07718ed74a9d715a91b090645aa4f55e7cd57a1216658f655604311e99699a1379d1409160d424129
         | 
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    CHANGED
    
    | @@ -3,6 +3,13 @@ source "http://rubygems.org" | |
| 3 3 | 
             
            # Specify your gem's dependencies in unique_generator.gemspec
         | 
| 4 4 | 
             
            gemspec
         | 
| 5 5 |  | 
| 6 | 
            +
            # Allow testing multiple versions with Travis.
         | 
| 7 | 
            +
            rails_version = ENV['RAILS_VERSION']
         | 
| 8 | 
            +
            if rails_version && rails_version.length > 0
         | 
| 9 | 
            +
              puts "Testing Rails Version = #{rails_version}"
         | 
| 10 | 
            +
              gem 'activerecord', rails_version
         | 
| 11 | 
            +
            end
         | 
| 12 | 
            +
             | 
| 6 13 | 
             
            gem "sqlite3"
         | 
| 7 14 | 
             
            gem "bson_ext"
         | 
| 8 | 
            -
            gem " | 
| 15 | 
            +
            gem "rake"
         | 
    
        data/LICENSE
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            Copyright (c) 2012 Filter Squad
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining a copy
         | 
| 4 | 
            +
            of this software and associated documentation files (the "Software"), to deal
         | 
| 5 | 
            +
            in the Software without restriction, including without limitation the rights
         | 
| 6 | 
            +
            to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         | 
| 7 | 
            +
            copies of the Software, and to permit persons to whom the Software is
         | 
| 8 | 
            +
            furnished to do so, subject to the following conditions:
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            The above copyright notice and this permission notice shall be included in
         | 
| 11 | 
            +
            all copies or substantial portions of the Software.
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         | 
| 14 | 
            +
            IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         | 
| 15 | 
            +
            FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         | 
| 16 | 
            +
            AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         | 
| 17 | 
            +
            LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         | 
| 18 | 
            +
            OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
         | 
| 19 | 
            +
            THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,73 @@ | |
| 1 | 
            +
            # Unique Generator [](http://travis-ci.org/filtersquad/unique_generator) [](https://gemnasium.com/filtersquad/unique_generator)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            ## Introduction
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Unique Generator is a plugin for ActiveRecord and MongoMapper which makes it simple to generate
         | 
| 6 | 
            +
            unique fields and unique tokens e.g. for invites, api keys and the like.
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            Unique Generator is only available for Rails 3.0+.
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            ## Installation
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            Installing Unique Generator is simple, just add the following to your Gemfile:
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            ```ruby
         | 
| 15 | 
            +
            gem 'unique_generator', '~> 1.0'
         | 
| 16 | 
            +
            ```
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            We'll automatically hook into ActiveRecord and MongoMapper via a railtie, adding three methods.
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            ## Usage
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            Unique Generator defines a very simple method, adding only four methods:
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            * `YourModelClass.generate_random(length = 32)` - Generates a case sensitive field with size characters.
         | 
| 25 | 
            +
            * `YourModelClass.generate_unique(length = 32, &block)` - Taking a block which defines the predicate of a given value,
         | 
| 26 | 
            +
              it will generate a random toke of the specified length until the block returns true.
         | 
| 27 | 
            +
            * `YourModelClass#unique_field?(name)` - Called on an instance, tells you whether that instances value for a given
         | 
| 28 | 
            +
              field is unique in the database.
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            And finally, for most people, the only method that matters:
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            * `YourModelClass#generate_unique_field!(name, length = 32)` - Generates a random token of the specified length, combining
         | 
| 33 | 
            +
               the above methods to ensure it has a unique value.
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            **Note:** Please keep in mind, in your migration your field accepting a random token should ideally be case sensitive.
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            ## Example
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            As a simple example, say we want to generate a unique invite token on a model, to do this, we'd do something like:
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            ```ruby
         | 
| 42 | 
            +
            class Invite < ActiveRecord::Base
         | 
| 43 | 
            +
             | 
| 44 | 
            +
              before_save :generate_invite_token
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def generate_invite_token
         | 
| 47 | 
            +
                generate_unique_field! :token, 32 if token.blank?
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
            end
         | 
| 51 | 
            +
            ```
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            ## Contributors
         | 
| 54 | 
            +
             | 
| 55 | 
            +
            - [Darcy Laycock](https://github.com/Sutto) - Main developer, current maintainer.
         | 
| 56 | 
            +
             | 
| 57 | 
            +
            ## Contributing
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            We encourage all community contributions. Keeping this in mind, please follow these general guidelines when contributing:
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            * Fork the project
         | 
| 62 | 
            +
            * Create a topic branch for what you’re working on (git checkout -b awesome_feature)
         | 
| 63 | 
            +
            * Commit away, push that up (git push your\_remote awesome\_feature)
         | 
| 64 | 
            +
            * Create a new GitHub Issue with the commit, asking for review. Alternatively, send a pull request with details of what you added.
         | 
| 65 | 
            +
            * Once it’s accepted, if you want access to the core repository feel free to ask! Otherwise, you can continue to hack away in your own fork.
         | 
| 66 | 
            +
             | 
| 67 | 
            +
            Other than that, our guidelines very closely match the GemCutter guidelines [here](http://wiki.github.com/qrush/gemcutter/contribution-guidelines).
         | 
| 68 | 
            +
             | 
| 69 | 
            +
            (Thanks to [GemCutter](http://wiki.github.com/qrush/gemcutter/) for the contribution guide)
         | 
| 70 | 
            +
             | 
| 71 | 
            +
            ## License
         | 
| 72 | 
            +
             | 
| 73 | 
            +
            RocketPants is released under the MIT License (see the [license file](https://github.com/filtersquad/unique_generator/blob/master/LICENSE)) and is copyright Filter Squad, 2012.
         | 
    
        data/lib/unique_generator.rb
    CHANGED
    
    | @@ -36,7 +36,7 @@ module UniqueGenerator | |
| 36 36 | 
             
                    scope = self.class.where(field_name => send(field_name))
         | 
| 37 37 | 
             
                    # When we have an id, check for those with a different id.
         | 
| 38 38 | 
             
                    if id.present?
         | 
| 39 | 
            -
                      conditions = self.class. | 
| 39 | 
            +
                      conditions = self.class.arel_table[:id].eq(id).not
         | 
| 40 40 | 
             
                      scope = scope.where(conditions)
         | 
| 41 41 | 
             
                    end
         | 
| 42 42 | 
             
                    scope.empty?
         | 
| @@ -44,7 +44,7 @@ module UniqueGenerator | |
| 44 44 | 
             
                end
         | 
| 45 45 |  | 
| 46 46 | 
             
              end
         | 
| 47 | 
            -
             | 
| 47 | 
            +
             | 
| 48 48 | 
             
              module ClassMethods
         | 
| 49 49 |  | 
| 50 50 | 
             
                # Generate a unique field
         | 
| @@ -58,10 +58,10 @@ module UniqueGenerator | |
| 58 58 | 
             
                def generate_random(length = 32)
         | 
| 59 59 | 
             
                  (1..length).inject("") { |token, _| token << CHOICES.sample }
         | 
| 60 60 | 
             
                end
         | 
| 61 | 
            -
             | 
| 61 | 
            +
             | 
| 62 62 | 
             
              end
         | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 63 | 
            +
             | 
| 64 | 
            +
             | 
| 65 65 | 
             
              # Generate unique field.
         | 
| 66 66 | 
             
              def generate_unique_field!(name, size = 32)
         | 
| 67 67 | 
             
                return if send(name).present?
         | 
| @@ -70,7 +70,7 @@ module UniqueGenerator | |
| 70 70 | 
             
                  unique_field? name
         | 
| 71 71 | 
             
                end
         | 
| 72 72 | 
             
              end
         | 
| 73 | 
            -
             | 
| 73 | 
            +
             | 
| 74 74 |  | 
| 75 75 | 
             
              setup!
         | 
| 76 76 |  | 
| @@ -81,5 +81,5 @@ module UniqueGenerator | |
| 81 81 | 
             
                  end
         | 
| 82 82 | 
             
                end
         | 
| 83 83 | 
             
              end
         | 
| 84 | 
            -
             | 
| 84 | 
            +
             | 
| 85 85 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,82 +1,99 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: unique_generator
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.0. | 
| 5 | 
            -
              prerelease: 
         | 
| 4 | 
            +
              version: 1.0.1
         | 
| 6 5 | 
             
            platform: ruby
         | 
| 7 6 | 
             
            authors:
         | 
| 8 7 | 
             
            - Darcy Laycock
         | 
| 9 8 | 
             
            autorequire: 
         | 
| 10 9 | 
             
            bindir: bin
         | 
| 11 10 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date:  | 
| 11 | 
            +
            date: 2014-02-02 00:00:00.000000000 Z
         | 
| 13 12 | 
             
            dependencies:
         | 
| 14 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 14 | 
             
              name: activesupport
         | 
| 16 | 
            -
              requirement:  | 
| 17 | 
            -
                none: false
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 18 16 | 
             
                requirements:
         | 
| 19 | 
            -
                - -  | 
| 17 | 
            +
                - - ">="
         | 
| 20 18 | 
             
                  - !ruby/object:Gem::Version
         | 
| 21 19 | 
             
                    version: '0'
         | 
| 22 20 | 
             
              type: :runtime
         | 
| 23 21 | 
             
              prerelease: false
         | 
| 24 | 
            -
              version_requirements:  | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - ">="
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '0'
         | 
| 25 27 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 26 28 | 
             
              name: activerecord
         | 
| 27 | 
            -
              requirement:  | 
| 28 | 
            -
                none: false
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 29 30 | 
             
                requirements:
         | 
| 30 | 
            -
                - -  | 
| 31 | 
            +
                - - ">="
         | 
| 31 32 | 
             
                  - !ruby/object:Gem::Version
         | 
| 32 33 | 
             
                    version: '0'
         | 
| 33 34 | 
             
              type: :development
         | 
| 34 35 | 
             
              prerelease: false
         | 
| 35 | 
            -
              version_requirements:  | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 36 41 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 37 42 | 
             
              name: mongo_mapper
         | 
| 38 | 
            -
              requirement:  | 
| 39 | 
            -
                none: false
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 40 44 | 
             
                requirements:
         | 
| 41 | 
            -
                - -  | 
| 45 | 
            +
                - - ">="
         | 
| 42 46 | 
             
                  - !ruby/object:Gem::Version
         | 
| 43 47 | 
             
                    version: '0'
         | 
| 44 48 | 
             
              type: :development
         | 
| 45 49 | 
             
              prerelease: false
         | 
| 46 | 
            -
              version_requirements:  | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - ">="
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '0'
         | 
| 47 55 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 48 56 | 
             
              name: reversible_data
         | 
| 49 | 
            -
              requirement:  | 
| 50 | 
            -
                none: false
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 51 58 | 
             
                requirements:
         | 
| 52 | 
            -
                - -  | 
| 59 | 
            +
                - - ">="
         | 
| 53 60 | 
             
                  - !ruby/object:Gem::Version
         | 
| 54 61 | 
             
                    version: '0'
         | 
| 55 62 | 
             
              type: :development
         | 
| 56 63 | 
             
              prerelease: false
         | 
| 57 | 
            -
              version_requirements:  | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - ">="
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '0'
         | 
| 58 69 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 59 70 | 
             
              name: rr
         | 
| 60 | 
            -
              requirement:  | 
| 61 | 
            -
                none: false
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 62 72 | 
             
                requirements:
         | 
| 63 | 
            -
                - -  | 
| 73 | 
            +
                - - ">="
         | 
| 64 74 | 
             
                  - !ruby/object:Gem::Version
         | 
| 65 75 | 
             
                    version: '0'
         | 
| 66 76 | 
             
              type: :development
         | 
| 67 77 | 
             
              prerelease: false
         | 
| 68 | 
            -
              version_requirements:  | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - ">="
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '0'
         | 
| 69 83 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 70 84 | 
             
              name: rspec
         | 
| 71 | 
            -
              requirement:  | 
| 72 | 
            -
                none: false
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 73 86 | 
             
                requirements:
         | 
| 74 | 
            -
                - -  | 
| 87 | 
            +
                - - ">="
         | 
| 75 88 | 
             
                  - !ruby/object:Gem::Version
         | 
| 76 89 | 
             
                    version: '0'
         | 
| 77 90 | 
             
              type: :development
         | 
| 78 91 | 
             
              prerelease: false
         | 
| 79 | 
            -
              version_requirements:  | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - ">="
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 80 97 | 
             
            description: ActiveRecord, MongoMapper and general tools for generating unique values
         | 
| 81 98 | 
             
              e.g. api keys and tokens.
         | 
| 82 99 | 
             
            email:
         | 
| @@ -85,10 +102,13 @@ executables: [] | |
| 85 102 | 
             
            extensions: []
         | 
| 86 103 | 
             
            extra_rdoc_files: []
         | 
| 87 104 | 
             
            files:
         | 
| 88 | 
            -
            - .gitignore
         | 
| 89 | 
            -
            - .rspec
         | 
| 90 | 
            -
            - .rvmrc
         | 
| 105 | 
            +
            - ".gitignore"
         | 
| 106 | 
            +
            - ".rspec"
         | 
| 107 | 
            +
            - ".rvmrc"
         | 
| 108 | 
            +
            - ".travis.yml"
         | 
| 91 109 | 
             
            - Gemfile
         | 
| 110 | 
            +
            - LICENSE
         | 
| 111 | 
            +
            - README.md
         | 
| 92 112 | 
             
            - Rakefile
         | 
| 93 113 | 
             
            - lib/unique_generator.rb
         | 
| 94 114 | 
             
            - lib/unique_generator/version.rb
         | 
| @@ -101,27 +121,26 @@ files: | |
| 101 121 | 
             
            - unique_generator.gemspec
         | 
| 102 122 | 
             
            homepage: http://github.com/filtersquad/unique_generator
         | 
| 103 123 | 
             
            licenses: []
         | 
| 124 | 
            +
            metadata: {}
         | 
| 104 125 | 
             
            post_install_message: 
         | 
| 105 126 | 
             
            rdoc_options: []
         | 
| 106 127 | 
             
            require_paths:
         | 
| 107 128 | 
             
            - lib
         | 
| 108 129 | 
             
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 109 | 
            -
              none: false
         | 
| 110 130 | 
             
              requirements:
         | 
| 111 | 
            -
              - -  | 
| 131 | 
            +
              - - ">="
         | 
| 112 132 | 
             
                - !ruby/object:Gem::Version
         | 
| 113 133 | 
             
                  version: '0'
         | 
| 114 134 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 115 | 
            -
              none: false
         | 
| 116 135 | 
             
              requirements:
         | 
| 117 | 
            -
              - -  | 
| 136 | 
            +
              - - ">="
         | 
| 118 137 | 
             
                - !ruby/object:Gem::Version
         | 
| 119 138 | 
             
                  version: '0'
         | 
| 120 139 | 
             
            requirements: []
         | 
| 121 140 | 
             
            rubyforge_project: 
         | 
| 122 | 
            -
            rubygems_version:  | 
| 141 | 
            +
            rubygems_version: 2.2.0
         | 
| 123 142 | 
             
            signing_key: 
         | 
| 124 | 
            -
            specification_version:  | 
| 143 | 
            +
            specification_version: 4
         | 
| 125 144 | 
             
            summary: Makes generating unique fields in MongoMapper and ActiveRecord easy.
         | 
| 126 145 | 
             
            test_files:
         | 
| 127 146 | 
             
            - spec/spec_helper.rb
         |