exegesis 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,57 +1,59 @@
1
- module Registerable
2
- def self.included(base)
3
- base.instance_eval do
4
- extend ClassMethods
5
- include InstanceMethods
1
+ module Exegesis
2
+ module Registerable
3
+ def self.included(base)
4
+ base.instance_eval do
5
+ extend ClassMethods
6
+ include InstanceMethods
7
+ end
6
8
  end
7
- end
8
9
 
9
- module ClassMethods
10
- def create(*args)
11
- retrieve(*args) || build(*args)
12
- end
10
+ module ClassMethods
11
+ def create(*args)
12
+ retrieve(*args) || build(*args)
13
+ end
13
14
 
14
- def retrieve(*args)
15
- path = build_path(*args.take(2))
16
- registry[path] if registry.has_key?(path)
17
- end
15
+ def retrieve(*args)
16
+ path = build_path(*args.take(2))
17
+ registry[path] if registry.has_key?(path)
18
+ end
18
19
 
19
- def build(*args)
20
- new(*args).tap do |instance|
21
- registry.register! instance
20
+ def build(*args)
21
+ new(*args).tap do |instance|
22
+ registry.register! instance
23
+ end
22
24
  end
23
- end
24
25
 
25
- def registry
26
- @flyweight ||= Flyweight.new do |dir|
27
- if dir.respond_to? :path
28
- dir.path
29
- else
30
- dir
26
+ def registry
27
+ @flyweight ||= Flyweight.new do |dir|
28
+ if dir.respond_to? :path
29
+ dir.path
30
+ else
31
+ dir
32
+ end
31
33
  end
32
34
  end
33
- end
34
35
 
35
- def clear_registry!
36
- registry.clear!
37
- end
36
+ def clear_registry!
37
+ registry.clear!
38
+ end
38
39
 
39
- def build_path(*args)
40
- parent, name = *args
41
- return parent if name.nil?
42
- File.join(parent.path, name)
40
+ def build_path(*args)
41
+ parent, name = *args
42
+ return parent if name.nil?
43
+ File.join(parent.path, name)
44
+ end
43
45
  end
44
- end
45
46
 
46
- module InstanceMethods
47
- def path
48
- build_path(parent, name)
49
- end
47
+ module InstanceMethods
48
+ def path
49
+ build_path(parent, name)
50
+ end
50
51
 
51
- private
52
+ private
52
53
 
53
- def build_path(parent, child)
54
- self.class.build_path(parent, child)
54
+ def build_path(parent, child)
55
+ self.class.build_path(parent, child)
56
+ end
55
57
  end
56
58
  end
57
59
  end
@@ -17,32 +17,34 @@
17
17
  # Collaborators:
18
18
  # SourceFile
19
19
  # SourceFileFactory -- to build the appropriate subclass based on file extenstion
20
- class SourceFile
21
- include FileSystemEntity
20
+ module Exegesis
21
+ class SourceFile
22
+ include FileSystemEntity
22
23
 
23
- def content
24
- fs_interface.read(path)
25
- end
24
+ def content
25
+ fs_interface.read(path)
26
+ end
26
27
 
27
- attr_reader :dependencies
28
- def depends_on(file)
29
- raise InvalidDependency unless file.is_a?(SourceFile)
30
- @dependencies << file
31
- end
28
+ attr_reader :dependencies
29
+ def depends_on(file)
30
+ raise InvalidDependency unless file.is_a?(SourceFile)
31
+ @dependencies << file
32
+ end
32
33
 
33
- private
34
+ private
34
35
 
35
- def initialize(parent, name, fs_interface = File)
36
- raise ArgumentError, "parent must be a directory" unless parent.is_a?(Directory)
36
+ def initialize(parent, name, fs_interface = File)
37
+ raise ArgumentError, "parent must be a directory" unless parent.is_a?(Directory)
37
38
 
38
- @fs_interface = fs_interface
39
- @ext = fs_interface.extname(name)
40
- @name = name
41
- @parent = parent
42
- @dependencies = []
39
+ @fs_interface = fs_interface
40
+ @ext = fs_interface.extname(name)
41
+ @name = name
42
+ @parent = parent
43
+ @dependencies = []
44
+ end
45
+
46
+ attr_reader :fs_interface
43
47
  end
