ac_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.
Files changed (6) hide show
  1. data/.gitignore +3 -0
  2. data/README.rdoc +5 -0
  3. data/Rakefile +22 -0
  4. data/VERSION +1 -0
  5. data/lib/uniquify.rb +35 -0
  6. metadata +59 -0
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ doc
3
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = Uniquify
2
+
3
+ Rails gem for generating a unique token in an Active Record model.
4
+
5
+ ....
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "ac_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 = "Generate a unique token with Active Record"
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
+
21
+
22
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
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
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ac_uniquify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eifion Bedford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-12 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generate a unique token with Active Record
17
+ email: eifion@asciicasts.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - .gitignore
26
+ - README.rdoc
27
+ - Rakefile
28
+ - VERSION
29
+ - lib/uniquify.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/eifion/uniquify
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Generate a unique token with Active Record
58
+ test_files: []
59
+