gluer 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gluer/file.rb CHANGED
@@ -22,7 +22,7 @@ module Gluer
22
22
  touch
23
23
  end
24
24
 
25
- def rollback_all
25
+ def unload
26
26
  registration_pool.rollback
27
27
  registration_pool.clear
28
28
  end
@@ -1,6 +1,6 @@
1
1
  require 'gluer/configuration'
2
2
  require 'gluer/file'
3
- require 'gluer/ordered_set'
3
+ require 'set'
4
4
 
5
5
  module Gluer
6
6
  class FilePool
@@ -9,19 +9,21 @@ module Gluer
9
9
  end
10
10
 
11
11
  def get(path)
12
- files.detect { |file| file.path == path }
12
+ files.fetch(path)
13
13
  end
14
14
 
15
15
  def clear
16
- @files = OrderedSet.new
16
+ @files = Hash.new
17
17
  end
18
18
 
19
19
  def update
20
- new_files = OrderedSet.new(collect)
21
- diff = files - new_files
22
- diff.each(&:rollback_all)
23
- self.files = new_files
24
- files.each(&:reload)
20
+ updated_file_paths = Set.new(collect)
21
+ (current_file_paths - updated_file_paths).each do |old_path|
22
+ files.delete(old_path).unload
23
+ end
24
+ updated_file_paths.each do |path|
25
+ (files[path] ||= File.new(path)).reload
26
+ end
25
27
  end
26
28
 
27
29
  private
@@ -30,8 +32,11 @@ module Gluer
30
32
  def collect
31
33
  base_path = Gluer.config.base_path
32
34
  signature = Gluer.config.magic_signature
33
- filtered_file_paths = Gluer.config.file_filter.call(base_path, signature)
34
- filtered_file_paths.map { |path| File.new(path) }
35
+ Gluer.config.file_filter.call(base_path, signature)
36
+ end
37
+
38
+ def current_file_paths
39
+ Set.new(files.keys)
35
40
  end
36
41
  end
37
42
  end
data/lib/gluer/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gluer
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,121 @@
1
+ require 'gluer/file_pool'
2
+ require 'set'
3
+
4
+ describe Gluer::FilePool do
5
+ subject { Gluer::FilePool.new }
6
+
7
+ let(:file_paths) { ['path1', 'path2', 'path3'] }
8
+ let(:reloaded_files) { Hash.new }
9
+
10
+ def stub_file(path)
11
+ stub('File', :path => path).tap do |file|
12
+ file.stub(:unload)
13
+ file.stub(:reload) { reloaded_files[path] = file }
14
+ end
15
+ end
16
+
17
+ before do
18
+ @old_file_filter = Gluer.config.file_filter
19
+ Gluer.config.file_filter = ->(base_path, signature) { file_paths }
20
+
21
+ Gluer::File.stub(:new) { |path| stub_file(path) }
22
+
23
+ reloaded_files.clear
24
+ end
25
+
26
+ after do
27
+ Gluer.config.file_filter = @old_file_filter
28
+ end
29
+
30
+ describe "#update" do
31
+ before { subject.update }
32
+
33
+ it "reloads each file" do
34
+ Set[*reloaded_files.keys].should == Set[*file_paths]
35
+ end
36
+
37
+ it "makes them available from #get" do
38
+ expect { subject.get('path1') }.to_not raise_error
39
+ expect { subject.get('path2') }.to_not raise_error
40
+ expect { subject.get('path3') }.to_not raise_error
41
+ end
42
+
43
+ context "when filtering doesn't change in next call" do
44
+ before do
45
+ @file1 = reloaded_files['path1']
46
+ @file2 = reloaded_files['path2']
47
+ @file3 = reloaded_files['path3']
48
+
49
+ reloaded_files.clear
50
+ end
51
+
52
+ it "does not unload existing files" do
53
+ @file1.should_not_receive(:unload)
54
+ @file2.should_not_receive(:unload)
55
+ @file3.should_not_receive(:unload)
56
+
57
+ subject.update
58
+ end
59
+
60
+ it "keeps existing files available from #get" do
61
+ subject.update
62
+ expect { subject.get('path1') }.to_not raise_error
63
+ end
64
+
65
+ it "reloads existing files" do
66
+ @file1.should_receive(:reload)
67
+ @file2.should_receive(:reload)
68
+ @file3.should_receive(:reload)
69
+
70
+ subject.update
71
+ end
72
+ end
73
+
74
+ context "when filtering changes in next call" do
75
+ before do
76
+ new_file_paths = file_paths - ['path1'] + ['path4']
77
+ Gluer.config.file_filter = ->(base_path, signature) { new_file_paths }
78
+
79
+ @file1 = reloaded_files['path1']
80
+ @file2 = reloaded_files['path2']
81
+ @file3 = reloaded_files['path3']
82
+
83
+ reloaded_files.clear
84
+ end
85
+
86
+ it "unloads the filtered out file" do
87
+ @file1.should_receive(:unload)
88
+
89
+ subject.update
90
+ end
91
+
92
+ it "does not reload the filtered out file" do
93
+ @file1.should_not_receive(:reload)
94
+
95
+ subject.update
96
+ end
97
+
98
+ specify "getting the unloaded file results in key error" do
99
+ subject.update
100
+ expect { subject.get('path1') }.to raise_error(KeyError)
101
+ end
102
+
103
+ it "reloads existing files" do
104
+ @file2.should_receive(:reload)
105
+ @file3.should_receive(:reload)
106
+
107
+ subject.update
108
+ end
109
+
110
+ it "loads the new file" do
111
+ subject.update
112
+ expect(reloaded_files.keys).to include('path4')
113
+ end
114
+
115
+ it "makes the new file available from #get" do
116
+ subject.update
117
+ expect { subject.get('path4') }.to_not raise_error
118
+ end
119
+ end
120
+ end
121
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gluer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-04 00:00:00.000000000 Z
12
+ date: 2013-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -119,6 +119,7 @@ files:
119
119
  - lib/gluer/registration_hook.rb
120
120
  - lib/gluer/registration_pool.rb
121
121
  - lib/gluer/version.rb
122
+ - spec/file_pool_spec.rb
122
123
  - spec/integration/default_usage_spec.rb
123
124
  - spec/registration_spec.rb
124
125
  - spec/spec_helper.rb
@@ -137,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
138
  version: '0'
138
139
  segments:
139
140
  - 0
140
- hash: -643542908819081747
141
+ hash: -2018170584366941344
141
142
  required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  none: false
143
144
  requirements:
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  version: '0'
147
148
  segments:
148
149
  - 0
149
- hash: -643542908819081747
150
+ hash: -2018170584366941344
150
151
  requirements: []
151
152
  rubyforge_project:
152
153
  rubygems_version: 1.8.25
@@ -154,6 +155,7 @@ signing_key:
154
155
  specification_version: 3
155
156
  summary: Configuration reloader
156
157
  test_files:
158
+ - spec/file_pool_spec.rb
157
159
  - spec/integration/default_usage_spec.rb
158
160
  - spec/registration_spec.rb
159
161
  - spec/spec_helper.rb