active_hash 0.9.4 → 0.9.5
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/Gemfile +14 -2
- data/README.md +49 -0
- data/active_hash.gemspec +2 -1
- data/lib/active_hash/base.rb +14 -14
- data/lib/active_hash/version.rb +1 -1
- data/spec/active_hash/base_spec.rb +17 -0
- metadata +22 -5
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
|
@@ -3,8 +3,20 @@ source :gemcutter
|
|
|
3
3
|
gemspec
|
|
4
4
|
|
|
5
5
|
group :development do
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
activerecord_version = ENV['ACTIVE_HASH_ACTIVERECORD_VERSION']
|
|
7
|
+
|
|
8
|
+
if activerecord_version == "edge"
|
|
9
|
+
git "git://github.com/rails/rails.git" do
|
|
10
|
+
gem "activerecord"
|
|
11
|
+
gem "activesupport"
|
|
12
|
+
end
|
|
13
|
+
elsif activerecord_version && activerecord_version.strip != ""
|
|
14
|
+
gem "activerecord", activerecord_version
|
|
15
|
+
else
|
|
16
|
+
gem "activerecord"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
gem "rake", "0.8.7"
|
|
8
20
|
gem "rspec", "2.2.0"
|
|
9
21
|
gem "sqlite3-ruby", ">= 1.3.2"
|
|
10
22
|
end
|
data/README.md
CHANGED
|
@@ -325,6 +325,55 @@ Constants are formed by first stripping all non-word characters and then upcasin
|
|
|
325
325
|
|
|
326
326
|
The field specified as the _enum_accessor_ must contain unique data values.
|
|
327
327
|
|
|
328
|
+
## Contributing
|
|
329
|
+
|
|
330
|
+
If you'd like to become an ActiveHash contributor, the easiest way it to fork this repo, make your changes, run the specs and submit a pull request once they pass.
|
|
331
|
+
|
|
332
|
+
To run specs, run:
|
|
333
|
+
|
|
334
|
+
bundle install
|
|
335
|
+
bundle exec rspec spec
|
|
336
|
+
|
|
337
|
+
If your changes seem reasonable and the specs pass I'll give you commit rights to this repo and add you to the list of people who can push the gem.
|
|
338
|
+
|
|
339
|
+
## Releasing a new version
|
|
340
|
+
|
|
341
|
+
To make users' lives easier, please maintain support for:
|
|
342
|
+
|
|
343
|
+
* Ruby 1.8.7
|
|
344
|
+
* Ruby Enterprise 1.8.7
|
|
345
|
+
* Ruby 1.92
|
|
346
|
+
* ActiveRecord/ActiveSupport from 2.3.2 through edge
|
|
347
|
+
|
|
348
|
+
To that end, there is a prerelease script that will run the tests against those 3 rubies, and against multiple versions of ActiveRecord/ActiveSupport.
|
|
349
|
+
Before releasing a new version of ActiveHash, please run the prelease shell script like so:
|
|
350
|
+
|
|
351
|
+
./prerelease
|
|
352
|
+
|
|
353
|
+
It requires you to have rvm installed, and it requires the latest patch-versions of 1.8.7, ree and 1.9.2. The prerelease script will:
|
|
354
|
+
|
|
355
|
+
* switch to rvm's ruby-1.8.7
|
|
356
|
+
* check for an active_hash gemset, and create one if it's not there
|
|
357
|
+
* check for bundler and install it if it's not there
|
|
358
|
+
* run `bundle exec rspec spec` against AR versions 2.3.2, 2.3.5, 2.3.11, the currently released version and edge
|
|
359
|
+
* switch to ree-1.8.7 and do the same
|
|
360
|
+
* switch to ruby-1.9.2 and do the same
|
|
361
|
+
|
|
362
|
+
Needless to say, this script takes some time to run. If you have to update your rubies, the first time you run this might take 45 minutes,
|
|
363
|
+
but it will save users lots of headaches, and save me from dealing with the bug reports :)
|
|
364
|
+
|
|
365
|
+
Once `prerelease` passes, follow these steps to release a new version of active_hash:
|
|
366
|
+
|
|
367
|
+
* update the changelog with a brief summary of the changes that are included in the release
|
|
368
|
+
* bump the gem version by editing the `version.rb` file
|
|
369
|
+
* if there are new contributors, add them to the list of authors in the Rakefile
|
|
370
|
+
* run `rake build`
|
|
371
|
+
* commit those changes
|
|
372
|
+
* run `rake install` and verify that the gem loads correctly from an irb session
|
|
373
|
+
* run `rake release`, which will rebuild the gem, tag it, push the tags (and your latest commit) to github, then push the gem to rubygems.org
|
|
374
|
+
|
|
375
|
+
If you have any questions about how to maintain backwards compatibility, please email me and we can figure it out.
|
|
376
|
+
|
|
328
377
|
## Copyright
|
|
329
378
|
|
|
330
379
|
Copyright (c) 2010 Jeff Dean. See LICENSE for details.
|
data/active_hash.gemspec
CHANGED
data/lib/active_hash/base.rb
CHANGED
|
@@ -91,7 +91,7 @@ module ActiveHash
|
|
|
91
91
|
|
|
92
92
|
def where(options)
|
|
93
93
|
(@records || []).select do |record|
|
|
94
|
-
options.all? {|col, match| record[col] == match}
|
|
94
|
+
options.all? { |col, match| record[col] == match }
|
|
95
95
|
end
|
|
96
96
|
end
|
|
97
97
|
|
|
@@ -118,7 +118,7 @@ module ActiveHash
|
|
|
118
118
|
when :all
|
|
119
119
|
all
|
|
120
120
|
when Array
|
|
121
|
-
all.select { |record| id.
|
|
121
|
+
all.select { |record| id.to_s.include?(record.id.to_s) }
|
|
122
122
|
else
|
|
123
123
|
find_by_id(id) || begin
|
|
124
124
|
raise RecordNotFound.new("Couldn't find #{name} with ID=#{id}")
|
|
@@ -127,7 +127,7 @@ module ActiveHash
|
|
|
127
127
|
end
|
|
128
128
|
|
|
129
129
|
def find_by_id(id)
|
|
130
|
-
all.detect { |record| record.id == id.
|
|
130
|
+
all.detect { |record| record.id.to_s == id.to_s }
|
|
131
131
|
end
|
|
132
132
|
|
|
133
133
|
delegate :first, :last, :to => :all
|
|
@@ -180,7 +180,7 @@ module ActiveHash
|
|
|
180
180
|
else
|
|
181
181
|
result = matches.first
|
|
182
182
|
if config[:bang?]
|
|
183
|
-
result || raise(RecordNotFound, "Couldn\'t find #{name} with #{attribute_pairs.collect {|pair| "#{pair[0]} = #{pair[1]}"}.join(', ')}")
|
|
183
|
+
result || raise(RecordNotFound, "Couldn\'t find #{name} with #{attribute_pairs.collect { |pair| "#{pair[0]} = #{pair[1]}" }.join(', ')}")
|
|
184
184
|
else
|
|
185
185
|
result
|
|
186
186
|
end
|
|
@@ -190,8 +190,8 @@ module ActiveHash
|
|
|
190
190
|
def configuration_for_custom_finder(finder_name)
|
|
191
191
|
if finder_name.to_s.match(/^find_(all_)?by_(.*?)(!)?$/) && !($1 && $3)
|
|
192
192
|
{
|
|
193
|
-
:all?
|
|
194
|
-
:bang?
|
|
193
|
+
:all? => !!$1,
|
|
194
|
+
:bang? => !!$3,
|
|
195
195
|
:fields => $2.split('_and_')
|
|
196
196
|
}
|
|
197
197
|
end
|
|
@@ -304,13 +304,13 @@ module ActiveHash
|
|
|
304
304
|
private :mark_clean
|
|
305
305
|
|
|
306
306
|
def has_instance_method?(name)
|
|
307
|
-
instance_methods.map{|method| method.to_sym}.include?(name)
|
|
307
|
+
instance_methods.map { |method| method.to_sym }.include?(name)
|
|
308
308
|
end
|
|
309
309
|
|
|
310
310
|
private :has_instance_method?
|
|
311
311
|
|
|
312
312
|
def has_singleton_method?(name)
|
|
313
|
-
singleton_methods.map{|method| method.to_sym}.include?(name)
|
|
313
|
+
singleton_methods.map { |method| method.to_sym }.include?(name)
|
|
314
314
|
end
|
|
315
315
|
|
|
316
316
|
private :has_singleton_method?
|
|
@@ -373,12 +373,12 @@ module ActiveHash
|
|
|
373
373
|
|
|
374
374
|
def cache_key
|
|
375
375
|
case
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
376
|
+
when new_record?
|
|
377
|
+
"#{self.class.model_name.cache_key}/new"
|
|
378
|
+
when timestamp = self[:updated_at]
|
|
379
|
+
"#{self.class.model_name.cache_key}/#{id}-#{timestamp.to_s(:number)}"
|
|
380
|
+
else
|
|
381
|
+
"#{self.class.model_name.cache_key}/#{id}"
|
|
382
382
|
end
|
|
383
383
|
end
|
|
384
384
|
|
data/lib/active_hash/version.rb
CHANGED
|
@@ -341,6 +341,23 @@ describe ActiveHash, "Base" do
|
|
|
341
341
|
it "finds the record with the specified id" do
|
|
342
342
|
Country.find_by_id(2).id.should == 2
|
|
343
343
|
end
|
|
344
|
+
|
|
345
|
+
it "finds the record with the specified id as a string" do
|
|
346
|
+
Country.find_by_id("2").id.should == 2
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
context "with string ids" do
|
|
351
|
+
before do
|
|
352
|
+
Country.data = [
|
|
353
|
+
{:id => "abc", :name => "US"},
|
|
354
|
+
{:id => "def", :name => "Canada"}
|
|
355
|
+
]
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
it "finds the record with the specified id" do
|
|
359
|
+
Country.find_by_id("abc").id.should == "abc"
|
|
360
|
+
end
|
|
344
361
|
end
|
|
345
362
|
|
|
346
363
|
context "with nil" do
|
metadata
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_hash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 49
|
|
4
5
|
prerelease:
|
|
5
|
-
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 9
|
|
9
|
+
- 5
|
|
10
|
+
version: 0.9.5
|
|
6
11
|
platform: ruby
|
|
7
12
|
authors:
|
|
8
13
|
- Jeff Dean
|
|
@@ -20,6 +25,7 @@ authors:
|
|
|
20
25
|
- Joel Chippindale
|
|
21
26
|
- Kevin Olsen
|
|
22
27
|
- Vladimir Andrijevik
|
|
28
|
+
- Adam Anderson
|
|
23
29
|
autorequire:
|
|
24
30
|
bindir: bin
|
|
25
31
|
cert_chain: []
|
|
@@ -28,16 +34,21 @@ date: 2011-01-22 00:00:00 -07:00
|
|
|
28
34
|
default_executable:
|
|
29
35
|
dependencies:
|
|
30
36
|
- !ruby/object:Gem::Dependency
|
|
31
|
-
|
|
32
|
-
prerelease: false
|
|
33
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
|
37
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
|
34
38
|
none: false
|
|
35
39
|
requirements:
|
|
36
40
|
- - ">="
|
|
37
41
|
- !ruby/object:Gem::Version
|
|
42
|
+
hash: 3
|
|
43
|
+
segments:
|
|
44
|
+
- 2
|
|
45
|
+
- 2
|
|
46
|
+
- 2
|
|
38
47
|
version: 2.2.2
|
|
48
|
+
requirement: *id001
|
|
49
|
+
prerelease: false
|
|
39
50
|
type: :runtime
|
|
40
|
-
|
|
51
|
+
name: activesupport
|
|
41
52
|
description:
|
|
42
53
|
email: jeff@zilkey.com
|
|
43
54
|
executables: []
|
|
@@ -81,12 +92,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
81
92
|
requirements:
|
|
82
93
|
- - ">="
|
|
83
94
|
- !ruby/object:Gem::Version
|
|
95
|
+
hash: 3
|
|
96
|
+
segments:
|
|
97
|
+
- 0
|
|
84
98
|
version: "0"
|
|
85
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
100
|
none: false
|
|
87
101
|
requirements:
|
|
88
102
|
- - ">="
|
|
89
103
|
- !ruby/object:Gem::Version
|
|
104
|
+
hash: 3
|
|
105
|
+
segments:
|
|
106
|
+
- 0
|
|
90
107
|
version: "0"
|
|
91
108
|
requirements: []
|
|
92
109
|
|