44
48
 
45
- attr_reader :fs_interface
49
+ class InvalidDependency < StandardError ; end
46
50
  end
47
-
48
- class InvalidDependency < StandardError ; end
@@ -1,4 +1,4 @@
1
1
  module Exegesis
2
2
  #@private
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
@@ -1,9 +1,9 @@
1
1
  require 'integration_spec_helper'
2
2
 
3
- describe Flyweight, "mixin", "other objects" do
3
+ describe Exegesis::Flyweight, "mixin", "other objects" do
4
4
  before do
5
5
  class Example
6
- include Registerable
6
+ include Exegesis::Registerable
7
7
  def initialize(parent, name, *args)
8
8
  @parent = parent
9
9
  @name = name
@@ -1,7 +1,7 @@
1
1
  require 'integration_spec_helper'
2
2
 
3
3
  describe '#visit' do
4
- let (:dir) { BaseDirectory.create('./spec/fake_project') }
4
+ let (:dir) { Exegesis::BaseDirectory.create('./spec/fake_project') }
5
5
 
6
6
  context 'visitor with a proc' do
7
7
  subject { proc { } }
@@ -0,0 +1,15 @@
1
+ shared_examples_for 'a FileSystemEntity' do
2
+ describe 'api' do
3
+ it { should respond_to :path }
4
+ it { should respond_to :basename }
5
+ it { should respond_to :visit }
6
+
7
+ it { should respond_to :parent }
8
+ it { should respond_to :container } #alias
9
+
10
+ it { should respond_to :name }
11
+
12
+ it { should respond_to :ext }
13
+ it { should respond_to :extension } #alias
14
+ end
15
+ end
@@ -11,14 +11,17 @@ require 'rspec-spies'
11
11
  #include helpers
12
12
  Dir["./spec/helpers/*.rb"].each { |file| require file }
13
13
 
14
+ #include shared examples
15
+ Dir["./spec/shared/*_examples.rb"].each { |file| require file }
16
+
14
17
  RSpec.configure do |config|
15
18
  config.before do
16
19
  allow_message_expectations_on_nil
17
20
  end
18
21
 
19
22
  config.after do
20
- SourceFile.clear_registry!
21
- Directory.clear_registry!
23
+ Exegesis::SourceFile.clear_registry!
24
+ Exegesis::Directory.clear_registry!
22
25
  end
23
26
 
24
27
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -1,13 +1,12 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
- describe BaseDirectory do
3
+ describe Exegesis::BaseDirectory do
4
4
  let (:root) { double('some path to a project directory').as_null_object }
5
5
  let (:searcher) { double('directory searcher like FileSearcher') }
6
6
  let (:dir) { double('fake directory') }
7
7
  let (:file) { double('fake file') }
8
8
 
9
- let (:project) { BaseDirectory.create(root, searcher) }
10
-
9
+ let (:project) { Exegesis::BaseDirectory.create(root, searcher) }
11
10
 
12
11
  before do
13
12
  searcher.stub(:[]).with(root + '*').and_return([dir, file])
@@ -15,30 +14,30 @@ describe BaseDirectory do
15
14
  searcher.stub(:search).and_return(searcher)
16
15
  end
17
16
 
18
- context do
19
- subject { project }
20
-
21
- describe 'api' do
22
- it { should be_a Directory }
23
- it { should respond_to :root }
24
- it { should respond_to :directories }
25
- it { should respond_to :files }
26
- end
27
-
28
- describe '#root' do
29
- subject { project.root }
30
- it { should == root }
31
- end
32
-
33
- it { should delegate(:files).to(searcher) }
34
- it { should delegate(:directories).to(searcher) }
35
-
36
- #it should also provide interface for
37
- # * creating files/directories
38
- # - something like `directories << Directory('foo')` ?
39
- #
40
- # *
41
- #
42
- #it should also have a #visit method, see the NOTES doc for details on that
17
+ subject { project }
18
+
19
+ it_should_behave_like 'a FileSystemEntity'
20
+
21
+ describe 'api' do
22
+ it { should be_a Exegesis::Directory }
23
+ it { should respond_to :root }
24
+ it { should respond_to :directories }
25
+ it { should respond_to :files }
43
26
  end
