active_hash 0.8.0 → 0.8.1
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 +3 -0
- data/README.md +1 -0
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/active_hash.gemspec +5 -6
- data/lib/active_file/base.rb +3 -1
- data/lib/active_hash/base.rb +18 -1
- data/spec/active_file/base_spec.rb +48 -0
- data/spec/active_hash/base_spec.rb +30 -0
- metadata +4 -3
data/CHANGELOG
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
2010-05-04
|
2
|
+
- When calling ActiveFile::Base.reload do not actually perform the reload if nothing has been modified unless you call reload(true) to force (Michael Schubert)
|
3
|
+
|
1
4
|
2010-04-25
|
2
5
|
- When ActiveRecord model belongs_to an ActiveHash and the associated id is nil, returns nil instead of raising RecordNotFound (Jeremy Weiskotten)
|
3
6
|
- Merged Nakajima's "add" alias for "create" - gotta save those ASCII characters :)
|
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ begin
|
|
8
8
|
gem.summary = %Q{An ActiveRecord-like model that uses a hash or file as a datasource}
|
9
9
|
gem.email = "jeff@zilkey.com"
|
10
10
|
gem.homepage = "http://github.com/zilkey/active_hash"
|
11
|
-
gem.authors = ["Jeff Dean", "Mike Dalessio", "Corey Innis", "Peter Jaros", "Brandon Keene", "Brian Takita", "Pat Nakajima", "John Pignata", "Jeremy Weiskotten"]
|
11
|
+
gem.authors = ["Jeff Dean", "Mike Dalessio", "Corey Innis", "Peter Jaros", "Brandon Keene", "Brian Takita", "Pat Nakajima", "John Pignata", "Michael Schubert", "Jeremy Weiskotten"]
|
12
12
|
gem.add_dependency('activesupport', [">= 2.2.2"])
|
13
13
|
gem.post_install_message = ""
|
14
14
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.1
|
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
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
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.8.
|
8
|
+
s.version = "0.8.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Jeff Dean", "Mike Dalessio", "Corey Innis", "Peter Jaros", "Brandon Keene", "Brian Takita", "Pat Nakajima", "John Pignata", "Jeremy Weiskotten"]
|
12
|
-
s.date = %q{2010-04
|
11
|
+
s.authors = ["Jeff Dean", "Mike Dalessio", "Corey Innis", "Peter Jaros", "Brandon Keene", "Brian Takita", "Pat Nakajima", "John Pignata", "Michael Schubert", "Jeremy Weiskotten"]
|
12
|
+
s.date = %q{2010-05-04}
|
13
13
|
s.email = %q{jeff@zilkey.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
@@ -71,4 +71,3 @@ Gem::Specification.new do |s|
|
|
71
71
|
s.add_dependency(%q<activesupport>, [">= 2.2.2"])
|
72
72
|
end
|
73
73
|
end
|
74
|
-
|
data/lib/active_file/base.rb
CHANGED
data/lib/active_hash/base.rb
CHANGED
@@ -4,11 +4,12 @@ module ActiveHash
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class Base
|
7
|
-
class_inheritable_accessor :data
|
7
|
+
class_inheritable_accessor :data, :dirty
|
8
8
|
class << self
|
9
9
|
attr_reader :field_names
|
10
10
|
|
11
11
|
def data=(array_of_hashes)
|
12
|
+
mark_dirty
|
12
13
|
@records = nil
|
13
14
|
write_inheritable_attribute(:data, array_of_hashes)
|
14
15
|
if array_of_hashes
|
@@ -22,6 +23,7 @@ module ActiveHash
|
|
22
23
|
def insert(record)
|
23
24
|
@records ||= []
|
24
25
|
record.attributes[:id] ||= next_id
|
26
|
+
mark_dirty
|
25
27
|
@records << record
|
26
28
|
end
|
27
29
|
|
@@ -37,6 +39,7 @@ module ActiveHash
|
|
37
39
|
def create(attributes = {})
|
38
40
|
record = new(attributes)
|
39
41
|
record.save
|
42
|
+
mark_dirty
|
40
43
|
record
|
41
44
|
end
|
42
45
|
alias_method :add, :create
|
@@ -62,6 +65,7 @@ module ActiveHash
|
|
62
65
|
end
|
63
66
|
|
64
67
|
def delete_all
|
68
|
+
mark_dirty
|
65
69
|
@records = []
|
66
70
|
end
|
67
71
|
|
@@ -217,10 +221,23 @@ module ActiveHash
|
|
217
221
|
|
218
222
|
def reload
|
219
223
|
self.data = read_inheritable_attribute(:data)
|
224
|
+
mark_clean
|
220
225
|
end
|
221
226
|
|
222
227
|
private :reload
|
223
228
|
|
229
|
+
def mark_dirty
|
230
|
+
self.dirty = true
|
231
|
+
end
|
232
|
+
|
233
|
+
private :mark_dirty
|
234
|
+
|
235
|
+
def mark_clean
|
236
|
+
self.dirty = false
|
237
|
+
end
|
238
|
+
|
239
|
+
private :mark_clean
|
240
|
+
|
224
241
|
end
|
225
242
|
|
226
243
|
attr_reader :attributes
|
@@ -80,4 +80,52 @@ describe ActiveFile::Base do
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
+
describe ".reload" do
|
84
|
+
before do
|
85
|
+
class Country
|
86
|
+
def self.load_file()
|
87
|
+
{"new_york"=>{"name"=>"New York", "id"=>1}}.values
|
88
|
+
end
|
89
|
+
end
|
90
|
+
Country.reload # initial load
|
91
|
+
end
|
92
|
+
|
93
|
+
context "when nothing has been modified" do
|
94
|
+
it "does not reload anything" do
|
95
|
+
class Country
|
96
|
+
def self.load_file()
|
97
|
+
raise "should not have been called"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
Country.dirty.should be_false
|
101
|
+
Country.reload
|
102
|
+
Country.dirty.should be_false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "when forced" do
|
107
|
+
it "reloads the data" do
|
108
|
+
class Country
|
109
|
+
def self.load_file()
|
110
|
+
{"new_york"=>{"name"=>"New York", "id"=>2}}.values
|
111
|
+
end
|
112
|
+
end
|
113
|
+
Country.dirty.should be_false
|
114
|
+
Country.find_by_id(2).should be_nil
|
115
|
+
Country.reload(true)
|
116
|
+
Country.dirty.should be_false
|
117
|
+
Country.find(2).name.should == "New York"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context "when the data has been modified" do
|
122
|
+
it "reloads the data" do
|
123
|
+
Country.create!
|
124
|
+
Country.dirty.should be_true
|
125
|
+
Country.reload
|
126
|
+
Country.dirty.should be_false
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
83
131
|
end
|
@@ -66,6 +66,12 @@ describe ActiveHash, "Base" do
|
|
66
66
|
Country.data.should == [{:name => "US", :id => 1}, {:name => "Canada", :id => 2}]
|
67
67
|
Region.data.should == [{:description => "A big region", :id => 1}, {:description => "A remote region", :id => 2}]
|
68
68
|
end
|
69
|
+
|
70
|
+
it "marks the class as dirty" do
|
71
|
+
Country.dirty.should be_false
|
72
|
+
Country.data = []
|
73
|
+
Country.dirty.should be_true
|
74
|
+
end
|
69
75
|
end
|
70
76
|
|
71
77
|
describe ".add" do
|
@@ -79,6 +85,12 @@ describe ActiveHash, "Base" do
|
|
79
85
|
}.should change { Country.count }
|
80
86
|
end
|
81
87
|
|
88
|
+
it "marks the class as dirty" do
|
89
|
+
Country.dirty.should be_false
|
90
|
+
Country.add :name => "Russia"
|
91
|
+
Country.dirty.should be_true
|
92
|
+
end
|
93
|
+
|
82
94
|
it "returns the record" do
|
83
95
|
record = Country.add :name => "Russia"
|
84
96
|
record.name.should == "Russia"
|
@@ -620,6 +632,12 @@ describe ActiveHash, "Base" do
|
|
620
632
|
Country.all.should == [country]
|
621
633
|
end
|
622
634
|
|
635
|
+
it "marks the class as dirty" do
|
636
|
+
Country.dirty.should be_false
|
637
|
+
Country.new(:id => 1, :name => "foo").save
|
638
|
+
Country.dirty.should be_true
|
639
|
+
end
|
640
|
+
|
623
641
|
end
|
624
642
|
|
625
643
|
describe ".create" do
|
@@ -676,6 +694,12 @@ describe ActiveHash, "Base" do
|
|
676
694
|
Country.all.should == [country]
|
677
695
|
end
|
678
696
|
|
697
|
+
it "marks the class as dirty" do
|
698
|
+
Country.dirty.should be_false
|
699
|
+
Country.create! :id => 1, :name => "foo"
|
700
|
+
Country.dirty.should be_true
|
701
|
+
end
|
702
|
+
|
679
703
|
end
|
680
704
|
|
681
705
|
describe "#valid?" do
|
@@ -742,6 +766,12 @@ describe ActiveHash, "Base" do
|
|
742
766
|
Country.all.should be_empty
|
743
767
|
end
|
744
768
|
|
769
|
+
it "marks the class as dirty" do
|
770
|
+
Country.dirty.should be_false
|
771
|
+
Country.delete_all
|
772
|
+
Country.dirty.should be_true
|
773
|
+
end
|
774
|
+
|
745
775
|
end
|
746
776
|
|
747
777
|
describe "using with Fixjour" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 8
|
8
|
-
-
|
9
|
-
version: 0.8.
|
8
|
+
- 1
|
9
|
+
version: 0.8.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jeff Dean
|
@@ -17,12 +17,13 @@ authors:
|
|
17
17
|
- Brian Takita
|
18
18
|
- Pat Nakajima
|
19
19
|
- John Pignata
|
20
|
+
- Michael Schubert
|
20
21
|
- Jeremy Weiskotten
|
21
22
|
autorequire:
|
22
23
|
bindir: bin
|
23
24
|
cert_chain: []
|
24
25
|
|
25
|
-
date: 2010-04
|
26
|
+
date: 2010-05-04 00:00:00 +00:00
|
26
27
|
default_executable:
|
27
28
|
dependencies:
|
28
29
|
- !ruby/object:Gem::Dependency
|