gen_token 0.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.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/gen_token.gemspec +22 -0
- data/lib/gen_token/version.rb +3 -0
- data/lib/gen_token.rb +37 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/gen_token.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/gen_token/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "gen_token"
|
6
|
+
s.version = GenToken::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Graeme Rouse"]
|
9
|
+
s.email = ["graeme@cloudsponge.com"]
|
10
|
+
s.homepage = "http://www.cloudsponge.com"
|
11
|
+
s.summary = "Simply generates a unique token in a database."
|
12
|
+
s.description = "Point GenToken at a database column and it will generate a random and unique key for the field. "
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
s.rubyforge_project = "gen_token"
|
16
|
+
|
17
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
21
|
+
s.require_path = 'lib'
|
22
|
+
end
|
data/lib/gen_token.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module GenToken
|
2
|
+
|
3
|
+
# Usage:
|
4
|
+
# include GenToken
|
5
|
+
# gen_token :key, :type => :hex, :length => AppConfig.token.token_length
|
6
|
+
# gen_token :password, :type => :hex, :length => AppConfig.token.token_length
|
7
|
+
|
8
|
+
module GenTokenClassMethods
|
9
|
+
def gen_token(key, options = {})
|
10
|
+
token_def = {:name => key, :type => :hex, :length => 16}.merge(options)
|
11
|
+
write_inheritable_array(:token_fields, [token_def])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ActiveRecordInstanceMethods
|
16
|
+
def generate_tokens
|
17
|
+
self.class.read_inheritable_attribute(:token_fields).each do |token|
|
18
|
+
name, type, length = token[:name], token[:type], token[:length]
|
19
|
+
self[token[:name]] = ActiveSupport::SecureRandom.send(:"#{token[:type]}", token[:length])
|
20
|
+
while !self.class.send(:"find_by_#{token[:name]}", self[token[:name]]).nil?
|
21
|
+
self[token[:name]] = ActiveSupport::SecureRandom.send(:"#{token[:type]}", token[:length])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.included(klass)
|
28
|
+
klass.extend GenTokenClassMethods
|
29
|
+
if Object.const_defined?(:ActiveRecord)
|
30
|
+
if klass < ActiveRecord::Base
|
31
|
+
klass.send :include, ActiveRecordInstanceMethods
|
32
|
+
klass.before_validation_on_create :generate_tokens
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gen_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Graeme Rouse
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-04 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: bundler
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: "Point GenToken at a database column and it will generate a random and unique key for the field. "
|
38
|
+
email:
|
39
|
+
- graeme@cloudsponge.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Rakefile
|
50
|
+
- gen_token.gemspec
|
51
|
+
- lib/gen_token.rb
|
52
|
+
- lib/gen_token/version.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://www.cloudsponge.com
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 23
|
77
|
+
segments:
|
78
|
+
- 1
|
79
|
+
- 3
|
80
|
+
- 6
|
81
|
+
version: 1.3.6
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project: gen_token
|
85
|
+
rubygems_version: 1.3.7
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Simply generates a unique token in a database.
|
89
|
+
test_files: []
|
90
|
+
|