27
+
28
+ describe '#root' do
29
+ subject { project.root }
30
+ it { should == root }
31
+ end
32
+
33
+ it { should delegate(:files).to(searcher) }
34
+ it { should delegate(:directories).to(searcher) }
35
+
36
+ #it should also provide interface for
37
+ # * creating files/directories
38
+ # - something like `directories << Directory('foo')` ?
39
+ #
40
+ # *
41
+ #
42
+ #it should also have a #visit method, see the NOTES doc for details on that
44
43
  end
@@ -1,14 +1,14 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
- describe Directory do
3
+ describe Exegesis::Directory do
4
4
  let (:parent) { double('parent directory') }
5
5
  let (:parent_path) { 'parent/' }
6
6
  let (:name) { 'subdir/' }
7
7
  let (:searcher) { double('searcher') }
8
- let (:directory) { Directory.create(parent, name, searcher) }
8
+ let (:directory) { Exegesis::Directory.create(parent, name, searcher) }
9
9
 
10
10
  before do
11
- parent.stub(:is_a?).with(Directory).and_return(true)
11
+ parent.stub(:is_a?).with(Exegesis::Directory).and_return(true)
12
12
  parent.stub(:path).and_return(parent_path)
13
13
 
14
14
  searcher.stub(:new).and_return(searcher)
@@ -16,12 +16,16 @@ describe Directory do
16
16
 
17
17
  subject { directory }
18
18
 
19
+ it_should_behave_like 'a FileSystemEntity'
20
+
19
21
  describe 'api' do
20
- it { should respond_to :directories }
21
- it { should respond_to :parent }
22
- it { should respond_to :files }
23
- it { should respond_to :path }
24
- it { should respond_to :children }
22
+ it { should respond_to :directories }
23
+ it { should respond_to :parent }
24
+ it { should respond_to :files }
25
+ it { should respond_to :path }
26
+ it { should respond_to :children }
27
+ it { should respond_to :find_file }
28
+ it { should respond_to :find_directory }
25
29
 
26
30
  its(:class) { should respond_to :create }
27
31
  end
@@ -35,4 +39,29 @@ describe Directory do
35
39
  it { should be_a String }
36
40
  it { should == File.join(parent_path, name) }
37
41
  end
42
+
43
+ describe '#find_file' do
44
+ let(:file_foo) { double("sourcefile('foo')", name: 'foo') }
45
+ let(:file_bar) { double("sourcefile('bar')", name: 'bar') }
46
+ let(:files) { [file_foo, file_bar] }
47
+
48
+ before { directory.stub(:files).and_return files }
49
+
50
+ subject { directory.find_file('foo') }
51
+
52
+ it { should be file_foo }
53
+ end
54
+
55
+ describe '#find_directory' do
56
+ let(:directory_foo) { double("directory('foo')", name: 'foo') }
57
+ let(:directory_bar) { double("directory('bar')", name: 'bar') }
58
+ let(:directories) { [directory_foo, directory_bar] }
59
+
60
+ before { directory.stub(:directories).and_return directories }
61
+
62
+ subject { directory.find_directory('foo') }
63
+
64
+ it { should be directory_foo }
65
+ end
66
+
38
67
  end
@@ -1,11 +1,11 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
- describe FileSearcher do
3
+ describe Exegesis::FileSearcher do
4
4
  let (:root) { double('a fake backend') }
5
5
  let (:root_path) { double('a fake path') }
6
6
  let (:globbed_path) { double('a fake glob of root_path with *') }
7
7
  let (:fs_interface) { double('a File System Interface like `File`') }
8
- let (:file_searcher) { FileSearcher.new(root, fs_interface) }
8
+ let (:file_searcher) { Exegesis::FileSearcher.new(root, fs_interface) }
9
9
  let (:dir) { double('a fake directory') }
10
10
  let (:file) { double('a fake file') }
11
11
  let (:file_path) { double('the fake file path') }
@@ -27,11 +27,11 @@ describe FileSearcher do
27
27
  fs_interface.stub(:join).with(root_path, file).and_return(file_path)
28
28
  fs_interface.stub(:join).with(root_path, dir).and_return(dir_path)
29
29
 
