have-code 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +59 -0
- data/VERSION.yml +4 -0
- data/have-code.gemspec +57 -0
- data/lib/have-code/ar_code.rb +39 -0
- data/lib/have-code/dm_code.rb +39 -0
- data/lib/have-code.rb +21 -0
- data/test/active_record_helper.rb +9 -0
- data/test/data_mapper_helper.rb +10 -0
- data/test/have_code_test.rb +44 -0
- data/test/test_helper.rb +19 -0
- metadata +79 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Bryce Kerley
|
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,23 @@
|
|
1
|
+
= have-code
|
2
|
+
|
3
|
+
Add clever little base36 codes for your DataMapper and ActiveRecord objects.
|
4
|
+
|
5
|
+
== Whoa seriously??
|
6
|
+
|
7
|
+
# ActiveRecord
|
8
|
+
initech = Mug.create :name=>'initech', :material=>'porcelain'
|
9
|
+
code = initech.code
|
10
|
+
initech == Mug.find_by_code code
|
11
|
+
|
12
|
+
# DataMapper
|
13
|
+
kohg = Goblet.create :name=>'king of hyrule', :material=>'gold'
|
14
|
+
code = kohg.code
|
15
|
+
kohg == Goblet.find_by_code code
|
16
|
+
|
17
|
+
== Dependencies
|
18
|
+
|
19
|
+
* bkerley-affine (waiting on rubyforge to approve)
|
20
|
+
|
21
|
+
== Copyright
|
22
|
+
|
23
|
+
Copyright (c) 2009 Bryce Kerley. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "have-code"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "bkerley@brycekerley.net"
|
10
|
+
gem.homepage = "http://github.com/bkerley/have-code"
|
11
|
+
gem.authors = ["Bryce Kerley"]
|
12
|
+
|
13
|
+
gem.add_dependency 'bkerley-affine'
|
14
|
+
|
15
|
+
gem.rubyforge_project = 'have-code'
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/*_test.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
if File.exist?('VERSION.yml')
|
48
|
+
config = YAML.load(File.read('VERSION.yml'))
|
49
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
50
|
+
else
|
51
|
+
version = ""
|
52
|
+
end
|
53
|
+
|
54
|
+
rdoc.rdoc_dir = 'rdoc'
|
55
|
+
rdoc.title = "have-code #{version}"
|
56
|
+
rdoc.rdoc_files.include('README*')
|
57
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
58
|
+
end
|
59
|
+
|
data/VERSION.yml
ADDED
data/have-code.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{have-code}
|
5
|
+
s.version = "0.1.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Bryce Kerley"]
|
9
|
+
s.date = %q{2009-05-18}
|
10
|
+
s.email = %q{bkerley@brycekerley.net}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION.yml",
|
22
|
+
"have-code.gemspec",
|
23
|
+
"lib/have-code.rb",
|
24
|
+
"lib/have-code/ar_code.rb",
|
25
|
+
"lib/have-code/dm_code.rb",
|
26
|
+
"test/active_record_helper.rb",
|
27
|
+
"test/data_mapper_helper.rb",
|
28
|
+
"test/have_code_test.rb",
|
29
|
+
"test/test_helper.rb"
|
30
|
+
]
|
31
|
+
s.has_rdoc = true
|
32
|
+
s.homepage = %q{http://github.com/bkerley/have-code}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubyforge_project = %q{have-code}
|
36
|
+
s.rubygems_version = %q{1.3.1}
|
37
|
+
s.summary = %q{TODO}
|
38
|
+
s.test_files = [
|
39
|
+
"test/active_record_helper.rb",
|
40
|
+
"test/data_mapper_helper.rb",
|
41
|
+
"test/have_code_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 = 2
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<bkerley-affine>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<bkerley-affine>, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<bkerley-affine>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module HaveCode
|
2
|
+
module ARCode
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def have_code(mod, a, b)
|
9
|
+
write_inheritable_attribute :have_code_cipher, Affine::Cipher.new(mod, a, b)
|
10
|
+
|
11
|
+
extend OptionalClassMethods
|
12
|
+
include InstanceMethods
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module OptionalClassMethods
|
17
|
+
def find_by_code(code)
|
18
|
+
return nil unless code.is_a? String
|
19
|
+
cipher = read_inheritable_attribute(:have_code_cipher)
|
20
|
+
candidate_id = cipher.decipher code.to_i(36)
|
21
|
+
object = self.find candidate_id
|
22
|
+
return object if object.code == code
|
23
|
+
return nil
|
24
|
+
rescue
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
def have_code_cipher
|
31
|
+
self.class.read_inheritable_attribute :have_code_cipher
|
32
|
+
end
|
33
|
+
|
34
|
+
def code
|
35
|
+
have_code_cipher.encipher(id).to_s(36)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module HaveCode
|
2
|
+
module DMCode
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def have_code(mod, a, b)
|
9
|
+
write_inheritable_attribute :have_code_cipher, Affine::Cipher.new(mod, a, b)
|
10
|
+
|
11
|
+
extend OptionalClassMethods
|
12
|
+
include InstanceMethods
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module OptionalClassMethods
|
17
|
+
def find_by_code(code)
|
18
|
+
return nil unless code.is_a? String
|
19
|
+
cipher = read_inheritable_attribute(:have_code_cipher)
|
20
|
+
candidate_id = cipher.decipher code.to_i(36)
|
21
|
+
object = self.get candidate_id
|
22
|
+
return object if object.code == code
|
23
|
+
return nil
|
24
|
+
rescue
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
def have_code_cipher
|
31
|
+
self.class.read_inheritable_attribute :have_code_cipher
|
32
|
+
end
|
33
|
+
|
34
|
+
def code
|
35
|
+
have_code_cipher.encipher(id).to_s(36)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/have-code.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'affine'
|
2
|
+
require File.join(File.dirname(__FILE__), 'have-code', 'ar_code')
|
3
|
+
require File.join(File.dirname(__FILE__), 'have-code', 'dm_code')
|
4
|
+
module HaveCode
|
5
|
+
def self.enable_activerecord
|
6
|
+
return if ActiveRecord::Base.respond_to? :have_code
|
7
|
+
ActiveRecord::Base.send :include, ARCode
|
8
|
+
end
|
9
|
+
def self.enable_datamapper
|
10
|
+
return if DataMapper::Resource.respond_to? :have_code
|
11
|
+
DataMapper::Resource.send :include, DMCode
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
if defined? ActiveRecord::Base
|
16
|
+
HaveCode.enable_activerecord
|
17
|
+
end
|
18
|
+
|
19
|
+
if defined? DataMapper::Resource
|
20
|
+
HaveCode.enable_datamapper
|
21
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
HaveCode.enable_activerecord
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
4
|
+
ActiveRecord::Base.connection.create_table :mugs do |t|
|
5
|
+
t.column :name, :string
|
6
|
+
t.column :material, :string
|
7
|
+
end
|
8
|
+
class Mug < ActiveRecord::Base
|
9
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HaveCodeTest < Test::Unit::TestCase
|
4
|
+
in_activerecord do
|
5
|
+
should "have a 'have_code' method" do
|
6
|
+
assert Mug.respond_to?(:have_code)
|
7
|
+
end
|
8
|
+
context "when configured with a code" do
|
9
|
+
setup do
|
10
|
+
Mug.have_code(60466169, 12034710206, 81268112)
|
11
|
+
end
|
12
|
+
should "have a 'find_by_code' method" do
|
13
|
+
assert Mug.respond_to?(:find_by_code)
|
14
|
+
end
|
15
|
+
should 'create an object, get its code, and recall it by code' do
|
16
|
+
initech = Mug.create :name=>'initech', :material=>'porcelain'
|
17
|
+
code = initech.code
|
18
|
+
assert code.is_a?(String)
|
19
|
+
lumbergh = Mug.find_by_code code
|
20
|
+
assert_equal initech, lumbergh
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
in_datamapper do
|
25
|
+
should "have a 'have_code' method" do
|
26
|
+
assert Goblet.respond_to?(:have_code)
|
27
|
+
end
|
28
|
+
context 'when configured with a code' do
|
29
|
+
setup do
|
30
|
+
Goblet.have_code(60466169, 12034710206, 81268112)
|
31
|
+
end
|
32
|
+
should "have a 'find_by_code' method" do
|
33
|
+
assert Goblet.respond_to?(:find_by_code)
|
34
|
+
end
|
35
|
+
should 'create an object, get its code, and recall it by code' do
|
36
|
+
kohg = Goblet.create :name=>'king of hyrule', :material=>'gold'
|
37
|
+
code = kohg.code
|
38
|
+
assert code.is_a?(String)
|
39
|
+
zelda = Goblet.find_by_code code
|
40
|
+
assert_equal kohg, zelda
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
require 'have-code'
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
def self.in_activerecord(&block)
|
11
|
+
require 'active_record_helper'
|
12
|
+
context "Mug, an ActiveRecord class,", &block
|
13
|
+
end
|
14
|
+
def self.in_datamapper(&block)
|
15
|
+
require 'data_mapper_helper'
|
16
|
+
context "Goblet, a DataMapper class,", &block
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: have-code
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bryce Kerley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-18 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bkerley-affine
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: bkerley@brycekerley.net
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION.yml
|
41
|
+
- have-code.gemspec
|
42
|
+
- lib/have-code.rb
|
43
|
+
- lib/have-code/ar_code.rb
|
44
|
+
- lib/have-code/dm_code.rb
|
45
|
+
- test/active_record_helper.rb
|
46
|
+
- test/data_mapper_helper.rb
|
47
|
+
- test/have_code_test.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/bkerley/have-code
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --charset=UTF-8
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: have-code
|
71
|
+
rubygems_version: 1.3.1
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: TODO
|
75
|
+
test_files:
|
76
|
+
- test/active_record_helper.rb
|
77
|
+
- test/data_mapper_helper.rb
|
78
|
+
- test/have_code_test.rb
|
79
|
+
- test/test_helper.rb
|