identitee 0.5.0 → 0.6.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cb24ae4857e7d85789960a9ddaa4878c595378c
4
- data.tar.gz: e9d1d745ee288a215cad41b253314a53baceda29
3
+ metadata.gz: 9da74fd161fc249437c10e388e20164b4276780e
4
+ data.tar.gz: fd8111133efc9edc77bfa1e7020b41bbf88f8fca
5
5
  SHA512:
6
- metadata.gz: 17dc8a313f29d6af6c08e17a56bc5ac724d969251cdde10a16f4de9af96bdb5be6f27f3c36d0bd3ebe6f628a2aec59232fdbabff6d3540794e4b902460ced467
7
- data.tar.gz: f1cb6f464b16f390eccdc8337913c75d027e967e12d9aa3fe9779a2b89bec4a11508dff765a48c8375e61bcb3f12050dff36cb9595a136380ae8341eb3a9b994
6
+ metadata.gz: dbcf9d2ab80514b2f87de44ac15b3fb6d6c7c57fa6667141ff40eb4cc798b33520fbaed4f5325e836973f2af79177019fa42a15cfca8bb9dfc286888589751ff
7
+ data.tar.gz: fb2518c7ec141523bb9df7337398096791ebc486af444bf48a64fecadb656150375f26a91985a74a5ae360f172a36eeaab244f22a9263ec45fd06767de6ced48
@@ -0,0 +1,3 @@
1
+ class EmptyLoader
2
+ def lazy_load key; end
3
+ end
@@ -1,9 +1,16 @@
1
1
  require 'identitee/identifiable_not_found'
2
+ require 'identitee/empty_loader'
2
3
 
3
4
  module Identitee
4
5
  class Identifiables
5
6
  def initialize
6
7
  @instances = {}
8
+ @loaders = []
9
+ @all_loaded = false
10
+ end
11
+
12
+ def add_loader loader
13
+ loaders << loader
7
14
  end
8
15
 
9
16
  def register key, instance
@@ -12,12 +19,16 @@ module Identitee
12
19
 
13
20
  def find key, *default_values, &block
14
21
  instances.fetch key.to_s do
15
- if block
16
- block.call key
17
- elsif default_values.length > 0
18
- default_values.first
19
- else
20
- raise ::Identitee::IdentifiableNotFound
22
+ lazy_load key
23
+
24
+ instances.fetch key.to_s do
25
+ if block
26
+ block.call key
27
+ elsif default_values.length > 0
28
+ default_values.first
29
+ else
30
+ raise ::Identitee::IdentifiableNotFound
31
+ end
21
32
  end
22
33
  end
23
34
  end
@@ -27,11 +38,20 @@ module Identitee
27
38
  end
28
39
 
29
40
  def all
41
+ load_all unless all_loaded
30
42
  instances.values
31
43
  end
32
44
 
45
+ def lazy_load key
46
+ (loaders.detect { |loader| loader.loadable? key } || EmptyLoader.new).lazy_load key
47
+ end
48
+
33
49
  private
34
50
 
35
- attr_reader :instances
51
+ def load_all
52
+ loaders.each { |loader| loader.load_all }
53
+ end
54
+
55
+ attr_reader :instances, :loaders, :all_loaded
36
56
  end
37
57
  end
@@ -3,40 +3,28 @@ require "identitee/loader"
3
3
 
4
4
  module Identitee
5
5
  module Identify
6
- def identify id, *extra_parameters, &block
6
+ def identify key, *extra_parameters, &block
7
7
  instance = new *extra_parameters
8
8
  instance.instance_eval &block if block_given?
9
9
 
10
- identifiables.register id, instance
10
+ identifiables.register key, instance
11
11
  instance
12
12
  end
13
13
 
14
- def find_identifiable candidate_id
15
- identifiables.find candidate_id.to_s do
16
- ::Identitee::Loader.new(identify_root_directory: identify_root_directory).lazy_load(candidate_id.to_s)
17
- identifiables.find candidate_id, "Unknown"
18
- end
14
+ def find_identifiable key, default=new, &block
15
+ identifiables.find key, default, &block
19
16
  end
