Permalink
Browse files

default identitee directory

  • Loading branch information...
1 parent 459bc73 commit f4c23c2bc7f548aecb5b7022e2fa5f4781b207d9 @natedavisolds committed Feb 18, 2014
Showing with 83 additions and 0 deletions.
  1. +3 −0 lib/identitee.rb
  2. +44 −0 lib/identitee/default_path_builder.rb
  3. +36 −0 spec/acceptance/default_directory_acceptance_spec.rb
View
3 lib/identitee.rb
@@ -1,10 +1,13 @@
require "identitee/version"
require "identitee/identify"
+require "identitee/default_path_builder"
require "identitee/identify/find_alias"
module Identitee
def self.included base
base.extend ::Identitee::Identify
base.extend ::Identitee::Identify::FindAlias unless base.method_defined? :find
+
+ base.set_identitee_root Identitee::DefaultPathBuilder.to_path caller, base
end
end
View
44 lib/identitee/default_path_builder.rb
@@ -0,0 +1,44 @@
+module Identitee
+ class DefaultPathBuilder
+ def self.to_path call_stack, including_class
+ new(call_stack, including_class).to_path
+ end
+
+ def initialize call_stack, including_class
+ @call_stack = call_stack
+ @including_class = including_class
+ end
+
+ def to_path
+ [directory_of_caller, pluralized_class_name].join('/')
+ end
+
+ private
+
+ attr_accessor :call_stack, :including_class
+
+ def directory_of_caller
+ File.dirname most_recent_caller_path_without_line_number
+ end
+
+ def most_recent_caller_path_without_line_number
+ most_recent_caller_path.split(':').first
+ end
+
+ def most_recent_caller_path
+ call_stack.first.to_s
+ end
+
+ def pluralized_class_name
+ including_class_name.gsub(/([A-Z]+)([A-Z])/,'\1_\2').
+ gsub(/([a-z])([A-Z])/,'\1_\2').
+ gsub('/' ,'__').
+ gsub('::','__').
+ downcase + "s"
+ end
+
+ def including_class_name
+ including_class.name.to_s
+ end
+ end
+end
View
36 spec/acceptance/default_directory_acceptance_spec.rb
@@ -0,0 +1,36 @@
+require 'spec_helper'
+
+describe "Default the directory to the caller classes directory and the pluralized version of the class name" do
+ class LoadingAllTest
+ include Identitee
+ end
+
+ it "loads all the identifiable before calling all" do
+ identifiables = LoadingAllTest.all_identifiables
+ identifiables.length.should == 2
+ end
+
+ class LazyLoadingTest
+ include Identitee
+
+ attr_accessor :successful
+
+ def initialize
+ @successful = false
+ end
+
+ def successful?
+ @successful
+ end
+ end
+
+ it "has a identify_root_directory" do
+ LazyLoadingTest.identify_root_directory.should =~ /lazy_loading_tests/
+ end
+
+ it "lazy loads identifyables" do
+ test = LazyLoadingTest.find_identifiable :autoload_test
+ test.should_not be_nil
+ test.should be_successful
+ end
+end

0 comments on commit f4c23c2

Please sign in to comment.