active_hash 0.9.3 → 0.9.4

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 CHANGED
@@ -1,3 +1,7 @@
1
+ 2011-06-05
2
+ - fixed deprecation warnings for class_inheritable_accessor (thanks scudco!)
3
+ - added basic compatibility with the `where` method from Arel (thanks rgarver!)
4
+
1
5
  2011-04-19
2
6
  - better dependency management and compatibility with ActiveSupport 2.x (thanks vandrijevik!)
3
7
 
data/README.md CHANGED
@@ -290,7 +290,7 @@ The two methods you need to implement are load_file, which needs to return an ar
290
290
  Setting the default file location in Rails:
291
291
 
292
292
  # config/initializers/active_file.rb
293
- ActiveFile.set_root_path "config/activefiles"
293
+ ActiveFile::Base.set_root_path "config/activefiles"
294
294
 
295
295
  In Rails, in development mode, it reloads the entire class, which reloads the file. In production, the data cached in memory.
296
296
 
@@ -1,7 +1,12 @@
1
1
  module ActiveFile
2
2
 
3
3
  class Base < ActiveHash::Base
4
- class_inheritable_accessor :filename, :root_path, :data_loaded
4
+
5
+ if respond_to?(:class_attribute)
6
+ class_attribute :filename, :root_path, :data_loaded
7
+ else
8
+ class_inheritable_accessor :filename, :root_path, :data_loaded
9
+ end
5
10
 
6
11
  class << self
7
12
 
@@ -10,6 +15,11 @@ module ActiveFile
10
15
  super
11
16
  end
12
17
 
18
+ def where(options)
19
+ reload unless data_loaded
20
+ super
21
+ end
22
+
13
23
  def delete_all
14
24
  self.data_loaded = true
15
25
  super
@@ -23,11 +33,11 @@ module ActiveFile
23
33
  end
24
34
 
25
35
  def set_filename(name)
26
- write_inheritable_attribute :filename, name
36
+ self.filename = name
27
37
  end
28
38
 
29
39
  def set_root_path(path)
30
- write_inheritable_attribute :root_path, path
40
+ self.root_path = path
31
41
  end
32
42
 
33
43
  def load_file
@@ -35,9 +45,9 @@ module ActiveFile
35
45
  end
36
46
 
37
47
  def full_path
38
- root_path = read_inheritable_attribute(:root_path) || Dir.pwd
39
- filename = read_inheritable_attribute(:filename) || name.tableize
40
- File.join(root_path, "#{filename}.#{extension}")
48
+ actual_root_path = root_path || Dir.pwd
49
+ actual_filename = filename || name.tableize
50
+ File.join(actual_root_path, "#{actual_filename}.#{extension}")
41
51
  end
42
52
 
43
53
  def extension
@@ -7,7 +7,12 @@ module ActiveHash
7
7
  end
8
8
 
9
9
  class Base
10
- class_inheritable_accessor :data, :dirty
10
+
11
+ if respond_to?(:class_attribute)
12
+ class_attribute :_data, :dirty
13
+ else
14
+ class_inheritable_accessor :_data, :dirty
15
+ end
11
16
 
12
17
  if Object.const_defined?(:ActiveModel)
13
18
  extend ActiveModel::Naming
@@ -29,10 +34,14 @@ module ActiveHash
29
34
  end
30
35
  end
31
36
 
37
+ def data
38
+ _data
39
+ end
40
+
32
41
  def data=(array_of_hashes)
33
42
  mark_dirty
34
43
  @records = nil
35
- write_inheritable_attribute(:data, array_of_hashes)
44
+ self._data = array_of_hashes
36
45
  if array_of_hashes
37
46
  auto_assign_fields(array_of_hashes)
38
47
  array_of_hashes.each do |hash|
@@ -74,14 +83,18 @@ module ActiveHash
74
83
 
75
84
  def all(options={})
76
85
  if options.has_key?(:conditions)
77
- (@records || []).select do |record|
78
- options[:conditions].all? {|col, match| record[col] == match}
79
- end
86
+ where(options[:conditions])
80
87
  else
81
88
  @records || []
82
89
  end
83
90
  end
84
91
 
92
+ def where(options)
93
+ (@records || []).select do |record|
94
+ options.all? {|col, match| record[col] == match}
95
+ end
96
+ end
97
+
85
98
  def count
86
99
  all.length
87
100
  end
@@ -272,7 +285,7 @@ module ActiveHash
272
285
  end
273
286
 
274
287
  def reload
275
- self.data = read_inheritable_attribute(:data)
288
+ self.data = _data
276
289
  mark_clean
277
290
  end
278
291
 
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "0.9.3"
3
+ VERSION = "0.9.4"
4
4
  end
5
5
  end
@@ -191,6 +191,53 @@ describe ActiveHash, "Base" do
191
191
  end
192
192
  end
193
193
 
194
+ describe ".where" do
195
+ before do
196
+ Country.field :name
197
+ Country.field :language
198
+ Country.data = [
199
+ {:id => 1, :name => "US", :language => 'English'},
200
+ {:id => 2, :name => "Canada", :language => 'English'},
201
+ {:id => 2, :name => "Mexico", :language => 'Spanish'}
202
+ ]
203
+ end
204
+
205
+ it "raises ArgumentError if no conditions are provided" do
206
+ lambda{
207
+ Country.where
208
+ }.should raise_error(ArgumentError)
209
+ end
210
+
211
+ it "returns all data as inflated objects" do
212
+ Country.where(:language => 'English').all? { |country| country.should be_kind_of(Country) }
213
+ end
214
+
215
+ it "populates the data correctly" do
216
+ records = Country.where(:language => 'English')
217
+ records.first.id.should == 1
218
+ records.first.name.should == "US"
219
+ records.last.id.should == 2
220
+ records.last.name.should == "Canada"
221
+ end
222
+
223
+ it "re-populates the records after data= is called" do
224
+ Country.data = [
225
+ {:id => 45, :name => "Canada"}
226
+ ]
227
+ records = Country.where(:name => 'Canada')
228
+ records.first.id.should == 45
229
+ records.first.name.should == "Canada"
230
+ records.length.should == 1
231
+ end
232
+
233
+ it "filters the records from a AR-like conditions hash" do
234
+ record = Country.where(:name => 'US')
235
+ record.count.should == 1
236
+ record.first.id.should == 1
237
+ record.first.name.should == 'US'
238
+ end
239
+ end
240
+
194
241
  describe ".count" do
195
242
  before do
196
243
  Country.data = [
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 61
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 9
9
- - 3
10
- version: 0.9.3
5
+ version: 0.9.4
11
6
  platform: ruby
12
7
  authors:
13
8
  - Jeff Dean
@@ -40,11 +35,6 @@ dependencies:
40
35
  requirements:
41
36
  - - ">="
42
37
  - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 2
46
- - 2
47
- - 2
48
38
  version: 2.2.2
49
39
  type: :runtime
50
40
  version_requirements: *id001
@@ -91,18 +81,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
81
  requirements:
92
82
  - - ">="
93
83
  - !ruby/object:Gem::Version
94
- hash: 3
95
- segments:
96
- - 0
97
84
  version: "0"
98
85
  required_rubygems_version: !ruby/object:Gem::Requirement
99
86
  none: false
100
87
  requirements:
101
88
  - - ">="
102
89
  - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
90
  version: "0"
107
91
  requirements: []
108
92