20
17
 
21
18
  def find_identifiable_key instance
22
19
  identifiables.find_key instance
23
20
  end
24
21
 
25
- def all_identifiables
26
- load_all_identifiables
22
+ def find_all_identifiables
27
23
  identifiables.all
28
24
  end
29
25
 
30
- def load_all_identifiables
31
- ::Identitee::Loader.new(identify_root_directory: identify_root_directory).load_all
32
- end
33
-
34
- def set_identitee_root path_to_template_root
35
- @identify_root_directory = path_to_template_root
36
- end
37
-
38
- def identify_root_directory
39
- @identify_root_directory
26
+ def add_identifiable_loader loader
27
+ identifiables.add_loader loader
40
28
  end
41
29
 
42
30
  def identifiables
@@ -0,0 +1,7 @@
1
+ module Identitee
2
+ module IdentifyKey
3
+ def key
4
+ self.class.find_identifiable_key self
5
+ end
6
+ end
7
+ end
@@ -4,11 +4,13 @@ module Identitee
4
4
  @identify_root_directory = options.delete(:identify_root_directory)
5
5
  end
6
6
 
7
- def lazy_load filename
8
- full_path = [identify_root_directory, "#{filename}.rb"].compact.join('/')
7
+ def loadable? filename
8
+ File.exists? full_path(filename)
9
+ end
9
10
 
10
- if File.exists? full_path
11
- force_load full_path
11
+ def lazy_load filename
12
+ if loadable? filename
13
+ force_load full_path(filename)
12
14
  else
13
15
  yield if block_given?
14
16
  end
@@ -24,6 +26,10 @@ module Identitee
24
26
 
25
27
  attr_reader :identify_root_directory
26
28
 
29
+ def full_path filename
30
+ [identify_root_directory, "#{filename}.rb"].compact.join('/')
31
+ end
32
+
27
33
  def force_full_load
28
34
  @fully_loaded = true
29
35
 
@@ -1,3 +1,3 @@
1
1
  module Identitee
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/identitee.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "identitee/version"
2
2
  require "identitee/identify"
3
3
  require "identitee/default_path_builder"
4
+ require "identitee/identify_key"
4
5
  require "identitee/identify/find_alias"
5
6
 
6
7
  module Identitee
@@ -8,6 +9,7 @@ module Identitee
8
9
  base.extend ::Identitee::Identify
9
10
  base.extend ::Identitee::Identify::FindAlias unless base.method_defined? :find
10
11
 
11
- base.set_identitee_root Identitee::DefaultPathBuilder.to_path caller, base
12
+ identify_root_directory = Identitee::DefaultPathBuilder.to_path caller, base
13
+ base.add_identifiable_loader ::Identitee::Loader.new(identify_root_directory: identify_root_directory)
12
14
  end
13
15
  end
@@ -15,12 +15,8 @@ describe "Default the directory to the caller classes directory and the pluraliz
15
15
  end
16
16
  end
17
17
 
18
- it "has a identify_root_directory" do
19
- DefaultDirectorySetTest.identify_root_directory.should =~ /default_directory_set_tests/
20
- end
21
-
22
18
  it "lazy loads identifyables" do
23
- test = DefaultDirectorySetTest.find_identifiable :default_directory_test
19
+ test = DefaultDirectorySetTest.find_identifiable :default_directory_test, nil
24
20
  test.should_not be_nil
25
21
  test.should be_successful
26
22
  end
@@ -3,11 +3,11 @@ require 'spec_helper'
3
3
  describe "Force all Loading" do
4
4
  class LoadingAllTest
5
5
  include Identitee
6
- set_identitee_root File.expand_path("../loading_all_tests/", __FILE__)
6
+ add_identifiable_loader Identitee::Loader.new(identify_root_directory: File.expand_path("../loading_all_tests/", __FILE__))
7
7
  end
8
8
 
9
9
  it "loads all the identifiable before calling all" do
