bkerley-have-code 0.1.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/LICENSE +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +59 -0
- data/VERSION.yml +4 -0
- data/lib/have-code.rb +21 -0
- data/lib/have-code/ar_code.rb +39 -0
- data/lib/have-code/dm_code.rb +39 -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 +76 -0
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/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,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
|
@@ -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,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bkerley-have-code
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bryce Kerley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07: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
|
+
- LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- VERSION.yml
|
39
|
+
- lib/have-code.rb
|
40
|
+
- lib/have-code/ar_code.rb
|
41
|
+
- lib/have-code/dm_code.rb
|
42
|
+
- test/active_record_helper.rb
|
43
|
+
- test/data_mapper_helper.rb
|
44
|
+
- test/have_code_test.rb
|
45
|
+
- test/test_helper.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/bkerley/have-code
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: have-code
|
68
|
+
rubygems_version: 1.2.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: TODO
|
72
|
+
test_files:
|
73
|
+
- test/active_record_helper.rb
|
74
|
+
- test/data_mapper_helper.rb
|
75
|
+
- test/have_code_test.rb
|
76
|
+
- test/test_helper.rb
|