identitee 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/Gemfile +4 -1
- data/README.md +44 -5
- data/lib/identitee/identify/find_alias.rb +6 -2
- data/lib/identitee/version.rb +1 -1
- data/spec/acceptance/alias_find_acceptance_spec.rb +15 -2
- data/spec/acceptance/default_directory_acceptance_spec.rb +2 -2
- data/spec/acceptance/force_all_loading_acceptance_spec.rb +1 -1
- data/spec/acceptance/lazy_loading_acceptance_spec.rb +2 -2
- data/spec/integration/loader_integration_spec.rb +1 -1
- data/spec/lib/identitee/identifiables_spec.rb +8 -8
- data/spec/lib/identitee/identify_key_spec.rb +2 -2
- data/spec/lib/identitee/identify_spec.rb +10 -10
- data/spec/lib/identitee/loader_spec.rb +6 -6
- data/spec/spec_helper.rb +0 -1
- metadata +10 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad8cee7849af1139f1d5c9b83870d81d17153efc
|
4
|
+
data.tar.gz: 9573dc1aede8998292d61d401cb72a827ef562ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8a9d6753888c4bad1fbcbc3b074fd181f7a9f6502c38c89ab23183ae3fd2d6aa63dfc0e986dce42ab1304a7093d4f330e4a19e48b6fe39e7ff3db6cc5fa7a53
|
7
|
+
data.tar.gz: 5c076e87ca24175305cbc56480b5788c50ba389de021a4c8e3e63cc1ec81623335901dc00e1ecc74ed9e3badd02a33a679eff7a6339a7342bf04e61fb86f9654
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.2
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -4,12 +4,13 @@ Provides methods for building and retrieving instances of a class. Identify is i
|
|
4
4
|
|
5
5
|
Many times you might see a database structure that will always remain static until the codebase changes. This is a headache to seed the proper data for the objects to work. Instead use identify to write these classes in code.
|
6
6
|
|
7
|
-
Likewise,
|
7
|
+
Likewise, Identitee extends composition by building objects which behave the same but with different values.
|
8
8
|
|
9
9
|
### Finding
|
10
10
|
|
11
11
|
Find an identifiable through the `find_identifiable` (aliased also to find) class method. (See the Basic Example)
|
12
12
|
|
13
|
+
```ruby
|
13
14
|
class Activity
|
14
15
|
include Identitee
|
15
16
|
|
@@ -26,17 +27,54 @@ Find an identifiable through the `find_identifiable` (aliased also to find) clas
|
|
26
27
|
|
27
28
|
Activity.find_identifiable(:new_activity).title # => "Created something new"
|
28
29
|
Activity.find(:new_activity).title # => "Created something new"
|
30
|
+
```
|
31
|
+
|
32
|
+
### When not found
|
29
33
|
|
30
|
-
|
34
|
+
You can set a default in two ways. First, pass the default as the second parameter to find
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
Activity.find("some_unknown_key", NullActivity.new)
|
38
|
+
```
|
39
|
+
|
40
|
+
If the key is not found, it will use the value passed as the second parameter.
|
41
|
+
|
42
|
+
Even more useful way to set a default value is through as block:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Activity.find("some_unknown_key") do |key|
|
46
|
+
Activity.identify(key)
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
When the key isn't found the block is run with the key passed to it. Here we are dynamically creating activities when they are not yet defined. This is great when we want the full power of the instance methods but we are okay with the default configuration (Think NullObject strategy).
|
51
|
+
|
52
|
+
### A Self aware instance
|
53
|
+
|
54
|
+
It is handy to have the key we used to identify. To give your instance objects access to key, include Identitee::IdentifyKey.
|
31
55
|
|
32
|
-
|
56
|
+
```ruby
|
57
|
+
class IdentifyKeyExample
|
58
|
+
include Identitee
|
59
|
+
include IdentifyKey
|
60
|
+
end
|
61
|
+
```
|
62
|
+
Access it by calling the #key method.
|
63
|
+
```ruby
|
64
|
+
example = IdentifyKeyExample.identify 'test_key'
|
65
|
+
example.key
|
66
|
+
# => 'text_key'
|
67
|
+
```
|
68
|
+
### Loading identifiables
|
33
69
|
|
70
|
+
Identitee will try to autoload the identifiables if it doesn't already know about them. By default it will use the pluralize class_name of the identifiable from the folder that the source file is in. Other loaders can be added to expose additional paths.
|
71
|
+
```ruby
|
34
72
|
class LazyLoadingTest
|
35
73
|
include Identitee
|
36
74
|
|
37
75
|
attr_accessor :successful
|
38
76
|
|
39
|
-
|
77
|
+
add_loader MyLoader.new
|
40
78
|
|
41
79
|
def initialize
|
42
80
|
@successful = false
|
@@ -46,6 +84,7 @@ Identitee will try to autoload the identifiables if it doesn't already know abou
|
|
46
84
|
@successful
|
47
85
|
end
|
48
86
|
end
|
87
|
+
```
|
49
88
|
|
50
89
|
## Installation
|
51
90
|
|
@@ -59,7 +98,7 @@ And then execute:
|
|
59
98
|
|
60
99
|
Or install it yourself as:
|
61
100
|
|
62
|
-
$ gem install
|
101
|
+
$ gem install identitee
|
63
102
|
|
64
103
|
## Contributing
|
65
104
|
|
@@ -1,8 +1,12 @@
|
|
1
1
|
module Identitee
|
2
2
|
module Identify
|
3
3
|
module FindAlias
|
4
|
-
def find candidate_id
|
5
|
-
|
4
|
+
def find candidate_id, default=nil, &block
|
5
|
+
if default.nil?
|
6
|
+
find_identifiable candidate_id, &block
|
7
|
+
else
|
8
|
+
find_identifiable candidate_id, default, &block
|
9
|
+
end
|
6
10
|
end
|
7
11
|
end
|
8
12
|
end
|
data/lib/identitee/version.rb
CHANGED
@@ -7,7 +7,7 @@ describe "Can find identifiables using the short hand find" do
|
|
7
7
|
|
8
8
|
it "uses the find alias" do
|
9
9
|
test_case = AliasFindingTestCase.identify :test
|
10
|
-
AliasFindingTestCase.find(:test).
|
10
|
+
expect(AliasFindingTestCase.find(:test)).to eq test_case
|
11
11
|
end
|
12
12
|
|
13
13
|
class OtherFindTestCase
|
@@ -20,6 +20,19 @@ describe "Can find identifiables using the short hand find" do
|
|
20
20
|
|
21
21
|
it "uses the existing find method" do
|
22
22
|
test_case = OtherFindTestCase.identify :test
|
23
|
-
OtherFindTestCase.find(:test).
|
23
|
+
expect(OtherFindTestCase.find(:test)).to eq "other find test"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "will pass a default value" do
|
27
|
+
oftc = AliasFindingTestCase.find(:test_default, "default")
|
28
|
+
expect(oftc).to eq "default"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "will pass a block value" do
|
32
|
+
oftc = AliasFindingTestCase.find(:test_default) do
|
33
|
+
"default"
|
34
|
+
end
|
35
|
+
|
36
|
+
expect(oftc).to eq "default"
|
24
37
|
end
|
25
38
|
end
|
@@ -17,7 +17,7 @@ describe "Default the directory to the caller classes directory and the pluraliz
|
|
17
17
|
|
18
18
|
it "lazy loads identifyables" do
|
19
19
|
test = DefaultDirectorySetTest.find_identifiable :default_directory_test, nil
|
20
|
-
test.
|
21
|
-
test.
|
20
|
+
expect(test).to_not be_nil
|
21
|
+
expect(test).to be_successful
|
22
22
|
end
|
23
23
|
end
|
@@ -4,6 +4,6 @@ describe "Loader Integration Test" do
|
|
4
4
|
it "loads a file" do
|
5
5
|
identify_root_directory = File.expand_path('../test_files/', __FILE__)
|
6
6
|
Identitee::Loader.new(identify_root_directory: identify_root_directory).lazy_load('test_lazy_load')
|
7
|
-
Identitee::Loader.
|
7
|
+
expect(Identitee::Loader).to respond_to :just_created_method
|
8
8
|
end
|
9
9
|
end
|
@@ -2,15 +2,15 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Identitee
|
4
4
|
describe Identifiables do
|
5
|
-
let(:instance) {
|
5
|
+
let(:instance) { double }
|
6
6
|
let(:identifiables) { Identifiables.new }
|
7
7
|
|
8
8
|
context "when found by key" do
|
9
9
|
it "returns the instance" do
|
10
|
-
identifiables.register :foo,
|
10
|
+
identifiables.register :foo, double
|
11
11
|
identifiables.register :test_id, instance
|
12
12
|
|
13
|
-
identifiables.find(:test_id).
|
13
|
+
expect(identifiables.find(:test_id)).to eq instance
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -20,19 +20,19 @@ module Identitee
|
|
20
20
|
"#{id} not found"
|
21
21
|
end
|
22
22
|
|
23
|
-
identifiable.
|
23
|
+
expect(identifiable).to eq "foo not found"
|
24
24
|
end
|
25
25
|
|
26
26
|
it "returns the second value if no block given" do
|
27
27
|
identifiable = identifiables.find :foo, "Default value"
|
28
28
|
|
29
|
-
identifiable.
|
29
|
+
expect(identifiable).to eq "Default value"
|
30
30
|
end
|
31
31
|
|
32
32
|
it "returns the second value if no block given even when that value is nil" do
|
33
33
|
identifiable = identifiables.find :foo, nil
|
34
34
|
|
35
|
-
identifiable.
|
35
|
+
expect(identifiable).to be_nil
|
36
36
|
end
|
37
37
|
|
38
38
|
it "raises IdentifiableNotFound if no block or default value" do
|
@@ -44,14 +44,14 @@ module Identitee
|
|
44
44
|
context "when requesting all the identifiables" do
|
45
45
|
it "gives an array of all the known instances" do
|
46
46
|
identifiables.register :foo, instance
|
47
|
-
identifiables.all.
|
47
|
+
expect(identifiables.all).to include instance
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
context "when find the key" do
|
52
52
|
it "gives the string identifier for the instance" do
|
53
53
|
identifiables.register :foo, instance
|
54
|
-
identifiables.find_key(instance).
|
54
|
+
expect(identifiables.find_key(instance)).to eq 'foo'
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
@@ -8,11 +8,11 @@ module Identitee
|
|
8
8
|
add_identifiable_loader Identitee::Loader.new(identify_root_directory: File.expand_path("../loading_all_tests/", __FILE__))
|
9
9
|
end
|
10
10
|
|
11
|
-
it "
|
11
|
+
it "has the key method" do
|
12
12
|
expect(IdentifyKeyExample.new).to respond_to :key
|
13
13
|
end
|
14
14
|
|
15
|
-
it "
|
15
|
+
it "returns the correct key" do
|
16
16
|
example = IdentifyKeyExample.identify :example_key_test
|
17
17
|
expect(example.key).to eq "example_key_test"
|
18
18
|
end
|
@@ -6,7 +6,7 @@ describe Identitee do
|
|
6
6
|
include Identitee
|
7
7
|
end
|
8
8
|
|
9
|
-
TestIdentifyInitialize.identify(:test).
|
9
|
+
expect(TestIdentifyInitialize.identify(:test)).to be_a TestIdentifyInitialize
|
10
10
|
end
|
11
11
|
|
12
12
|
it "registers the identifiable" do
|
@@ -16,7 +16,7 @@ describe Identitee do
|
|
16
16
|
|
17
17
|
instance = TestIdentifyRegister.identify :test
|
18
18
|
|
19
|
-
TestIdentifyRegister.find_identifiable(:test).
|
19
|
+
expect(TestIdentifyRegister.find_identifiable(:test)).to eq instance
|
20
20
|
end
|
21
21
|
|
22
22
|
it "passes extra parameters through to new" do
|
@@ -31,7 +31,7 @@ describe Identitee do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
instance = TestIdentifyMultipleParams.identify :test, "Testing"
|
34
|
-
instance.attrib.
|
34
|
+
expect(instance.attrib).to eq "Testing"
|
35
35
|
end
|
36
36
|
|
37
37
|
it "passes the block to the new as well" do
|
@@ -49,17 +49,17 @@ describe Identitee do
|
|
49
49
|
set_attrib "It ran the block!"
|
50
50
|
end
|
51
51
|
|
52
|
-
instance.attrib.
|
52
|
+
expect(instance.attrib).to eq "It ran the block!"
|
53
53
|
end
|
54
54
|
|
55
55
|
it "simplifies find_identifiable to find" do
|
56
56
|
TestAddingFind = Class.new
|
57
57
|
|
58
|
-
TestAddingFind.
|
58
|
+
expect(TestAddingFind).to_not respond_to :find
|
59
59
|
|
60
60
|
TestAddingFind.send(:include, Identitee)
|
61
61
|
|
62
|
-
TestAddingFind.
|
62
|
+
expect(TestAddingFind).to respond_to :find
|
63
63
|
end
|
64
64
|
|
65
65
|
it "doesn't symplify find_identifiable when find exists" do
|
@@ -69,12 +69,12 @@ describe Identitee do
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
TestNotReplacingFind.find("something").
|
72
|
+
expect(TestNotReplacingFind.find("something")).to eq "something"
|
73
73
|
|
74
74
|
TestNotReplacingFind.send(:include, Identitee)
|
75
75
|
|
76
|
-
TestNotReplacingFind.find("something").
|
77
|
-
TestNotReplacingFind.find_identifiable("something", "Unknown").
|
76
|
+
expect(TestNotReplacingFind.find("something")).to eq "something"
|
77
|
+
expect(TestNotReplacingFind.find_identifiable("something", "Unknown")).to eq "Unknown"
|
78
78
|
end
|
79
79
|
|
80
80
|
it "finds the key for an identifiable" do
|
@@ -84,6 +84,6 @@ describe Identitee do
|
|
84
84
|
|
85
85
|
instance = TestFindIdentiteeKey.identify :testing_key
|
86
86
|
|
87
|
-
TestFindIdentiteeKey.find_identifiable_key(instance).
|
87
|
+
expect(TestFindIdentiteeKey.find_identifiable_key(instance)).to eq 'testing_key'
|
88
88
|
end
|
89
89
|
end
|
@@ -4,11 +4,11 @@ module Identitee
|
|
4
4
|
describe Loader do
|
5
5
|
context "when source file does not exist" do
|
6
6
|
it "fails gracefully by calling block" do
|
7
|
-
|
8
|
-
|
9
|
-
loader.lazy_load('unknown') do
|
7
|
+
result = Loader.new.lazy_load('unknown') do
|
10
8
|
"Passing"
|
11
|
-
end
|
9
|
+
end
|
10
|
+
|
11
|
+
expect(result).to eq "Passing"
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -16,9 +16,9 @@ module Identitee
|
|
16
16
|
it "loads the file" do
|
17
17
|
loader = Loader.new
|
18
18
|
|
19
|
-
File.
|
19
|
+
expect(File).to receive(:exists?).with('known.rb').and_return(true)
|
20
20
|
|
21
|
-
loader.
|
21
|
+
expect(loader).to receive(:force_load).with('known.rb').once
|
22
22
|
|
23
23
|
loader.lazy_load('known')
|
24
24
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: identitee
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Davis Olds
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Identifies and stores instances of a class for later loading
|
14
14
|
email:
|
@@ -17,10 +17,11 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- .coveralls.yml
|
21
|
-
- .gitignore
|
22
|
-
- .rspec
|
23
|
-
- .
|
20
|
+
- ".coveralls.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".ruby-version"
|
24
|
+
- ".travis.yml"
|
24
25
|
- Gemfile
|
25
26
|
- Guardfile
|
26
27
|
- LICENSE.txt
|
@@ -63,17 +64,17 @@ require_paths:
|
|
63
64
|
- lib
|
64
65
|
required_ruby_version: !ruby/object:Gem::Requirement
|
65
66
|
requirements:
|
66
|
-
- -
|
67
|
+
- - ">="
|
67
68
|
- !ruby/object:Gem::Version
|
68
69
|
version: '0'
|
69
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
71
|
requirements:
|
71
|
-
- -
|
72
|
+
- - ">="
|
72
73
|
- !ruby/object:Gem::Version
|
73
74
|
version: '0'
|
74
75
|
requirements: []
|
75
76
|
rubyforge_project:
|
76
|
-
rubygems_version: 2.
|
77
|
+
rubygems_version: 2.2.2
|
77
78
|
signing_key:
|
78
79
|
specification_version: 4
|
79
80
|
summary: ''
|