30
- Directory.stub(:new).with(root, dir).and_return(fake_directory_instance)
31
- SourceFile.stub(:new).with(root, file).and_return(fake_source_file_instance)
30
+ Exegesis::Directory.stub(:new).with(root, dir).and_return(fake_directory_instance)
31
+ Exegesis::SourceFile.stub(:new).with(root, file).and_return(fake_source_file_instance)
32
32
 
33
- fake_source_file_instance.stub(:is_a?).with(SourceFile).and_return(true)
34
- fake_directory_instance.stub(:is_a?).with(Directory).and_return(true)
33
+ fake_source_file_instance.stub(:is_a?).with(Exegesis::SourceFile).and_return(true)
34
+ fake_directory_instance.stub(:is_a?).with(Exegesis::Directory).and_return(true)
35
35
 
36
36
  root.stub(:path).and_return(root_path)
37
37
  end
@@ -67,8 +67,8 @@ describe FileSearcher do
67
67
  it { should contain(fake_directory_instance) }
68
68
  it { should exclude(fake_source_file_instance) }
69
69
 
70
- the_class(Directory) { should have_received(:new).with(root, dir) }
71
- the_class(SourceFile) { should_not have_received(:new) }
70
+ the_class(Exegesis::Directory) { should have_received(:new).with(root, dir) }
71
+ the_class(Exegesis::SourceFile) { should_not have_received(:new) }
72
72
  end
73
73
 
74
74
  describe '#files' do
@@ -78,8 +78,8 @@ describe FileSearcher do
78
78
  it { should contain(fake_source_file_instance) }
79
79
  it { should exclude(fake_directory_instance) }
80
80
 
81
- the_class(Directory) { should_not have_received(:new) }
82
- the_class(SourceFile) { should have_received(:new).with(root, file) }
81
+ the_class(Exegesis::Directory) { should_not have_received(:new) }
82
+ the_class(Exegesis::SourceFile) { should have_received(:new).with(root, file) }
83
83
  end
84
84
  end
85
85
  end
@@ -1,10 +1,10 @@
1
1
  require 'unit_spec_helper'
2
2
 
3
- describe Flyweight do
4
- let (:processor) { proc { |instance| instance_key } }
5
- let (:flyweight) { Flyweight.new(&processor) }
6
- let (:instance) { double('instance') }
7
- let (:instance_key) { double('instance_key') }
3
+ describe Exegesis::Flyweight do
4
+ let (:processor) { proc { |instance| instance_key } }
5
+ let (:flyweight) { Exegesis::Flyweight.new(&processor) }
6
+ let (:instance) { double('instance') }
7
+ let (:instance_key) { double('instance_key') }
8
8
 
9
9
  subject { flyweight }
10
10
 
@@ -17,12 +17,12 @@ describe Flyweight do
17
17
 
18
18
  describe 'unregistering an unused key' do
19
19
  it 'raises an error' do
20
- expect { flyweight.unregister!(key) }.to raise_error Flyweight::NoEntryError
20
+ expect { flyweight.unregister!(key) }.to raise_error Exegesis::Flyweight::NoEntryError
21
21
  end
22
22
 
23
23
  describe '#unregister' do
24
24
  it 'raises an error' do
25
- expect { flyweight.unregister(key) }.to_not raise_error Flyweight::NoEntryError
25
+ expect { flyweight.unregister(key) }.to_not raise_error Exegesis::Flyweight::NoEntryError
26
26
  end
27
27
  end
28
28
  end
@@ -46,12 +46,12 @@ describe Flyweight do
46
46
  describe '#register! and #register' do
47
47
  describe 'registering an instance with an already-used key' do
48
48
  it 'raises an error' do
49
- expect { flyweight.register!(instance) }.to raise_error Flyweight::AlreadyRegisteredError
49
+ expect { flyweight.register!(instance) }.to raise_error Exegesis::Flyweight::AlreadyRegisteredError
50
50
  end
51
51
 
52
52
  describe '#register' do
53
53
  it 'does not raise an error' do
54
- expect { flyweight.register(instance) }.to_not raise_error Flyweight::AlreadyRegisteredError
54
+ expect { flyweight.register(instance) }.to_not raise_error Exegesis::Flyweight::AlreadyRegisteredError
55
55
  end
56
56
  end
57
57
  end