active_hash 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.1
1
+ 0.7.2
data/active_hash.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{active_hash}
8
- s.version = "0.7.1"
8
+ s.version = "0.7.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeff Dean", "Mike Dalessio", "Corey Innis", "Peter Jaros"]
12
- s.date = %q{2009-10-13}
12
+ s.date = %q{2009-10-21}
13
13
  s.email = %q{jeff@zilkey.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
@@ -66,3 +66,4 @@ Gem::Specification.new do |s|
66
66
  s.add_dependency(%q<activesupport>, [">= 2.2.2"])
67
67
  end
68
68
  end
69
+
@@ -1,23 +1,23 @@
1
1
  module ActiveFile
2
2
 
3
3
  class Base < ActiveHash::Base
4
- class_inheritable_accessor :filename, :root_path, :cached_mtime, :reload_active_file, :data_has_been_set
4
+ class_inheritable_accessor :filename, :root_path, :data_loaded
5
5
 
6
6
  class << self
7
+
7
8
  def all
8
- reload
9
+ reload unless data_loaded
9
10
  super
10
11
  end
11
12
 
12
- def reload(force = false)
13
- if force || should_reload?
14
- self.data = load_file
15
- end
13
+ def delete_all
14
+ self.data_loaded = true
15
+ super
16
16
  end
17
17
 
18
- def data=(array_of_hashes)
19
- write_inheritable_attribute :data_has_been_set, true
20
- super
18
+ def reload(foo = true)
19
+ self.data_loaded = true
20
+ self.data = load_file
21
21
  end
22
22
 
23
23
  def set_filename(name)
@@ -38,16 +38,6 @@ module ActiveFile
38
38
  File.join(root_path, "#{filename}.#{extension}")
39
39
  end
40
40
 
41
- def should_reload?
42
- return false if read_inheritable_attribute(:data_has_been_set) && ! read_inheritable_attribute(:reload_active_file)
43
- return false if (mtime = File.mtime(full_path)) == read_inheritable_attribute(:cached_mtime)
44
-
45
- write_inheritable_attribute :cached_mtime, mtime
46
- true
47
- end
48
-
49
- private :should_reload?
50
-
51
41
  def extension
52
42
  raise "Override Me"
53
43
  end
@@ -57,4 +47,4 @@ module ActiveFile
57
47
  end
58
48
  end
59
49
 
60
- end
50
+ end
@@ -10,14 +10,6 @@ describe ActiveFile::Base do
10
10
  Object.send :remove_const, :Country
11
11
  end
12
12
 
13
- describe ".reload_active_file" do
14
-
15
- it "returns false by default" do
16
- Country.reload_active_file.should be_nil
17
- end
18
-
19
- end
20
-
21
13
  describe ".filename=" do
22
14
  before do
23
15
  Country.filename = "foo-izzle"
@@ -88,178 +80,4 @@ describe ActiveFile::Base do
88
80
  end
89
81
  end
90
82
 
91
- describe ".all" do
92
- before do
93
- class MyClass
94
- end
95
- end
96
-
97
- it "loads the data from the load_file method" do
98
- class Country
99
- class << self
100
- def extension
101
- "myfile"
102
- end
103
-
104
- def load_file
105
- MyClass.load_file(full_path)
106
- end
107
- end
108
- end
109
-
110
- Country.reload_active_file = true
111
- File.stub!(:mtime).and_return(1234)
112
- MyClass.should_receive(:load_file).and_return([{:id => 1}, {:id => 2}, {:id => 3}])
113
-
114
- records = Country.all
115
- records.length.should == 3
116
- records.should =~ [Country.new(:id => 1), Country.new(:id => 2), Country.new(:id => 3)]
117
- end
118
-
119
- context "with reload=true" do
120
- it "does not re-fetch the data if the file's mtime has not changed" do
121
- class Country < ActiveFile::Base
122
- class << self
123
- def extension
124
- "myfile"
125
- end
126
-
127
- def load_file
128
- MyClass.load_file(full_path)
129
- end
130
- end
131
- end
132
-
133
- Country.reload_active_file = true
134
- File.stub!(:mtime).and_return(1234)
135
- MyClass.should_receive(:load_file).once.and_return([{:foo => :bar}])
136
- Country.all
137
- Country.all
138
- end
139
-
140
- it "does re-fetch the data if the yaml file's mtime has changed" do
141
- class Country < ActiveFile::Base
142
- class << self
143
- def extension
144
- "myfile"
145
- end
146
-
147
- def load_file
148
- MyClass.load_file(full_path)
149
- end
150
- end
151
- end
152
-
153
- Country.reload_active_file = true
154
- MyClass.should_receive(:load_file).twice.and_return([{:foo => :bar}])
155
-
156
- File.stub!(:mtime).and_return(1234)
157
- Country.all
158
-
159
- File.stub!(:mtime).and_return(3456)
160
- Country.all
161
- end
162
- end
163
-
164
- context "with reload=false" do
165
- it "does not re-fetch the data after the first call to .all" do
166
- class Country < ActiveFile::Base
167
- class << self
168
- def extension
169
- "myfile"
170
- end
171
-
172
- def load_file
173
- MyClass.load_file(full_path)
174
- end
175
- end
176
- end
177
-
178
- File.stub!(:mtime).once.and_return(1234)
179
- MyClass.should_receive(:load_file).once.and_return([{:foo => :bar}])
180
- Country.all
181
-
182
- Country.all
183
- end
184
-
185
- it "does not re-fetch the data if the yaml file's mtime has changed" do
186
- class Country < ActiveFile::Base
187
- class << self
188
- def extension
189
- "myfile"
190
- end
191
-
192
- def load_file
193
- MyClass.load_file(full_path)
194
- end
195
- end
196
- end
197
-
198
- MyClass.should_receive(:load_file).once.and_return([{:foo => :bar}])
199
-
200
- File.stub!(:mtime).and_return(1234)
201
- Country.all
202
-
203
- File.stub!(:mtime).and_return(3456)
204
- Country.all
205
- end
206
-
207
- it "does not fetch data if data has been set to non-nil" do
208
- class Country < ActiveFile::Base
209
- class << self
210
- def extension
211
- "myfile"
212
- end
213
-
214
- def load_file
215
- MyClass.load_file(full_path)
216
- end
217
- end
218
- end
219
-
220
- MyClass.should_not_receive(:load_file)
221
- Country.data = [{:foo => :bar}]
222
- Country.all
223
- end
224
-
225
- it "does not fetch data if data has been set to nil" do
226
- class Country < ActiveFile::Base
227
- class << self
228
- def extension
229
- "myfile"
230
- end
231
-
232
- def load_file
233
- MyClass.load_file(full_path)
234
- end
235
- end
236
- end
237
-
238
- MyClass.should_not_receive(:load_file)
239
- Country.data = nil
240
- Country.all
241
- end
242
-
243
- it "does re-fetch data if force is true" do
244
- class Country < ActiveFile::Base
245
- class << self
246
- def extension
247
- "myfile"
248
- end
249
-
250
- def load_file
251
- MyClass.load_file(full_path)
252
- end
253
- end
254
- end
255
-
256
- MyClass.should_receive(:load_file).once.and_return([{:foo => :bar}])
257
- Country.data = nil
258
- Country.reload(true)
259
- Country.all.first.foo.should == :bar
260
- end
261
-
262
- end
263
- end
264
-
265
83
  end
@@ -21,6 +21,34 @@ describe ActiveYaml::Base do
21
21
  Object.send :remove_const, :State
22
22
  end
23
23
 
24
+ describe ".all" do
25
+
26
+ context "before the file is loaded" do
27
+ it "reads from the file" do
28
+ State.all.should_not be_empty
29
+ State.count.should > 0
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ describe ".delete_all" do
36
+ context "when called before .all" do
37
+ it "causes all to not load data" do
38
+ State.delete_all
39
+ State.all.should be_empty
40
+ end
41
+ end
42
+
43
+ context "when called after .all" do
44
+ it "clears out the data" do
45
+ State.all.should_not be_empty
46
+ State.delete_all
47
+ State.all.should be_empty
48
+ end
49
+ end
50
+ end
51
+
24
52
  describe ".raw_data" do
25
53
 
26
54
  it "returns the raw hash data loaded from yaml hash-formatted files" do
@@ -47,6 +75,7 @@ describe ActiveYaml::Base do
47
75
  it "returns an array of hashes" do
48
76
  City.load_file.should be_kind_of(Array)
49
77
  City.load_file.should include({"state" => :new_york, "name" => "Albany", "id" => 1})
78
+ City.reload
50
79
  City.all.should include( City.new(:id => 1) )
51
80
  end
52
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2009-10-13 00:00:00 -04:00
15
+ date: 2009-10-21 00:00:00 -04:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency