identitee 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDZmNWUyM2Y2ZGY0MmU4MzEzNDE5MzljZGZkZGQwMGI5ZjU2NzY0OA==
5
+ data.tar.gz: !binary |-
6
+ OWZkMzhlZGQ4YzkyNDFmYTU3NmU1YTVkZTE1NjMyNmZlNDYwMDJjOQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ M2M1MTEwNDk0MzkxNjE2M2I0MWJiZWQ2MzExMjczOTM1OGUxNmU4NTUxMDU4
10
+ NzNkMWQ5MmIyZGM1NzZkODc5YWFhZTVkZTQyYTAyMWJiN2U5MjgwNWMzNzc4
11
+ YmUxYmIyNGJhZDY2ODM0ZTcwOGYxNDFlNzg2MGFmZDJkNmEyMzU=
12
+ data.tar.gz: !binary |-
13
+ NDg2Y2JiZGFmOWMxMGNiMDQwNGM0NDA5ZWM3Mjc2M2IwYzIzMzFjYjliMjI0
14
+ OWY3Mjk4MDkzYjMwMjM4MDI5YWJhNDMxZWNlMTZmOTIyNTZmNmVmNWVmMTA5
15
+ NTRmM2M0ZjY2M2RiMjkyZmIyMzk4YjFjOWFmMDA5ZDVhODM1Zjc=
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
+ vendor/ruby
19
+ .DS_Store
20
+ bin/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.1.0
5
+ - jruby-19mode
6
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'rspec'
5
+ gem 'rb-fsevent', '~> 0.9.1'
6
+ gem 'guard-rspec'
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'rspec', :notification => false, :all_on_start => false, :all_after_pass => false do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+
6
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
7
+
8
+ watch(%r{^spec/acceptance/(.+)\.feature$})
9
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
10
+ end
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nate Davis Olds
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,69 @@
1
+ # Identitee [![Build Status](https://travis-ci.org/natedavisolds/identitee.png?branch=master)](https://travis-ci.org/natedavisolds/identify) [![Code Climate](https://codeclimate.com/github/natedavisolds/identify.png)](https://codeclimate.com/github/natedavisolds/identify)
2
+
3
+ Provides methods for constructing and retrieving a domain object. Identify is intended to replace persisting data that is tightly couple to domain logic.
4
+
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
+
7
+ Likewise, many classes may be constructed that do the same thing... just a little bit differently. Like a use case. Identitee allows easy retrieval of data.
8
+
9
+ ### Finding
10
+
11
+ Find an identifiable through the `find_identifiable` class method. (See the Basic Example)
12
+
13
+ class Activity
14
+ include Identitee
15
+
16
+ attr_reader :title
17
+
18
+ def titled title
19
+ @title = title
20
+ end
21
+ end
22
+
23
+ Activity.identify :new_activity do
24
+ titled "Created something new"
25
+ end
26
+
27
+ Activity.find_identifiable(:new_activity).title # => "Created something new"
28
+
29
+ ### Loading identifiables
30
+
31
+ 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. The load path can explicitly be set with the `set_identify_root` method call.
32
+
33
+ class LazyLoadingTest
34
+ include Identitee
35
+
36
+ attr_accessor :successful
37
+
38
+ set_identitee_root File.expand_path("../lazy_loading_tests/", __FILE__)
39
+
40
+ def initialize
41
+ @successful = false
42
+ end
43
+
44
+ def successful?
45
+ @successful
46
+ end
47
+ end
48
+
49
+ ## Installation
50
+
51
+ Add this line to your application's Gemfile:
52
+
53
+ gem 'identitee'
54
+
55
+ And then execute:
56
+
57
+ $ bundle
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install identify
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/ROADMAP.md ADDED
@@ -0,0 +1,5 @@
1
+ ## ROADMAP
2
+
3
+ - Document a working example
4
+ - Allow for other/additional loaders to be used
5
+ - reduce the number of class methods that identify creates
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/identitee.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'identitee/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "identitee"
8
+ gem.version = Identitee::VERSION
9
+ gem.authors = ["Nate Davis Olds"]
10
+ gem.email = ["nate@davisolds.com"]
11
+ gem.description = "Identifies and stores instances of a class for later loading"
12
+ gem.summary = ""
13
+ gem.homepage = "https://github.com/natedavisolds/identify"
14
+ gem.licenses = ["MIT"]
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = []
17
+ gem.test_files = gem.files.grep(%r{^(spec)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,4 @@
1
+ module Identitee
2
+ class IdentifiableNotFound < Exception
3
+ end
4
+ end
@@ -0,0 +1,33 @@
1
+ require 'identitee/identifiable_not_found'
2
+
3
+ module Identitee
4
+ class Identifiables
5
+ def initialize
6
+ @instances = {}
7
+ end
8
+
9
+ def register key, instance
10
+ instances[key.to_s] = instance
11
+ end
12
+
13
+ def find key, *default_values, &block
14
+ 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
21
+ end
22
+ end
23
+ end
24
+
25
+ def all
26
+ instances.values
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :instances
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ module Identitee
2
+ module Identify
3
+ module FindAlias
4
+ def find candidate_id
5
+ find_identifiable candidate_id
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ require "identitee/identifiables"
2
+ require "identitee/loader"
3
+
4
+ module Identitee
5
+ module Identify
6
+ def identify id, *extra_parameters, &block
7
+ instance = new *extra_parameters
8
+ instance.instance_eval &block if block_given?
9
+
10
+ identifiables.register id, instance
11
+ instance
12
+ end
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
19
+ end
20
+
21
+ def all_identifiables
22
+ load_all_identifiables
23
+ identifiables.all
24
+ end
25
+
26
+ def load_all_identifiables
27
+ ::Identitee::Loader.new(identify_root_directory: identify_root_directory).load_all
28
+ end
29
+
30
+ def set_identify_root path_to_template_root
31
+ @identify_root_directory = path_to_template_root
32
+ end
33
+
34
+ def identify_root_directory
35
+ @identify_root_directory
36
+ end
37
+
38
+ def identifiables
39
+ @identifiables ||= Identifiables.new
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ module Identitee
2
+ class Loader
3
+ def initialize options={}
4
+ @identify_root_directory = options.delete(:identify_root_directory)
5
+ end
6
+
7
+ def lazy_load filename
8
+ full_path = [identify_root_directory, "#{filename}.rb"].compact.join('/')
9
+
10
+ if File.exists? full_path
11
+ force_load full_path
12
+ else
13
+ yield if block_given?
14
+ end
15
+ end
16
+
17
+ def load_all
18
+ if not_fully_loaded?
19
+ force_full_load
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :identify_root_directory
26
+
27
+ def force_full_load
28
+ @fully_loaded = true
29
+
30
+ Dir.glob("#{identify_root_directory}/*.rb").each do |file_name|
31
+ lazy_load File.basename(file_name, ".*")
32
+ end
33
+ end
34
+
35
+ def force_load filename
36
+ load filename.to_s
37
+ end
38
+
39
+ def loaded_already?
40
+ @fully_loaded == true
41
+ end
42
+
43
+ def not_fully_loaded?
44
+ not loaded_already?
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Identitee
2
+ VERSION = "0.4.0"
3
+ end
data/lib/identitee.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "identitee/version"
2
+ require "identitee/identify"
3
+ require "identitee/identify/find_alias"
4
+
5
+ module Identitee
6
+ def self.included base
7
+ base.extend ::Identitee::Identify
8
+ base.extend ::Identitee::Identify::FindAlias unless method_defined? :find
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ require 'identitee'
2
+
3
+ describe "Force all Loading" do
4
+
5
+ class LazyLoadingTest
6
+ include Identitee
7
+
8
+ attr_accessor :successful
9
+
10
+ set_identify_root File.expand_path("../lazy_loading_tests/", __FILE__)
11
+
12
+ def initialize
13
+ @successful = false
14
+ end
15
+
16
+ def successful?
17
+ @successful
18
+ end
19
+ end
20
+
21
+ it "loads all the identifiable before calling all" do
22
+ LazyLoadingTest.all_identifiables.length.should == 1
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'identitee'
2
+
3
+ describe "Lazy Loading" do
4
+
5
+ class LazyLoadingTest
6
+ include Identitee
7
+
8
+ attr_accessor :successful
9
+
10
+ set_identify_root File.expand_path("../lazy_loading_tests/", __FILE__)
11
+
12
+ def initialize
13
+ @successful = false
14
+ end
15
+
16
+ def successful?
17
+ @successful
18
+ end
19
+ end
20
+
21
+ it "has a identify_root_directory" do
22
+ LazyLoadingTest.identify_root_directory.should =~ /lazy_loading_tests/
23
+ end
24
+
25
+ it "lazy loads identifyables" do
26
+ test = LazyLoadingTest.find_identifiable :autoload_test
27
+ test.should_not be_nil
28
+ test.should be_successful
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ LazyLoadingTest.identify :autoload_test do
2
+ @successful = true
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'identitee/loader'
2
+
3
+ describe "Loader Integration Test" do
4
+ it "loads a file" do
5
+ identify_root_directory = File.expand_path('../test_files/', __FILE__)
6
+ Identitee::Loader.new(identify_root_directory: identify_root_directory).lazy_load('test_lazy_load')
7
+ Identitee::Loader.should respond_to :just_created_method
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ class Identitee::Loader
2
+ def self.just_created_method; end
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'identitee/identifiables'
2
+
3
+ module Identitee
4
+ describe Identifiables do
5
+ let(:instance) { stub }
6
+ let(:identifiables) { Identifiables.new }
7
+
8
+ context "when found by key" do
9
+ it "returns the instance" do
10
+ identifiables.register :foo, stub
11
+ identifiables.register :test_id, instance
12
+
13
+ identifiables.find(:test_id).should == instance
14
+ end
15
+ end
16
+
17
+ context "when not found by key" do
18
+ it "returns value returned by a given block" do
19
+ identifiable = identifiables.find(:foo) do |id|
20
+ "#{id} not found"
21
+ end
22
+
23
+ identifiable.should == "foo not found"
24
+ end
25
+
26
+ it "returns the second value if no block given" do
27
+ identifiable = identifiables.find :foo, "Default value"
28
+
29
+ identifiable.should == "Default value"
30
+ end
31
+
32
+ it "returns the second value if no block given even when that value is nil" do
33
+ identifiable = identifiables.find :foo, nil
34
+
35
+ identifiable.should be_nil
36
+ end
37
+
38
+ it "raises IdentifiableNotFound if no block or default value" do
39
+ expect { identifiables.find(:foo) }
40
+ .to raise_error Identitee::IdentifiableNotFound
41
+ end
42
+ end
43
+
44
+ context "when requesting all the identifiables" do
45
+ it "gives an array of all the known instances" do
46
+ identifiables.register :foo, instance
47
+ identifiables.all.should == [instance]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,79 @@
1
+ require 'identitee'
2
+
3
+ describe Identitee do
4
+ it "acts as a constructor" do
5
+ TestIdentifyInitialize = Class.new do
6
+ include Identitee
7
+ end
8
+
9
+ TestIdentifyInitialize.identify(:test).should be_a TestIdentifyInitialize
10
+ end
11
+
12
+ it "registers the identifiable" do
13
+ TestIdentifyRegister = Class.new do
14
+ include Identitee
15
+ end
16
+
17
+ instance = TestIdentifyRegister.identify :test
18
+
19
+ TestIdentifyRegister.find_identifiable(:test).should == instance
20
+ end
21
+
22
+ it "passes extra parameters through to new" do
23
+ TestIdentifyMultipleParams = Class.new do
24
+ include Identitee
25
+
26
+ attr_reader :attrib
27
+
28
+ def initialize attrib
29
+ @attrib = attrib
30
+ end
31
+ end
32
+
33
+ instance = TestIdentifyMultipleParams.identify :test, "Testing"
34
+ instance.attrib.should == "Testing"
35
+ end
36
+
37
+ it "passes the block to the new as well" do
38
+ TestIdentifyPassingBlock = Class.new do
39
+ include Identitee
40
+
41
+ attr_reader :attrib
42
+
43
+ def set_attrib str
44
+ @attrib = str
45
+ end
46
+ end
47
+
48
+ instance = TestIdentifyPassingBlock.identify :test do
49
+ set_attrib "It ran the block!"
50
+ end
51
+
52
+ instance.attrib.should == "It ran the block!"
53
+ end
54
+
55
+ it "simplifies find_identifiable to find" do
56
+ TestAddingFind = Class.new
57
+
58
+ TestAddingFind.should_not respond_to :find
59
+
60
+ TestAddingFind.send(:include, Identitee)
61
+
62
+ TestAddingFind.should respond_to :find
63
+ end
64
+
65
+ it "doesn't symplifies find_identifiable when find exists" do
66
+ TestNotReplacingFind = Class.new do
67
+ def self.find key
68
+ key
69
+ end
70
+ end
71
+
72
+ TestNotReplacingFind.find("something").should == "something"
73
+
74
+ TestNotReplacingFind.send(:include, Identitee)
75
+
76
+ TestNotReplacingFind.find("something").should == "something"
77
+ TestNotReplacingFind.find_identifiable("something").should == "Unknown"
78
+ end
79
+ end
@@ -0,0 +1,31 @@
1
+ require 'identitee/loader'
2
+
3
+ module Identitee
4
+ describe Loader do
5
+ context "when source file does not exist" do
6
+ it "fails gracefully by calling block" do
7
+ loader = Loader.new
8
+
9
+ loader.lazy_load('unknown') do
10
+ "Passing"
11
+ end.should == "Passing"
12
+ end
13
+ end
14
+
15
+ context "when source file does exist" do
16
+ it "loads the file" do
17
+ loader = Loader.new
18
+
19
+ File.stub!(:exists?).with('known.rb').and_return(true)
20
+
21
+ loader.should_receive(:force_load).with('known.rb').once
22
+
23
+ identity = loader.lazy_load('known') do
24
+ "Failing"
25
+ end
26
+
27
+ identity.should_not == "Failing"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: identitee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Nate Davis Olds
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Identifies and stores instances of a class for later loading
14
+ email:
15
+ - nate@davisolds.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - .rspec
22
+ - .travis.yml
23
+ - Gemfile
24
+ - Guardfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - ROADMAP.md
28
+ - Rakefile
29
+ - identitee.gemspec
30
+ - lib/identitee.rb
31
+ - lib/identitee/identifiable_not_found.rb
32
+ - lib/identitee/identifiables.rb
33
+ - lib/identitee/identify.rb
34
+ - lib/identitee/identify/find_alias.rb
35
+ - lib/identitee/loader.rb
36
+ - lib/identitee/version.rb
37
+ - spec/acceptance/force_all_loading_acceptance_spec.rb
38
+ - spec/acceptance/lazy_loading_acceptance_spec.rb
39
+ - spec/acceptance/lazy_loading_tests/autoload_test.rb
40
+ - spec/integration/loader_integration_spec.rb
41
+ - spec/integration/test_files/test_lazy_load.rb
42
+ - spec/lib/identitee/identifiables_spec.rb
43
+ - spec/lib/identitee/identify_spec.rb
44
+ - spec/lib/identitee/loader_spec.rb
45
+ - spec/spec_helper.rb
46
+ homepage: https://github.com/natedavisolds/identify
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.0.3
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: ''
70
+ test_files:
71
+ - spec/acceptance/force_all_loading_acceptance_spec.rb
72
+ - spec/acceptance/lazy_loading_acceptance_spec.rb
73
+ - spec/acceptance/lazy_loading_tests/autoload_test.rb
74
+ - spec/integration/loader_integration_spec.rb
75
+ - spec/integration/test_files/test_lazy_load.rb
76
+ - spec/lib/identitee/identifiables_spec.rb
77
+ - spec/lib/identitee/identify_spec.rb
78
+ - spec/lib/identitee/loader_spec.rb
79
+ - spec/spec_helper.rb