mongoid_uniquify 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ coverage
6
+ rdoc
7
+ tmp
8
+ log
9
+ .yardoc
10
+ doc
11
+
12
+ .DS_Store
13
+ *.tmproj
14
+ tmtags
15
+ *~
16
+ \#*
17
+ .\#*
18
+ *.swp
19
+
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Kichiro IKEMOTO
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ = mongoid_uniquify
2
+
3
+ Generate unique token in a Mongoid field. Mongoid version of Ryan Bates uniquify gem.
4
+
5
+ == Installation
6
+
7
+ gem install mongoid_uniquify
8
+
9
+ In your Gemfile
10
+
11
+ gem "mongoid_uniquify"
12
+
13
+ == REQUIREMENTS:
14
+
15
+ * mongoid 2.0.0 or later
16
+
17
+ == USAGE:
18
+
19
+ class Project
20
+ include Mongoid::Document
21
+ include Mongoid::Uniquify
22
+
23
+ uniquify :token, :length => 7, :chars => ('a'..'z').to_a + ('0'..'9').to_a
24
+
25
+ field :token, :type => String
26
+
27
+ # ...
28
+ end
29
+
30
+ If you want to use generated token as key and add unique index, do something like below.
31
+
32
+ class Project
33
+ include Mongoid::Document
34
+ include Mongoid::Uniquify
35
+
36
+ uniquify :token, :length => 7, :chars => ('a'..'z').to_a + ('0'..'9').to_a
37
+
38
+ field :token, :type => String
39
+ key :token
40
+
41
+ index :token, :unique => true
42
+
43
+ # ...
44
+ end
45
+
46
+ == Copyright
47
+
48
+ Copyright (c) 2011 Kichiro IKEMOTO. See LICENSE.txt for
49
+ further details.
50
+
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,31 @@
1
+ module Mongoid #:nodoc:
2
+ module Uniquify
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ def ensure_unique(name)
8
+ begin
9
+ self[name] = yield
10
+ end while self.class.first(:conditions => {name => self[name]})
11
+ end
12
+
13
+ module ClassMethods
14
+ def uniquify(*args, &block)
15
+ options = { :length => 8, :chars => ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a }
16
+ options.merge!(args.pop) if args.last.kind_of? Hash
17
+ args.each do |name|
18
+ after_initialize do |record|
19
+ if block
20
+ record.ensure_unique(name, &block)
21
+ else
22
+ record.ensure_unique(name) do
23
+ Array.new(options[:length]) { options[:chars].to_a[rand(options[:chars].to_a.size)] }.join
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidUniquify
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid_uniquify/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid_uniquify"
7
+ s.version = MongoidUniquify::VERSION
8
+ s.authors = ["Kichiro IKEMOTO"]
9
+ s.email = ["kichiro@yomukaku.net"]
10
+ s.homepage = "http://github.com/kichiro/mongoid_uniquify"
11
+ s.summary = %q{Generate a unique token with Mongoid}
12
+ s.description = %q{Generate unique token in a Mongoid field. Mongoid version of Ryan Bates uniquify gem.}
13
+
14
+ s.rubyforge_project = "mongoid_uniquify"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_uniquify
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Kichiro IKEMOTO
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-19 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Generate unique token in a Mongoid field. Mongoid version of Ryan Bates uniquify gem.
17
+ email:
18
+ - kichiro@yomukaku.net
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - .rspec
29
+ - Gemfile
30
+ - LICENSE
31
+ - README.rdoc
32
+ - Rakefile
33
+ - lib/mongoid_uniquify.rb
34
+ - lib/mongoid_uniquify/version.rb
35
+ - mongoid_uniquify.gemspec
36
+ homepage: http://github.com/kichiro/mongoid_uniquify
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project: mongoid_uniquify
59
+ rubygems_version: 1.8.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Generate a unique token with Mongoid
63
+ test_files: []
64
+