zilkey-active_hash 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +4 -1
- data/README.md +16 -2
- data/VERSION +1 -1
- data/active_hash.gemspec +3 -3
- data/lib/active_file/base.rb +12 -17
- data/spec/active_file/base_spec.rb +120 -45
- metadata +2 -2
data/CHANGELOG
CHANGED
|
@@ -2,4 +2,7 @@
|
|
|
2
2
|
- Added support for auto-defining methods based on hash keys in ActiveHash::Base
|
|
3
3
|
- Changed the :field and :fields API so that they don't overwrite existing methods (useful when ActiveHash auto-defines methods)
|
|
4
4
|
- Fixed a bug where ActiveFile incorrectly set the root_path to be the path in the gem directory, not the current working directory
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
2009-07-24
|
|
7
|
+
- ActiveFile no longer reloads files by default
|
|
8
|
+
- Added ActiveFile.reload_active_file= so you can cause ActiveFile to reload
|
data/README.md
CHANGED
|
@@ -28,13 +28,16 @@ To use ActiveHash, you need to:
|
|
|
28
28
|
A quick example would be:
|
|
29
29
|
|
|
30
30
|
class Country < ActiveHash::Base
|
|
31
|
-
field :name
|
|
32
31
|
self.data = [
|
|
33
32
|
{:id => 1, :name => "US"},
|
|
34
33
|
{:id => 2, :name => "Canada"}
|
|
35
34
|
]
|
|
36
35
|
end
|
|
37
36
|
|
|
37
|
+
country = Country.new(:name => "Mexico")
|
|
38
|
+
country.name # => "Mexico"
|
|
39
|
+
country.name? # => true
|
|
40
|
+
|
|
38
41
|
## Auto-Defined fields
|
|
39
42
|
|
|
40
43
|
ActiveHash will auto-define all fields for you when you load the hash. For example, if you have the following class:
|
|
@@ -173,11 +176,22 @@ If you store encrypted data, or you'd like to store your flat files as CSV or XM
|
|
|
173
176
|
|
|
174
177
|
The two methods you need to implement are load_file, which needs to return an array of hashes, and .extension, which returns the file extension you are using. You have full_path available to you if you wish, or you can provide your own path.
|
|
175
178
|
|
|
179
|
+
Setting the default file location in Rails:
|
|
180
|
+
|
|
181
|
+
# config/initializers/active_file.rb
|
|
182
|
+
ActiveFile.set_root_path "config/activefiles"
|
|
183
|
+
|
|
184
|
+
By default, ActiveFile will not reload data from the file when it changes. You can enable this by setting:
|
|
185
|
+
|
|
186
|
+
ActiveFile.reload_active_file = true
|
|
187
|
+
|
|
188
|
+
In Rails, in development mode, it reloads the entire class, which reloads the file, so you don't need to turn this on in development.
|
|
189
|
+
|
|
176
190
|
NOTE: By default, .full_path refers to the current working directory. In a rails app, this will be RAILS_ROOT.
|
|
177
191
|
|
|
178
192
|
## Authors
|
|
179
193
|
|
|
180
|
-
Written by Mike Dalessio and
|
|
194
|
+
Written by Jeff Dean, Mike Dalessio and Ben Woosley
|
|
181
195
|
|
|
182
196
|
## Development
|
|
183
197
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.3.0
|
data/active_hash.gemspec
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Gem::Specification.new do |s|
|
|
4
4
|
s.name = %q{active_hash}
|
|
5
|
-
s.version = "0.
|
|
5
|
+
s.version = "0.3.0"
|
|
6
6
|
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
8
|
s.authors = ["Jeff Dean", "Mike Dalessio"]
|
|
9
|
-
s.date = %q{2009-07-
|
|
9
|
+
s.date = %q{2009-07-24}
|
|
10
10
|
s.email = %q{jeff@zilkey.com}
|
|
11
11
|
s.extra_rdoc_files = [
|
|
12
12
|
"LICENSE",
|
|
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
|
|
|
34
34
|
s.homepage = %q{http://github.com/zilkey/active_hash}
|
|
35
35
|
s.rdoc_options = ["--charset=UTF-8"]
|
|
36
36
|
s.require_paths = ["lib"]
|
|
37
|
-
s.rubygems_version = %q{1.3.
|
|
37
|
+
s.rubygems_version = %q{1.3.4}
|
|
38
38
|
s.summary = %q{An ActiveRecord-like model that uses a hash as a datasource}
|
|
39
39
|
s.test_files = [
|
|
40
40
|
"spec/active_file/base_spec.rb",
|
data/lib/active_file/base.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module ActiveFile
|
|
2
2
|
|
|
3
3
|
class Base < ActiveHash::Base
|
|
4
|
-
class_inheritable_accessor :filename, :root_path, :cached_mtime
|
|
4
|
+
class_inheritable_accessor :filename, :root_path, :cached_mtime, :reload_active_file
|
|
5
5
|
|
|
6
6
|
class << self
|
|
7
7
|
def all
|
|
@@ -21,24 +21,14 @@ module ActiveFile
|
|
|
21
21
|
write_inheritable_attribute :filename, name
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
protected :set_filename
|
|
25
|
-
|
|
26
24
|
def set_root_path(path)
|
|
27
25
|
write_inheritable_attribute :root_path, path
|
|
28
26
|
end
|
|
29
27
|
|
|
30
|
-
protected :set_root_path
|
|
31
|
-
|
|
32
28
|
def load_file
|
|
33
29
|
raise "Override Me"
|
|
34
30
|
end
|
|
35
31
|
|
|
36
|
-
def extension
|
|
37
|
-
raise "Override Me"
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
protected :extension
|
|
41
|
-
|
|
42
32
|
def full_path
|
|
43
33
|
root_path = read_inheritable_attribute(:root_path) || Dir.pwd
|
|
44
34
|
filename = read_inheritable_attribute(:filename) || name.tableize
|
|
@@ -46,16 +36,21 @@ module ActiveFile
|
|
|
46
36
|
end
|
|
47
37
|
|
|
48
38
|
def should_reload?
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
end
|
|
39
|
+
return false if read_inheritable_attribute(:data) && ! read_inheritable_attribute(:reload_active_file)
|
|
40
|
+
return false if (mtime = File.mtime(full_path)) == read_inheritable_attribute(:cached_mtime)
|
|
41
|
+
|
|
42
|
+
write_inheritable_attribute :cached_mtime, mtime
|
|
43
|
+
true
|
|
55
44
|
end
|
|
56
45
|
|
|
57
46
|
private :should_reload?
|
|
58
47
|
|
|
48
|
+
def extension
|
|
49
|
+
raise "Override Me"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
protected :extension
|
|
53
|
+
|
|
59
54
|
end
|
|
60
55
|
end
|
|
61
56
|
|
|
@@ -1,12 +1,26 @@
|
|
|
1
1
|
require 'spec/spec_helper'
|
|
2
2
|
|
|
3
3
|
describe ActiveFile::Base do
|
|
4
|
+
before do
|
|
5
|
+
class Country < ActiveFile::Base
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
after do
|
|
10
|
+
Object.send :remove_const, :Country
|
|
11
|
+
end
|
|
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
|
|
4
20
|
|
|
5
21
|
describe ".filename=" do
|
|
6
22
|
before do
|
|
7
|
-
|
|
8
|
-
self.filename = "foo-izzle"
|
|
9
|
-
end
|
|
23
|
+
Country.filename = "foo-izzle"
|
|
10
24
|
|
|
11
25
|
class Bar < ActiveFile::Base
|
|
12
26
|
self.filename = "bar-izzle"
|
|
@@ -14,16 +28,14 @@ describe ActiveFile::Base do
|
|
|
14
28
|
end
|
|
15
29
|
|
|
16
30
|
it "sets the filename on a per-subclass basis" do
|
|
17
|
-
|
|
31
|
+
Country.filename.should == "foo-izzle"
|
|
18
32
|
Bar.filename.should == "bar-izzle"
|
|
19
33
|
end
|
|
20
34
|
end
|
|
21
35
|
|
|
22
36
|
describe ".set_filename" do
|
|
23
37
|
before do
|
|
24
|
-
|
|
25
|
-
set_filename "foo-izzle"
|
|
26
|
-
end
|
|
38
|
+
Country.set_filename "foo-izzle"
|
|
27
39
|
|
|
28
40
|
class Bar < ActiveFile::Base
|
|
29
41
|
set_filename "bar-izzle"
|
|
@@ -31,16 +43,14 @@ describe ActiveFile::Base do
|
|
|
31
43
|
end
|
|
32
44
|
|
|
33
45
|
it "sets the filename on a per-subclass basis" do
|
|
34
|
-
|
|
46
|
+
Country.filename.should == "foo-izzle"
|
|
35
47
|
Bar.filename.should == "bar-izzle"
|
|
36
48
|
end
|
|
37
49
|
end
|
|
38
50
|
|
|
39
51
|
describe ".root_path=" do
|
|
40
52
|
before do
|
|
41
|
-
|
|
42
|
-
self.root_path = "foo-izzle"
|
|
43
|
-
end
|
|
53
|
+
Country.root_path = "foo-izzle"
|
|
44
54
|
|
|
45
55
|
class Bar < ActiveFile::Base
|
|
46
56
|
self.root_path = "bar-izzle"
|
|
@@ -48,16 +58,14 @@ describe ActiveFile::Base do
|
|
|
48
58
|
end
|
|
49
59
|
|
|
50
60
|
it "sets the root_path on a per-subclass basis" do
|
|
51
|
-
|
|
61
|
+
Country.root_path.should == "foo-izzle"
|
|
52
62
|
Bar.root_path.should == "bar-izzle"
|
|
53
63
|
end
|
|
54
64
|
end
|
|
55
65
|
|
|
56
66
|
describe ".set_root_path" do
|
|
57
67
|
before do
|
|
58
|
-
|
|
59
|
-
set_root_path "foo-izzle"
|
|
60
|
-
end
|
|
68
|
+
Country.set_root_path "foo-izzle"
|
|
61
69
|
|
|
62
70
|
class Bar < ActiveFile::Base
|
|
63
71
|
set_root_path "bar-izzle"
|
|
@@ -65,18 +73,18 @@ describe ActiveFile::Base do
|
|
|
65
73
|
end
|
|
66
74
|
|
|
67
75
|
it "sets the root_path on a per-subclass basis" do
|
|
68
|
-
|
|
76
|
+
Country.root_path.should == "foo-izzle"
|
|
69
77
|
Bar.root_path.should == "bar-izzle"
|
|
70
78
|
end
|
|
71
79
|
end
|
|
72
80
|
|
|
73
81
|
describe ".full_path" do
|
|
74
82
|
it "defaults to the directory of the calling file" do
|
|
75
|
-
class
|
|
83
|
+
class Country
|
|
76
84
|
def self.extension() "foo" end
|
|
77
85
|
end
|
|
78
86
|
|
|
79
|
-
|
|
87
|
+
Country.full_path.should == "#{Dir.pwd}/countries.foo"
|
|
80
88
|
end
|
|
81
89
|
end
|
|
82
90
|
|
|
@@ -87,7 +95,7 @@ describe ActiveFile::Base do
|
|
|
87
95
|
end
|
|
88
96
|
|
|
89
97
|
it "loads the data from the load_file method" do
|
|
90
|
-
class
|
|
98
|
+
class Country
|
|
91
99
|
class << self
|
|
92
100
|
def extension
|
|
93
101
|
"myfile"
|
|
@@ -99,53 +107,120 @@ describe ActiveFile::Base do
|
|
|
99
107
|
end
|
|
100
108
|
end
|
|
101
109
|
|
|
110
|
+
Country.reload_active_file = true
|
|
102
111
|
File.stub!(:mtime).and_return(1234)
|
|
103
112
|
MyClass.should_receive(:load_file).and_return([{:id => 1}, {:id => 2}, {:id => 3}])
|
|
104
113
|
|
|
105
|
-
records =
|
|
114
|
+
records = Country.all
|
|
106
115
|
records.length.should == 3
|
|
107
|
-
records.should =~ [
|
|
116
|
+
records.should =~ [Country.new(:id => 1), Country.new(:id => 2), Country.new(:id => 3)]
|
|
108
117
|
end
|
|
109
118
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
class
|
|
113
|
-
|
|
114
|
-
|
|
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
|
|
115
130
|
end
|
|
131
|
+
end
|
|
116
132
|
|
|
117
|
-
|
|
118
|
-
|
|
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
|
|
119
150
|
end
|
|
120
151
|
end
|
|
121
|
-
end
|
|
122
152
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
|
127
162
|
end
|
|
128
163
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
class
|
|
132
|
-
|
|
133
|
-
|
|
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
|
|
134
175
|
end
|
|
176
|
+
end
|
|
135
177
|
|
|
136
|
-
|
|
137
|
-
|
|
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
|
|
138
195
|
end
|
|
139
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
|
|
140
205
|
end
|
|
141
206
|
|
|
142
|
-
|
|
207
|
+
it "does not fetch data if data has been set" do
|
|
208
|
+
class Country < ActiveFile::Base
|
|
209
|
+
class << self
|
|
210
|
+
def extension
|
|
211
|
+
"myfile"
|
|
212
|
+
end
|
|
143
213
|
|
|
144
|
-
|
|
145
|
-
|
|
214
|
+
def load_file
|
|
215
|
+
MyClass.load_file(full_path)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
146
219
|
|
|
147
|
-
|
|
148
|
-
|
|
220
|
+
MyClass.should_not_receive(:load_file)
|
|
221
|
+
Country.data = [{:foo => :bar}]
|
|
222
|
+
Country.all
|
|
223
|
+
end
|
|
149
224
|
end
|
|
150
225
|
end
|
|
151
226
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: zilkey-active_hash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jeff Dean
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
12
|
|
|
13
|
-
date: 2009-07-
|
|
13
|
+
date: 2009-07-24 00:00:00 -07:00
|
|
14
14
|
default_executable:
|
|
15
15
|
dependencies:
|
|
16
16
|
- !ruby/object:Gem::Dependency
|