wayne_uniquify 0.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/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ doc
3
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,62 @@
1
+ = Uniquify
2
+
3
+ Rails gem for generating a unique token in an Active Record model.
4
+
5
+ This is from Railscasts episode #135 showing how to make a gem.
6
+
7
+ http://railscasts.com/episodes/135-making-a-gem
8
+
9
+
10
+ == Install
11
+
12
+ gem install ryanb-uniquify --source http://gems.github.com
13
+
14
+
15
+ == Usage
16
+
17
+ Let's say you have a Project model with a "token" string column that you
18
+ want to be a unique identifier. Just add this to your model.
19
+
20
+ class Project < ActiveRecord::Base
21
+ uniquify :token
22
+ end
23
+
24
+ This will generate a random and unique string before each book is
25
+ created.
26
+
27
+ You can specify multiple columns ands options like this.
28
+
29
+ uniquify :token, :salt, :length => 12, :chars => 0..9
30
+
31
+ You can also pass a block to generate your own characters.
32
+
33
+ uniquify :token do
34
+ rand(99999)
35
+ end
36
+
37
+ If the generated value already exists, the block will be run again
38
+ until the value is unique.
39
+
40
+
41
+ == License
42
+
43
+ Copyright (c) 2008 Ryan Bates
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ "Software"), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
59
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
60
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
61
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
62
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "wayne_uniquify"
8
+ gemspec.summary = "Generate a unique token with Active Record"
9
+ gemspec.description = "Generate a unique token with Active Record"
10
+ gemspec.email = "eifion@asciicasts.com"
11
+ gemspec.homepage = "http://github.com/eifion/uniquify"
12
+ gemspec.description = "trial run at gemcutter"
13
+ gemspec.authors = ["Eifion Bedford"]
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'uniquify'
data/lib/uniquify.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Uniquify
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ def ensure_unique(name)
7
+ begin
8
+ self[name] = yield
9
+ end while self.class.exists?(name => self[name])
10
+ end
11
+
12
+ module ClassMethods
13
+
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
+ before_create 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
+
30
+ end
31
+ end
32
+
33
+ class ActiveRecord::Base
34
+ include Uniquify
35
+ end
data/uniquify.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{uniquify}
3
+ s.version = "0.1.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Ryan Bates"]
7
+ s.date = %q{2008-11-09}
8
+ s.description = %q{Generate a unique token with Active Record.}
9
+ s.email = %q{ryan@railscasts.com}
10
+ s.extra_rdoc_files = ["lib/uniquify.rb", "README.rdoc"]
11
+ s.files = ["lib/uniquify.rb", "Rakefile", "README.rdoc", "Manifest", "uniquify.gemspec"]
12
+ s.has_rdoc = true
13
+ s.homepage = %q{http://github.com/ryanb/uniquify}
14
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Uniquify", "--main", "README.rdoc"]
15
+ s.require_paths = ["lib"]
16
+ s.rubyforge_project = %q{uniquify}
17
+ s.rubygems_version = %q{1.2.0}
18
+ s.summary = %q{Generate a unique token with Active Record.}
19
+
20
+ if s.respond_to? :specification_version then
21
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
+ s.specification_version = 2
23
+
24
+ if current_version >= 3 then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wayne_uniquify
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Eifion Bedford
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-09 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: trial run at gemcutter
22
+ email: eifion@asciicasts.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - .gitignore
31
+ - README.rdoc
32
+ - Rakefile
33
+ - VERSION
34
+ - init.rb
35
+ - lib/uniquify.rb
36
+ - uniquify.gemspec
37
+ has_rdoc: true
38
+ homepage: http://github.com/eifion/uniquify
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.6
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Generate a unique token with Active Record
67
+ test_files: []
68
+