acts_as_keyed 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in acts_as_keyed.gemspec
4
4
  gemspec
5
+
6
+ # Development dependencies
7
+ gem "rake"
8
+ gem "activesupport", "~>3.0"
9
+ gem "sqlite3-ruby"
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Jeremy Hubert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,2 +1,15 @@
1
1
  require 'bundler'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the restful_authentication plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
2
15
  Bundler::GemHelper.install_tasks
@@ -12,10 +12,10 @@ Gem::Specification.new do |s|
12
12
  s.summary = %q{Automatically key an active record model with a unique key}
13
13
  s.description = %q{A simple plugin that automatically generates a key for a model on create. It takes care of protecting the key, automatically generating it and making sure it is unique.}
14
14
 
15
+ s.add_dependency "activerecord", "~> 3.0"
15
16
  s.rubyforge_project = "acts_as_keyed"
16
17
 
17
18
  s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.test_files = `git ls-files -- test/*`.split("\n")
20
20
  s.require_paths = ["lib"]
21
21
  end
@@ -0,0 +1,30 @@
1
+ module ActsAsKeyed
2
+ module ClassMethods
3
+ def acts_as_keyed(options={})
4
+ before_validation :create_key, :on => :create
5
+ class_attribute :options
6
+
7
+ options[:as_param] ||= false
8
+ options[:size] ||= 10
9
+ options[:chars] ||= ('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a - ['l','I','O']
10
+
11
+ self.options = options
12
+
13
+ raise ArgumentError, "#{self.name} is missing key column" if ActiveRecord::Base.connection.table_exists?(self.table_name) && columns_hash['key'].nil?
14
+
15
+ attr_protected :key
16
+
17
+ class << self
18
+ def find(*args)
19
+ if self.options[:as_param] && args.first.is_a?(String)
20
+ find_by_key(args)
21
+ else
22
+ super(*args)
23
+ end
24
+ end
25
+ end
26
+
27
+ include InstanceMethods
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ module ActsAsKeyed
2
+ class NoAvailableKeys < StandardError
3
+ def to_s
4
+ 'Very few unique keys are still available. Please change your acts_as_keyed chars or size settings.'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ module ActsAsKeyed
2
+ module InstanceMethods
3
+
4
+ def to_param
5
+ options[:as_param] ? self.key : self.id.to_s
6
+ end
7
+
8
+ def regenerate_key!
9
+ self.create_key
10
+ self.save
11
+ end
12
+
13
+ protected
14
+
15
+ def create_key
16
+ k = nil
17
+ 100.times do
18
+ k = random_key
19
+ break if self.class.count(:conditions => { :key => k }) == 0
20
+ k = nil
21
+ end
22
+ raise NoAvailableKeys if k.nil?
23
+ self.key = k
24
+ end
25
+
26
+ def random_key
27
+ code_array=[]
28
+ 1.upto(options[:size]) { code_array << options[:chars][rand(options[:chars].length)] }
29
+ code_array.join('')
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module ActsAsKeyed
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/acts_as_keyed.rb CHANGED
@@ -1,66 +1,12 @@
1
+ require 'active_record'
2
+ require 'acts_as_keyed/errors'
3
+ require 'acts_as_keyed/class_methods'
4
+ require 'acts_as_keyed/instance_methods'
5
+
1
6
  module ActsAsKeyed
2
7
  def self.included(base)
3
8
  base.extend ClassMethods
4
9
  end
5
-
6
- module ClassMethods
7
- def acts_as_keyed(options={})
8
- class_inheritable_accessor :options
9
- options[:size] ||= 10
10
- options[:chars] ||= ('a'..'z').to_a + ('A'..'Z').to_a + (1..9).to_a - ['l','I','O']
11
- self.options = options
12
-
13
- raise ArgumentError, "#{self.name} is missing key column" if columns_hash['key'].nil?
14
-
15
- if Rails.version < '3'
16
- before_validation_on_create :create_key
17
- else
18
- before_validation :create_key, :on => :create
19
- end
20
-
21
- attr_protected :key
22
-
23
- class << self
24
- def find(*args)
25
- if self.options[:as_param] && args.first.is_a?(String)
26
- find_by_key(args)
27
- else
28
- super(*args)
29
- end
30
- end
31
- end
32
-
33
- include InstanceMethods
34
- end
35
- end
36
-
37
- module InstanceMethods
38
-
39
- def to_param
40
- options[:as_param] ? self.key : self.id.to_s
41
- end
42
-
43
- def regenerate_key!
44
- self.create_key
45
- self.save
46
- end
47
-
48
- protected
49
-
50
- def create_key
51
- k = random_key
52
- while(self.class.count(:conditions => { :key => k }) > 0)
53
- k = random_key
54
- end
55
- self.key = k
56
- end
57
-
58
- def random_key
59
- code_array=[]
60
- 1.upto(options[:size]) { code_array << options[:chars][rand(options[:chars].length)] }
61
- code_array.join('')
62
- end
63
- end
64
10
  end
65
11
 
66
12
  ActiveRecord::Base.send :include, ActsAsKeyed
@@ -0,0 +1,83 @@
1
+ require 'test/test_helper'
2
+
3
+ class ActsAsKeyedBaseTest < ActiveSupport::TestCase
4
+ def setup
5
+ end
6
+
7
+ def teardown
8
+ end
9
+ end
10
+
11
+ class ActsAsKeyedTest < ActsAsKeyedBaseTest
12
+ test "should generate key on create" do
13
+ owk_setup
14
+
15
+ o = ObjectWithKey.create()
16
+ assert_not_nil o.key
17
+ end
18
+
19
+ test "should generate key based on custom characters" do
20
+ owk_setup(:chars => ['a'])
21
+
22
+ o = ObjectWithKey.create
23
+ assert_equal 'aaaaaaaaaa', o.key
24
+ end
25
+
26
+ test "should generate key based on size parameter" do
27
+ owk_setup(:size => 2)
28
+
29
+ o = ObjectWithKey.create
30
+ assert_equal 2, o.key.length
31
+ end
32
+
33
+ test "should treat key as a protected attribute" do
34
+ owk_setup
35
+
36
+ o = ObjectWithKey.create
37
+ o.update_attributes(:key => 'hello')
38
+ assert_not_equal 'hello', o.key
39
+ end
40
+
41
+ test "should require a unique key" do
42
+ owk_setup(:chars => (0...10).to_a, :size => 1)
43
+
44
+ sum = 0
45
+ 10.times do
46
+ o = ObjectWithKey.create
47
+ sum += o.key.to_i
48
+ end
49
+
50
+ assert_equal 45, sum
51
+ end
52
+
53
+ test "should raise an error if no unique keys can be found easily" do
54
+ owk_setup(:chars => ['a'], :size => 1)
55
+
56
+ error = false
57
+ begin
58
+ 2.times do
59
+ o = ObjectWithKey.create
60
+ end
61
+ rescue ActsAsKeyed::NoAvailableKeys
62
+ error = true
63
+ end
64
+
65
+ assert error
66
+ end
67
+
68
+ test "should fail if object doesn't have key column" do
69
+ output = nil
70
+ begin
71
+ ObjectWithoutKey.acts_as_keyed
72
+ rescue ArgumentError => e
73
+ output = e.message
74
+ end
75
+ assert_equal "ObjectWithoutKey is missing key column", output
76
+ end
77
+
78
+ private
79
+
80
+ def owk_setup(options = {})
81
+ ObjectWithKey.acts_as_keyed(options)
82
+ end
83
+ end
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_record'
5
+ require 'active_model'
6
+
7
+ $:.unshift "#{File.dirname(__FILE__)}/../"
8
+ $:.unshift "#{File.dirname(__FILE__)}/../lib/"
9
+
10
+ require 'acts_as_keyed'
11
+
12
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
13
+
14
+ ActiveRecord::Schema.define(:version => 1) do
15
+ create_table :objects_with_key_column do |t|
16
+ t.string :key
17
+ end
18
+
19
+ create_table :objects_without_key_column do |t|
20
+ t.string :name
21
+ end
22
+ end
23
+
24
+ def teardown_db
25
+ ActiveRecord::Base.connection.tables.each do |table|
26
+ ActiveRecord::Base.connection.drop_table(table)
27
+ end
28
+ end
29
+
30
+ class ObjectWithKey < ActiveRecord::Base
31
+ self.table_name = 'objects_with_key_column'
32
+ end
33
+
34
+ class ObjectWithoutKey < ActiveRecord::Base
35
+ self.table_name = 'objects_without_key_column'
36
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_keyed
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 8
10
- version: 0.0.8
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Hubert
@@ -15,9 +15,23 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-22 00:00:00 Z
19
- dependencies: []
20
-
18
+ date: 2012-02-04 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
33
+ type: :runtime
34
+ version_requirements: *id001
21
35
  description: A simple plugin that automatically generates a key for a model on create. It takes care of protecting the key, automatically generating it and making sure it is unique.
22
36
  email:
23
37
  - jhubert@gmail.com
@@ -28,13 +42,20 @@ extensions: []
28
42
  extra_rdoc_files: []
29
43
 
30
44
  files:
45
+ - .gemtest
31
46
  - .gitignore
32
47
  - Gemfile
48
+ - LICENSE
33
49
  - README.md
34
50
  - Rakefile
35
51
  - acts_as_keyed.gemspec
36
52
  - lib/acts_as_keyed.rb
53
+ - lib/acts_as_keyed/class_methods.rb
54
+ - lib/acts_as_keyed/errors.rb
55
+ - lib/acts_as_keyed/instance_methods.rb
37
56
  - lib/acts_as_keyed/version.rb
57
+ - test/acts_as_keyed_test.rb
58
+ - test/test_helper.rb
38
59
  homepage: http://github.com/jhubert/acts-as-keyed
39
60
  licenses: []
40
61
 
@@ -64,9 +85,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
85
  requirements: []
65
86
 
66
87
  rubyforge_project: acts_as_keyed
67
- rubygems_version: 1.8.6
88
+ rubygems_version: 1.8.15
68
89
  signing_key:
69
90
  specification_version: 3
70
91
  summary: Automatically key an active record model with a unique key
71
- test_files: []
72
-
92
+ test_files:
93
+ - test/acts_as_keyed_test.rb
94
+ - test/test_helper.rb