10
- identifiables = LoadingAllTest.all_identifiables
10
+ identifiables = LoadingAllTest.find_all_identifiables
11
11
  identifiables.length.should == 2
12
12
  end
13
13
  end
@@ -7,7 +7,7 @@ describe "Lazy Loading" do
7
7
 
8
8
  attr_accessor :successful
9
9
 
10
- set_identitee_root File.expand_path("../lazy_loading_tests/", __FILE__)
10
+ add_identifiable_loader Identitee::Loader.new(identify_root_directory: File.expand_path("../loading_all_tests/", __FILE__))
11
11
 
12
12
  def initialize
13
13
  @successful = false
@@ -18,10 +18,6 @@ describe "Lazy Loading" do
18
18
  end
19
19
  end
20
20
 
21
- it "has a identify_root_directory" do
22
- LazyLoadingTest.identify_root_directory.should =~ /lazy_loading_tests/
23
- end
24
-
25
21
  it "lazy loads identifyables" do
26
22
  test = LazyLoadingTest.find_identifiable :autoload_test
27
23
  test.should_not be_nil
@@ -0,0 +1,20 @@
1
+ module Identitee
2
+ describe IdentifyKey do
3
+
4
+ class IdentifyKeyExample
5
+ include Identitee
6
+ include IdentifyKey
7
+
8
+ add_identifiable_loader Identitee::Loader.new(identify_root_directory: File.expand_path("../loading_all_tests/", __FILE__))
9
+ end
10
+
11
+ it "should have the key method" do
12
+ expect(IdentifyKeyExample.new).to respond_to :key
13
+ end
14
+
15
+ it "should return the correct key" do
16
+ example = IdentifyKeyExample.identify :example_key_test
17
+ expect(example.key).to eq "example_key_test"
18
+ end
19
+ end
20
+ end
@@ -74,7 +74,7 @@ describe Identitee do
74
74
  TestNotReplacingFind.send(:include, Identitee)
75
75
 
76
76
  TestNotReplacingFind.find("something").should == "something"
77
- TestNotReplacingFind.find_identifiable("something").should == "Unknown"
77
+ TestNotReplacingFind.find_identifiable("something", "Unknown").should == "Unknown"
78
78
  end
79
79
 
80
80
  it "finds the key for an identifiable" do
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.5.0
4
+ version: 0.6.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-02-18 00:00:00.000000000 Z
11
+ date: 2014-10-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Identifies and stores instances of a class for later loading
14
14
  email:
@@ -30,10 +30,12 @@ files:
30
30
  - identitee.gemspec
31
31
  - lib/identitee.rb
32
32
  - lib/identitee/default_path_builder.rb
33
+ - lib/identitee/empty_loader.rb
33
34
  - lib/identitee/identifiable_not_found.rb
34
35
  - lib/identitee/identifiables.rb
35
36
  - lib/identitee/identify.rb
36
37
  - lib/identitee/identify/find_alias.rb
38
+ - lib/identitee/identify_key.rb
37
39
  - lib/identitee/loader.rb
38
40
  - lib/identitee/version.rb
39
41
  - spec/acceptance/alias_find_acceptance_spec.rb
@@ -47,6 +49,7 @@ files:
47
49
  - spec/integration/loader_integration_spec.rb
48
50
  - spec/integration/test_files/test_lazy_load.rb
49
51
  - spec/lib/identitee/identifiables_spec.rb
52
+ - spec/lib/identitee/identify_key_spec.rb
50
53
  - spec/lib/identitee/identify_spec.rb
51
54
  - spec/lib/identitee/loader_spec.rb
52
55
  - spec/spec_helper.rb
@@ -86,6 +89,7 @@ test_files:
86
89
  - spec/integration/loader_integration_spec.rb
87
90
  - spec/integration/test_files/test_lazy_load.rb
88
91
  - spec/lib/identitee/identifiables_spec.rb
92
+ - spec/lib/identitee/identify_key_spec.rb
89
93
  - spec/lib/identitee/identify_spec.rb
90
94
  - spec/lib/identitee/loader_spec.rb
91
95
  - spec/spec_helper.rb