acts_as_keyed 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_keyed.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "acts_as_keyed/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "acts_as_keyed"
7
+ s.version = ActsAsKeyed::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jeremy Hubert"]
10
+ s.email = ["jhubert@gmail.com"]
11
+ s.homepage = "http://github.com/jhubert/acts_as_keyed"
12
+ s.summary = %q{Automatically key an active record model}
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
+
15
+ s.rubyforge_project = "acts_as_keyed"
16
+
17
+ 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) }
20
+ s.require_paths = ["lib"]
21
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,3 @@
1
+ module ActsAsKeyed
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,53 @@
1
+ module ActsAsKeyed
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+ module ClassMethods
6
+ def acts_as_keyed(options={})
7
+ class_inheritable_accessor :options
8
+ options[:size] ||= 10
9
+ options[:chars] ||= ('a'..'z').to_a - ['a','e','i','o','u'] + (1..9).to_a
10
+ self.options = options
11
+
12
+ raise ArgumentError, "#{self.name} is missing key column" if columns_hash['key'].nil?
13
+
14
+ before_validation_on_create :create_key
15
+
16
+ attr_protected :key
17
+
18
+ class << self
19
+ def find(*args)
20
+ if self.options[:as_param] && args.first.is_a?(String)
21
+ find_by_key(args)
22
+ else
23
+ super(*args)
24
+ end
25
+ end
26
+ end
27
+
28
+ include InstanceMethods
29
+ end
30
+ end
31
+ module InstanceMethods
32
+
33
+ def to_param
34
+ options[:as_param] ? self.key : self.id.to_s
35
+ end
36
+
37
+ protected
38
+
39
+ def create_key
40
+ k = random_key
41
+ while(self.class.count(:conditions => { :key => k }) > 0)
42
+ k = random_key
43
+ end
44
+ self.key = k
45
+ end
46
+
47
+ def random_key
48
+ code_array=[]
49
+ 1.upto(options[:size]) { code_array << options[:chars][rand(options[:chars].length)] }
50
+ code_array.to_s
51
+ end
52
+ end
53
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'acts_as_keyed'
2
+ ActiveRecord::Base.send(:include, Hubert::Acts::Keyed)
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_keyed
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jeremy Hubert
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-05 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ 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.
23
+ email:
24
+ - jhubert@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - acts_as_keyed.gemspec
36
+ - init.rb
37
+ - lib/acts_as_keyed.rb
38
+ - lib/acts_as_keyed/version.rb
39
+ - rails/init.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/jhubert/acts_as_keyed
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project: acts_as_keyed
70
+ rubygems_version: 1.4.2
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Automatically key an active record model
74
+ test_files: []
75
+