has_token 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg
3
+ rdoc
4
+ test/*.db
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Steve Richert
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.
data/README.rdoc ADDED
@@ -0,0 +1,13 @@
1
+ = has_token
2
+
3
+ == Installation
4
+
5
+ script/plugin install git://github.com/laserlemon/has_token.git
6
+
7
+ == Example
8
+
9
+ Coming soon...
10
+
11
+ == Tips
12
+
13
+ Coming soon...
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |g|
9
+ g.name = 'has_token'
10
+ g.summary = %(Generate unique tokens on your ActiveRecord models)
11
+ g.description = %(Generate unique tokens on your ActiveRecord models)
12
+ g.email = 'steve@laserlemon.com'
13
+ g.homepage = 'http://github.com/laserlemon/has_token'
14
+ g.authors = %w(laserlemon)
15
+ g.rubyforge_project = 'laser-lemon'
16
+ end
17
+ Jeweler::RubyforgeTasks.new do |r|
18
+ r.doc_task = 'rdoc'
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
23
+ end
24
+
25
+ Rake::TestTask.new do |t|
26
+ t.libs = %w(test)
27
+ t.pattern = 'test/**/*_test.rb'
28
+ end
29
+
30
+ task :default => :test
31
+
32
+ Rake::RDocTask.new do |r|
33
+ version = File.exist?('VERSION') ? File.read('VERSION') : nil
34
+ r.rdoc_dir = 'rdoc'
35
+ r.title = ['has_token', version].compact.join(' ')
36
+ r.rdoc_files.include('README*')
37
+ r.rdoc_files.include('lib/**/*.rb')
38
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.4
@@ -0,0 +1,11 @@
1
+ class HasTokenMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate'
5
+ end
6
+ end
7
+
8
+ def file_name
9
+ 'create_tokens'
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ class CreateTokens < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :tokens do |t|
4
+ t.belongs_to :parent, :polymorphic => true
5
+ t.string :value
6
+ end
7
+
8
+ change_table :tokens do |t|
9
+ t.index [:parent_id, :parent_type]
10
+ t.index :value, :unique => true
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :tokens
16
+ end
17
+ end
data/has_token.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{has_token}
8
+ s.version = "0.3.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["laserlemon"]
12
+ s.date = %q{2009-10-07}
13
+ s.description = %q{Generate unique tokens on your ActiveRecord models}
14
+ s.email = %q{steve@laserlemon.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "generators/has_token_migration/has_token_migration_generator.rb",
26
+ "generators/has_token_migration/templates/migration.rb",
27
+ "has_token.gemspec",
28
+ "init.rb",
29
+ "lib/has_token.rb",
30
+ "lib/token.rb",
31
+ "test/has_token_test.rb",
32
+ "test/test_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/laserlemon/has_token}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubyforge_project = %q{laser-lemon}
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{Generate unique tokens on your ActiveRecord models}
40
+ s.test_files = [
41
+ "test/has_token_test.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'has_token'
data/lib/has_token.rb ADDED
@@ -0,0 +1,56 @@
1
+ require 'token'
2
+
3
+ module LaserLemon
4
+ module HasToken
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def has_token(*args)
11
+ options = args.extract_options!.symbolize_keys
12
+ options[:column] = (args.first || :token)
13
+ options.reverse_merge!(
14
+ :length => 6,
15
+ :characters => [('a'..'z'), ('A'..'Z'), ('0'..'9')].map(&:to_a).sum,
16
+ :constructor => proc(&:has_token_value),
17
+ :to_param => false,
18
+ :readonly => true
19
+ )
20
+
21
+ cattr_accessor :has_token_options
22
+ return(false) if self.has_token_options.present?
23
+ self.has_token_options = options
24
+
25
+ attr_readonly(options[:column]) if options[:readonly]
26
+ define_method(:to_param){ read_attribute(options[:column]) } if options[:to_param]
27
+
28
+ has_one :global_token, :class_name => 'Token', :as => :parent, :autosave => true, :dependent => :nullify
29
+
30
+ include InstanceMethods
31
+ before_create :set_token
32
+ end
33
+ end
34
+
35
+ module InstanceMethods
36
+ def has_token_options
37
+ self.class.has_token_options
38
+ end
39
+
40
+ def has_token_value
41
+ Array.new(has_token_options[:length]){ has_token_options[:characters].rand }.join
42
+ end
43
+
44
+ def set_token
45
+ begin
46
+ token_value = has_token_options[:constructor].call(self)
47
+ end while Token.exists?(:value => token_value)
48
+
49
+ build_global_token(:value => token_value)
50
+ write_attribute(has_token_options[:column], token_value)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ ActiveRecord::Base.send(:include, LaserLemon::HasToken)
data/lib/token.rb ADDED
@@ -0,0 +1,14 @@
1
+ class Token < ActiveRecord::Base
2
+ belongs_to :parent, :polymorphic => true
3
+
4
+ named_scope :for, lambda{|k| {:conditions => {:parent_type => k.to_s.classify}} }
5
+
6
+ def self.get(value)
7
+ find_by_value(value).try(:parent)
8
+ end
9
+
10
+ def self.get!(value)
11
+ token = find_by_value!(value)
12
+ token.parent_type.constantize.find(token.parent_id)
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class HasTokenTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.4
5
+ platform: ruby
6
+ authors:
7
+ - laserlemon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-07 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generate unique tokens on your ActiveRecord models
17
+ email: steve@laserlemon.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION
31
+ - generators/has_token_migration/has_token_migration_generator.rb
32
+ - generators/has_token_migration/templates/migration.rb
33
+ - has_token.gemspec
34
+ - init.rb
35
+ - lib/has_token.rb
36
+ - lib/token.rb
37
+ - test/has_token_test.rb
38
+ - test/test_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/laserlemon/has_token
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: laser-lemon
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Generate unique tokens on your ActiveRecord models
67
+ test_files:
68
+ - test/has_token_test.rb
69
+ - test/test_helper.rb