hash_identable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +2 -0
- data/hash_identable.gemspec +28 -0
- data/lib/hash_identable.rb +19 -0
- data/lib/hash_identable/class_methods.rb +16 -0
- data/lib/hash_identable/configuration.rb +48 -0
- data/lib/hash_identable/exceptions.rb +4 -0
- data/lib/hash_identable/hsh_function.rb +28 -0
- data/lib/hash_identable/identity.rb +57 -0
- data/lib/hash_identable/locator.rb +18 -0
- data/lib/hash_identable/lookup_table.rb +51 -0
- data/lib/hash_identable/version.rb +3 -0
- data/spec/lib/hash_identable/class_methods_spec.rb +28 -0
- data/spec/lib/hash_identable/configuration_spec.rb +45 -0
- data/spec/lib/hash_identable/hsh_function_spec.rb +21 -0
- data/spec/lib/hash_identable/identity_spec.rb +64 -0
- data/spec/lib/hash_identable/locator_spec.rb +60 -0
- data/spec/lib/hash_identable/lookup_table_spec.rb +36 -0
- data/spec/lib/hash_identable_spec.rb +44 -0
- data/spec/spec_helper.rb +100 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e0ce71df7edb14d8f815f83ed1f4d963e5a65964
|
4
|
+
data.tar.gz: 29586b4a186cdce0f8bc80cddd1c4fe816aefc36
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8a008df4a8c8d2f1d9577741ca233b91f4402c8ad365666ea884c43355f8772f866f35fa22e897faf3a7bbc6fa51dc899ae21b53091ee7893942dfc945ad12b4
|
7
|
+
data.tar.gz: 5355dea4b4f462428d099a31ca61b06a15f58801f8e6e38ab6299e1346da47c8f137d268dd29a201221c1bf71b1d3def098d77faada4680c18966ec45e6d8336
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# HashIdentable
|
2
|
+
|
3
|
+
Why expose your public ID's if you don't need to? Hang hash_identable on your rails models and use the #uuid method to get a unique id of any item.
|
4
|
+
|
5
|
+
## Credits
|
6
|
+
|
7
|
+
This gem wraps the hashids.org ruby gem: http://hashids.org/ruby/
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'hash_identable', :git => 'https://github.com/octaviuslabs/hash_identable.git'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Assuming your model
|
22
|
+
```
|
23
|
+
class MyModel
|
24
|
+
attr_reader :id #HashIdentable depends on the presence of an ID
|
25
|
+
# Other code
|
26
|
+
end
|
27
|
+
|
28
|
+
```
|
29
|
+
Configure the gem
|
30
|
+
|
31
|
+
```
|
32
|
+
HashIdentable.config do |c|
|
33
|
+
c.set_salt "My Salt" #THIS VALUE SHOULD NEVER CHANGE FOR THE APPLICATION
|
34
|
+
c.set_length 36
|
35
|
+
c.add_object MyModel, 3
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
When you want the hashid:
|
40
|
+
```
|
41
|
+
obj = MyModel.new(id: 12)
|
42
|
+
obj.uuid
|
43
|
+
```
|
44
|
+
Or:
|
45
|
+
|
46
|
+
```
|
47
|
+
obj = MyModel.new(id: 12)
|
48
|
+
obj.identity.to_s
|
49
|
+
```
|
50
|
+
|
51
|
+
When you want to find an object based on the hashid:
|
52
|
+
```
|
53
|
+
obj = HashIdentable::Locator.find([<find_method>], id) # Non ActiveRecord
|
54
|
+
obj = HashIdentable::Locator::ActiveRecord.find(id) #ActiveRecord
|
55
|
+
```
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it ( https://github.com/[my-github-username]/hash_identable/fork )
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_identable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_identable"
|
8
|
+
spec.version = HashIdentable::VERSION
|
9
|
+
spec.authors = ["Jimi Smoot"]
|
10
|
+
spec.email = ["jsfour@gmail.com"]
|
11
|
+
spec.summary = %q{An easy uuid wrapper for ruby applications}
|
12
|
+
spec.description = %q{An easy uuid wrapper for ruby applications}
|
13
|
+
spec.homepage = "https://github.com/octaviuslabs/hash_identable"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "simplecov"
|
25
|
+
spec.add_runtime_dependency "hashids"
|
26
|
+
spec.add_runtime_dependency "hashie"
|
27
|
+
spec.add_runtime_dependency "activesupport"
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "hash_identable/version"
|
2
|
+
require "hash_identable/class_methods"
|
3
|
+
require "hash_identable/configuration"
|
4
|
+
require "hash_identable/lookup_table"
|
5
|
+
require "hash_identable/exceptions"
|
6
|
+
require "hash_identable/identity"
|
7
|
+
require "hash_identable/locator"
|
8
|
+
|
9
|
+
module HashIdentable
|
10
|
+
|
11
|
+
def identity
|
12
|
+
@identity ||= Identity.new(self.class, id)
|
13
|
+
end
|
14
|
+
|
15
|
+
def uuid
|
16
|
+
identity.encoded_id
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module HashIdentable
|
2
|
+
|
3
|
+
def self.config
|
4
|
+
yield(configuration)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
class Configuration
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@hash_length = 36
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_object klass, id
|
18
|
+
klass.class_eval do
|
19
|
+
include(HashIdentable)
|
20
|
+
end
|
21
|
+
lookup_table.store(id, klass.to_s)
|
22
|
+
end
|
23
|
+
|
24
|
+
def salt
|
25
|
+
return @salt unless @salt.nil?
|
26
|
+
raise "You must set the salt for the system"
|
27
|
+
end
|
28
|
+
|
29
|
+
def hash_length
|
30
|
+
return @hash_length unless @hash_length.nil?
|
31
|
+
raise "You must set the hash_length for the system"
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_salt(new_salt)
|
35
|
+
@salt = new_salt
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_length(length_of_string)
|
39
|
+
@hash_length = length_of_string
|
40
|
+
end
|
41
|
+
private
|
42
|
+
def lookup_table
|
43
|
+
HashIdentable::lookup_table
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "hashids"
|
2
|
+
|
3
|
+
module HashIdentable
|
4
|
+
module HshFunction
|
5
|
+
|
6
|
+
def self.hashing_object
|
7
|
+
@hashing_object ||= Hashids.new(salt, hash_length)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.encode key
|
11
|
+
hashing_object.encode key
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.decode item
|
15
|
+
hashing_object.decode item
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def self.salt
|
20
|
+
@salt ||= HashIdentable.configuration.salt
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.hash_length
|
24
|
+
@hash_length ||= HashIdentable.configuration.hash_length
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "hash_identable/exceptions"
|
2
|
+
require "hash_identable/hsh_function"
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
|
6
|
+
class Identity
|
7
|
+
attr_reader :klass, :id
|
8
|
+
|
9
|
+
def initialize klass, id
|
10
|
+
@klass = klass.to_s
|
11
|
+
@id = id
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
encoded_id.to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
def hash_table_id
|
19
|
+
@hash_table_id ||= HashIdentable.lookup_table.fetch_id(klass){ raise NoObjectRegistered, "No object registered" }
|
20
|
+
end
|
21
|
+
|
22
|
+
def hash_key
|
23
|
+
raise "An ID is required to build a #hash_key" if id.nil? or klass.nil?
|
24
|
+
return [hash_table_id, id]
|
25
|
+
end
|
26
|
+
|
27
|
+
def encoded_id
|
28
|
+
id_wrapper.encode(hash_key)
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_h
|
32
|
+
{
|
33
|
+
klass: klass,
|
34
|
+
id: id,
|
35
|
+
encoded_id: encoded_id
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def klass_constant
|
40
|
+
klass.constantize
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.find(encoded_id)
|
44
|
+
decoded = HshFunction.decode(encoded_id)
|
45
|
+
klass = HashIdentable.lookup_table.fetch(decoded[0]){ raise NoObjectRegistered, "No object registered" }
|
46
|
+
new klass, decoded[1]
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def id_wrapper
|
52
|
+
HshFunction
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "hash_identable/identity"
|
2
|
+
|
3
|
+
module HashIdentable
|
4
|
+
module Locator
|
5
|
+
def self.find(key, find_method=:find)
|
6
|
+
identity = Identity.find(key)
|
7
|
+
return identity.klass_constant.send(find_method, identity.id)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActiveRecord
|
11
|
+
|
12
|
+
def self.find(key)
|
13
|
+
HashIdentable::Locator.find(key, :find)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "active_support/inflector"
|
2
|
+
|
3
|
+
module HashIdentable
|
4
|
+
|
5
|
+
def self.lookup_table
|
6
|
+
@lookup_table ||= LookupTable.new
|
7
|
+
end
|
8
|
+
|
9
|
+
class LookupTable < Hash
|
10
|
+
|
11
|
+
def fetch_id klass, &blk
|
12
|
+
begin
|
13
|
+
klass = serialzie(klass)
|
14
|
+
return invert[klass]
|
15
|
+
rescue
|
16
|
+
return blk.call if block_given?
|
17
|
+
return nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def fetch key, &blk
|
22
|
+
begin
|
23
|
+
return de_serialize(self[key])
|
24
|
+
rescue
|
25
|
+
return blk.call if block_given?
|
26
|
+
return nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def store key, klass
|
31
|
+
klass = serialzie(klass)
|
32
|
+
if has_key?(key)
|
33
|
+
raise "Id's for objects must be unique"
|
34
|
+
end
|
35
|
+
if has_value?(klass)
|
36
|
+
raise "Object is already registered"
|
37
|
+
end
|
38
|
+
super(key, klass)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def serialzie klass
|
43
|
+
klass.to_s.underscore
|
44
|
+
end
|
45
|
+
|
46
|
+
def de_serialize klass
|
47
|
+
klass.to_s.camelize
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hash_identable'
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
module TestData
|
6
|
+
class TestObject
|
7
|
+
include HashIdentable
|
8
|
+
attr_reader :id
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
describe HashIdentable do
|
14
|
+
subject(:klass) { TestData::TestObject }
|
15
|
+
it { is_expected.to respond_to :has_hashid}
|
16
|
+
it "should allow you to find based on hashid"
|
17
|
+
|
18
|
+
describe "#has_hashid" do
|
19
|
+
subject do
|
20
|
+
klass.has_hashid(3)
|
21
|
+
end
|
22
|
+
it { expect{subject}.to change{HashIdentable::lookup_table.keys.count}.from(0).to(1) }
|
23
|
+
it { expect(subject).to eql "hash_identable/test_data/test_object" }
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hash_identable'
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
module TestData
|
6
|
+
class Foo; end
|
7
|
+
end
|
8
|
+
describe Configuration do
|
9
|
+
subject(:config){ Configuration.new }
|
10
|
+
|
11
|
+
describe "salt" do
|
12
|
+
subject{ config.salt }
|
13
|
+
context "with no salt configured" do
|
14
|
+
it { expect{subject}.to raise_error "You must set the salt for the system" }
|
15
|
+
end
|
16
|
+
context "salt configured" do
|
17
|
+
before{ config.set_salt "Salty Cookies" }
|
18
|
+
it { is_expected.to eql "Salty Cookies" }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#set_salt" do
|
23
|
+
subject do
|
24
|
+
config.set_salt "the salt string"
|
25
|
+
end
|
26
|
+
it { is_expected.to eql "the salt string" }
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#add_object" do
|
31
|
+
subject do
|
32
|
+
config.add_object TestData::Foo, 3
|
33
|
+
end
|
34
|
+
it { is_expected.to be_truthy }
|
35
|
+
it { subject; expect(TestData::Foo.included_modules).to include(HashIdentable)}
|
36
|
+
it { expect{subject}.to change{HashIdentable::lookup_table.keys.count}.from(0).to(1) }
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hash_identable/hsh_function'
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
module HshFunction
|
6
|
+
def self.salt
|
7
|
+
"this is my salt"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
describe HshFunction do
|
11
|
+
describe "#decode" do
|
12
|
+
subject{ HshFunction.decode "k54p36QKgnM7LbXGkLhr10EJ1WweRNr9ODx2" }
|
13
|
+
it{ is_expected.to eql [3, 500]}
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#encode" do
|
17
|
+
subject{ HshFunction.encode [3, 500] }
|
18
|
+
it { is_expected.to eql "k54p36QKgnM7LbXGkLhr10EJ1WweRNr9ODx2"}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hash_identable/identity'
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
module TestStubs
|
6
|
+
|
7
|
+
class ActiveRecordObject; end
|
8
|
+
def self.decode key
|
9
|
+
[3, 500]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
describe Identity do
|
13
|
+
|
14
|
+
describe "class" do
|
15
|
+
describe "#find" do
|
16
|
+
subject do
|
17
|
+
HshFunction.extend(TestStubs)
|
18
|
+
HashIdentable.lookup_table.store(3, TestStubs::ActiveRecordObject.new.class)
|
19
|
+
Identity.find("k54p36QKgnM7LbXGkLhr10EJ1WweRNr9ODx2")
|
20
|
+
end
|
21
|
+
it{ expect(subject.klass).to eql "HashIdentable::TestStubs::ActiveRecordObject" }
|
22
|
+
it{ expect(subject.id).to eql 500 }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "insance" do
|
27
|
+
let(:klass){ double("ActiveRecordModel") }
|
28
|
+
subject(:obj) do
|
29
|
+
Identity.new("ActiveRecordModel", 500)
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "hash_key" do
|
33
|
+
subject do
|
34
|
+
obj.hash_key
|
35
|
+
end
|
36
|
+
|
37
|
+
context "has id" do
|
38
|
+
before do
|
39
|
+
allow(obj).to receive(:hash_table_id) { 3 }
|
40
|
+
end
|
41
|
+
it{ is_expected.to eql [3, 500]}
|
42
|
+
end
|
43
|
+
|
44
|
+
context "no id" do
|
45
|
+
before do
|
46
|
+
allow(obj).to receive(:hash_table_id) { 3 }
|
47
|
+
allow(obj).to receive(:id) { nil }
|
48
|
+
end
|
49
|
+
it{ expect{subject}.to raise_error "An ID is required to build a #hash_key"}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#to_s" do
|
54
|
+
before do
|
55
|
+
allow(obj).to receive(:hash_table_id) { 3 }
|
56
|
+
end
|
57
|
+
subject do
|
58
|
+
obj.to_s
|
59
|
+
end
|
60
|
+
it { is_expected.to eql "k54p36QKgnM7LbXGkLhr10EJ1WweRNr9ODx2"} # from http://codepen.io/ivanakimov/pen/bNmExm
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hash_identable/locator'
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
module Locator
|
6
|
+
module TestData
|
7
|
+
class Tobj
|
8
|
+
attr_reader :id
|
9
|
+
include HashIdentable
|
10
|
+
|
11
|
+
def self.find(id)
|
12
|
+
return new
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Obj1 < Tobj; end
|
17
|
+
class Obj2 < Tobj; end
|
18
|
+
class Obj3 < Tobj; end
|
19
|
+
end
|
20
|
+
|
21
|
+
shared_examples "a found object" do
|
22
|
+
let(:obj1) { TestData::Obj1 }
|
23
|
+
let(:obj2) { TestData::Obj2 }
|
24
|
+
let(:obj3) { TestData::Obj3 }
|
25
|
+
let(:objs) do
|
26
|
+
obj_klass = [obj1, obj2, obj3]
|
27
|
+
objs = Array.new
|
28
|
+
obj_klass.each_with_index do |obj, i|
|
29
|
+
obj.has_hashid(100+Random.rand(1000))
|
30
|
+
obj = obj.new
|
31
|
+
allow(obj).to receive(:id){ i }
|
32
|
+
objs << obj
|
33
|
+
end
|
34
|
+
objs
|
35
|
+
end
|
36
|
+
it { is_expected.to be_truthy }
|
37
|
+
it { is_expected.to be_a TestData::Obj1 }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#find" do
|
41
|
+
subject do
|
42
|
+
obj = objs[0]
|
43
|
+
allow(Identity).to receive(:find){ Identity.new(obj.class.to_s, obj.id) }
|
44
|
+
Locator.find(3, :find)
|
45
|
+
end
|
46
|
+
it_should_behave_like "a found object"
|
47
|
+
end
|
48
|
+
|
49
|
+
module ActiveRecord
|
50
|
+
describe "#find" do
|
51
|
+
subject do
|
52
|
+
obj = objs[0]
|
53
|
+
allow(Identity).to receive(:find){ Identity.new(obj.class.to_s, obj.id) }
|
54
|
+
ActiveRecord.find(3)
|
55
|
+
end
|
56
|
+
it_should_behave_like "a found object"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hash_identable/lookup_table'
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
describe LookupTable do
|
6
|
+
subject(:table){ LookupTable.new }
|
7
|
+
|
8
|
+
describe "adds object" do
|
9
|
+
subject(:store){table.store(3, :first_test)}
|
10
|
+
|
11
|
+
context "unique" do
|
12
|
+
it { is_expected.to be_truthy }
|
13
|
+
it { is_expected.to eql "first_test"}
|
14
|
+
end
|
15
|
+
|
16
|
+
context "existing key" do
|
17
|
+
subject do
|
18
|
+
table.store(3, :new_test)
|
19
|
+
store
|
20
|
+
end
|
21
|
+
it { expect{subject}.to raise_error "Id's for objects must be unique" }
|
22
|
+
end
|
23
|
+
|
24
|
+
context "existing object" do
|
25
|
+
subject do
|
26
|
+
table.store(8, :first_test)
|
27
|
+
store
|
28
|
+
end
|
29
|
+
it { expect{subject}.to raise_error "Object is already registered" }
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hash_identable'
|
3
|
+
|
4
|
+
module HashIdentable
|
5
|
+
module TestStub
|
6
|
+
Identity = Struct.new(:id) do
|
7
|
+
def encoded_id
|
8
|
+
"k54p36QKgnM7LbXGkLhr10EJ1WweRNr9ODx2"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def identity
|
13
|
+
Identity.new(1)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe HashIdentable do
|
19
|
+
subject(:obj)do
|
20
|
+
obj = instance_double("ActiveRecordObject")
|
21
|
+
allow(obj).to receive(:id){ 500 }
|
22
|
+
obj.extend(HashIdentable)
|
23
|
+
obj
|
24
|
+
end
|
25
|
+
|
26
|
+
it { is_expected.to respond_to(:uuid) }
|
27
|
+
it { is_expected.to respond_to(:identity) }
|
28
|
+
describe "#identity" do
|
29
|
+
subject{ obj.identity }
|
30
|
+
it { is_expected.to be_a Identity }
|
31
|
+
it { expect(subject.id).to eql 500 }
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#uuid" do
|
35
|
+
subject do
|
36
|
+
obj.extend(TestStub)
|
37
|
+
obj.uuid
|
38
|
+
end
|
39
|
+
it { is_expected.to be_a String }
|
40
|
+
it { is_expected.to eql "k54p36QKgnM7LbXGkLhr10EJ1WweRNr9ODx2" }
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
require 'hash_identable'
|
4
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
5
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
6
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
7
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
8
|
+
# files.
|
9
|
+
#
|
10
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
11
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
12
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
13
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
14
|
+
# a separate helper file that requires the additional dependencies and performs
|
15
|
+
# the additional setup, and require it from the spec files that actually need
|
16
|
+
# it.
|
17
|
+
#
|
18
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
19
|
+
# users commonly want.
|
20
|
+
#
|
21
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
22
|
+
RSpec.configure do |config|
|
23
|
+
config.filter_run focus: true
|
24
|
+
config.run_all_when_everything_filtered = true
|
25
|
+
|
26
|
+
config.before(:each) do
|
27
|
+
HashIdentable.lookup_table.clear
|
28
|
+
end
|
29
|
+
# rspec-expectations config goes here. You can use an alternate
|
30
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
31
|
+
# assertions if you prefer.
|
32
|
+
config.expect_with :rspec do |expectations|
|
33
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
34
|
+
# and `failure_message` of custom matchers include text for helper methods
|
35
|
+
# defined using `chain`, e.g.:
|
36
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
37
|
+
# # => "be bigger than 2 and smaller than 4"
|
38
|
+
# ...rather than:
|
39
|
+
# # => "be bigger than 2"
|
40
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
44
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
45
|
+
config.mock_with :rspec do |mocks|
|
46
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
47
|
+
# a real object. This is generally recommended, and will default to
|
48
|
+
# `true` in RSpec 4.
|
49
|
+
mocks.verify_partial_doubles = true
|
50
|
+
end
|
51
|
+
|
52
|
+
# The settings below are suggested to provide a good initial experience
|
53
|
+
# with RSpec, but feel free to customize to your heart's content.
|
54
|
+
=begin
|
55
|
+
# These two settings work together to allow you to limit a spec run
|
56
|
+
# to individual examples or groups you care about by tagging them with
|
57
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
58
|
+
# get run.
|
59
|
+
config.filter_run :focus
|
60
|
+
config.run_all_when_everything_filtered = true
|
61
|
+
|
62
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
63
|
+
# recommended. For more details, see:
|
64
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
65
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
66
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
67
|
+
config.disable_monkey_patching!
|
68
|
+
|
69
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
70
|
+
# be too noisy due to issues in dependencies.
|
71
|
+
config.warnings = true
|
72
|
+
|
73
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
74
|
+
# file, and it's useful to allow more verbose output when running an
|
75
|
+
# individual spec file.
|
76
|
+
if config.files_to_run.one?
|
77
|
+
# Use the documentation formatter for detailed output,
|
78
|
+
# unless a formatter has already been configured
|
79
|
+
# (e.g. via a command-line flag).
|
80
|
+
config.default_formatter = 'doc'
|
81
|
+
end
|
82
|
+
|
83
|
+
# Print the 10 slowest examples and example groups at the
|
84
|
+
# end of the spec run, to help surface which specs are running
|
85
|
+
# particularly slow.
|
86
|
+
config.profile_examples = 10
|
87
|
+
|
88
|
+
# Run specs in random order to surface order dependencies. If you find an
|
89
|
+
# order dependency and want to debug it, you can fix the order by providing
|
90
|
+
# the seed, which is printed after each run.
|
91
|
+
# --seed 1234
|
92
|
+
config.order = :random
|
93
|
+
|
94
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
95
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
96
|
+
# test failures related to randomization by passing the same `--seed` value
|
97
|
+
# as the one that triggered the failure.
|
98
|
+
Kernel.srand config.seed
|
99
|
+
=end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_identable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jimi Smoot
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: hashids
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: hashie
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activesupport
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: An easy uuid wrapper for ruby applications
|
112
|
+
email:
|
113
|
+
- jsfour@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- hash_identable.gemspec
|
125
|
+
- lib/hash_identable.rb
|
126
|
+
- lib/hash_identable/class_methods.rb
|
127
|
+
- lib/hash_identable/configuration.rb
|
128
|
+
- lib/hash_identable/exceptions.rb
|
129
|
+
- lib/hash_identable/hsh_function.rb
|
130
|
+
- lib/hash_identable/identity.rb
|
131
|
+
- lib/hash_identable/locator.rb
|
132
|
+
- lib/hash_identable/lookup_table.rb
|
133
|
+
- lib/hash_identable/version.rb
|
134
|
+
- spec/lib/hash_identable/class_methods_spec.rb
|
135
|
+
- spec/lib/hash_identable/configuration_spec.rb
|
136
|
+
- spec/lib/hash_identable/hsh_function_spec.rb
|
137
|
+
- spec/lib/hash_identable/identity_spec.rb
|
138
|
+
- spec/lib/hash_identable/locator_spec.rb
|
139
|
+
- spec/lib/hash_identable/lookup_table_spec.rb
|
140
|
+
- spec/lib/hash_identable_spec.rb
|
141
|
+
- spec/spec_helper.rb
|
142
|
+
homepage: https://github.com/octaviuslabs/hash_identable
|
143
|
+
licenses:
|
144
|
+
- MIT
|
145
|
+
metadata: {}
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - '>='
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - '>='
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
requirements: []
|
161
|
+
rubyforge_project:
|
162
|
+
rubygems_version: 2.0.14.1
|
163
|
+
signing_key:
|
164
|
+
specification_version: 4
|
165
|
+
summary: An easy uuid wrapper for ruby applications
|
166
|
+
test_files:
|
167
|
+
- spec/lib/hash_identable/class_methods_spec.rb
|
168
|
+
- spec/lib/hash_identable/configuration_spec.rb
|
169
|
+
- spec/lib/hash_identable/hsh_function_spec.rb
|
170
|
+
- spec/lib/hash_identable/identity_spec.rb
|
171
|
+
- spec/lib/hash_identable/locator_spec.rb
|
172
|
+
- spec/lib/hash_identable/lookup_table_spec.rb
|
173
|
+
- spec/lib/hash_identable_spec.rb
|
174
|
+
- spec/spec_helper.rb
|