find_as_hashes 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +12 -6
- data/README.markdown +38 -0
- data/find_as_hashes.gemspec +2 -0
- data/lib/active_resource/find_as_hashes.rb +37 -0
- data/lib/find_as_hashes/version.rb +1 -1
- data/test/active_resource_test.rb +61 -0
- metadata +65 -30
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
find_as_hashes (0.
|
4
|
+
find_as_hashes (0.4.0)
|
5
5
|
activerecord
|
6
6
|
|
7
7
|
GEM
|
@@ -17,27 +17,33 @@ GEM
|
|
17
17
|
activesupport (= 3.1.0)
|
18
18
|
arel (~> 2.2.1)
|
19
19
|
tzinfo (~> 0.3.29)
|
20
|
+
activeresource (3.1.0)
|
21
|
+
activemodel (= 3.1.0)
|
22
|
+
activesupport (= 3.1.0)
|
20
23
|
activesupport (3.1.0)
|
21
24
|
multi_json (~> 1.0)
|
22
|
-
ansi (1.3
|
23
|
-
arel (2.2.
|
25
|
+
ansi (1.4.3)
|
26
|
+
arel (2.2.3)
|
24
27
|
bcrypt-ruby (3.0.1)
|
25
|
-
builder (3.0.
|
26
|
-
|
28
|
+
builder (3.0.4)
|
29
|
+
fakeweb (1.3.0)
|
30
|
+
i18n (0.6.1)
|
27
31
|
multi_json (1.0.3)
|
28
32
|
rake (0.9.2)
|
29
33
|
shoulda (2.11.3)
|
30
34
|
sqlite3 (1.3.4)
|
31
35
|
turn (0.8.2)
|
32
36
|
ansi (>= 1.2.2)
|
33
|
-
tzinfo (0.3.
|
37
|
+
tzinfo (0.3.35)
|
34
38
|
|
35
39
|
PLATFORMS
|
36
40
|
ruby
|
37
41
|
|
38
42
|
DEPENDENCIES
|
43
|
+
activeresource
|
39
44
|
activesupport
|
40
45
|
bundler
|
46
|
+
fakeweb
|
41
47
|
find_as_hashes!
|
42
48
|
rake
|
43
49
|
shoulda
|
data/README.markdown
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
[![TravisCI](https://secure.travis-ci.org/tstmedia/find_as_hashes.png "TravisCI")](http://travis-ci.org/tstmedia/find_as_hashes "Travis-CI FindAsHashes")
|
2
2
|
|
3
|
+
# ActiveRecord
|
4
|
+
|
3
5
|
Adds methods to return attribute hashes rather than instantiated
|
4
6
|
ActiveRecord objects. This is useful when working with large sets of
|
5
7
|
records and you need the performance more than you need the convenience
|
@@ -42,6 +44,42 @@ In one example, looping through 36,400 User records and accessing an
|
|
42
44
|
attribute used 32% less memory and performed 17% quicker using
|
43
45
|
`all_as_hashes` over `all`.
|
44
46
|
|
47
|
+
# ActiveResource
|
48
|
+
|
49
|
+
The equivalent methods are available in ActiveResource as well, providing
|
50
|
+
similar benefits.
|
51
|
+
|
52
|
+
## Methods
|
53
|
+
|
54
|
+
The hashes that return are essentially the xml or json data hashified.
|
55
|
+
|
56
|
+
* `all_as_hashes` works similarly to `all`, but returns the results as
|
57
|
+
an array of hashes.
|
58
|
+
* `find_as_hashes` works similarly to `find`, but returns the results as
|
59
|
+
an array of hashes.
|
60
|
+
* `first_as_hash` works similarly to `first`, but returns a hash of the
|
61
|
+
first matching record.
|
62
|
+
* `last_as_hash` works similarly to `last`, but returns a hash of the
|
63
|
+
last matching record.
|
64
|
+
|
65
|
+
## Usage
|
66
|
+
|
67
|
+
To enable the use of these methods, simply add this line to the top of your
|
68
|
+
ActiveResource class.
|
69
|
+
|
70
|
+
`require 'active_resource/find_as_hashes'`
|
71
|
+
|
72
|
+
## Examples
|
73
|
+
|
74
|
+
> User.all_as_hashes(:active => 1)
|
75
|
+
=> [
|
76
|
+
{:id => 123, :name => "Joe User", :active => 1,},
|
77
|
+
{:id => 456, :name => "Jane User", :active => 1}
|
78
|
+
]
|
79
|
+
|
80
|
+
> User.first_as_hash(:active => 1)
|
81
|
+
=> {:id => 123, :name => "Joe User", :active => 1}
|
82
|
+
|
45
83
|
## Tests
|
46
84
|
|
47
85
|
Run tests with `bundle exec rake test` (or just `rake test` if you're
|
data/find_as_hashes.gemspec
CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.add_development_dependency "rake"
|
23
23
|
s.add_development_dependency "turn"
|
24
24
|
s.add_development_dependency "shoulda"
|
25
|
+
s.add_development_dependency "fakeweb"
|
25
26
|
s.add_development_dependency "activesupport"
|
26
27
|
s.add_development_dependency "sqlite3"
|
28
|
+
s.add_development_dependency "activeresource"
|
27
29
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "find_as_hashes/version"
|
2
|
+
require 'active_resource'
|
3
|
+
|
4
|
+
module ActiveResource
|
5
|
+
class Base
|
6
|
+
class << self
|
7
|
+
def find_as_hashes(*args)
|
8
|
+
@skip_instantiate = true
|
9
|
+
find(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def all_as_hashes(*args)
|
13
|
+
find_as_hashes(:all, *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def first_as_hash(*args)
|
17
|
+
find_as_hashes(:first, *args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def last_as_hash(*args)
|
21
|
+
find_as_hashes(:last, *args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def instantiate_collection_with_check(collection, prefix_options = {})
|
25
|
+
if @skip_instantiate
|
26
|
+
@skip_instantiate = nil
|
27
|
+
collection
|
28
|
+
else
|
29
|
+
instantiate_collection_without_check(collection, prefix_options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
alias_method :instantiate_collection_without_check, :instantiate_collection
|
34
|
+
alias_method :instantiate_collection, :instantiate_collection_with_check
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/test_helper"
|
2
|
+
require 'fakeweb'
|
3
|
+
require 'active_resource/find_as_hashes'
|
4
|
+
|
5
|
+
module Resource
|
6
|
+
class User < ActiveResource::Base
|
7
|
+
self.site = 'http://api.test.com/'
|
8
|
+
self.format = :json
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ActiveResourceTest < ActiveSupport::TestCase
|
13
|
+
def setup
|
14
|
+
FakeWeb.allow_net_connect = false
|
15
|
+
FakeWeb.register_uri(:get, "http://api.test.com/users.json", :body => User.all.to_json, :status => ["200", "OK"])
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
FakeWeb.allow_net_connect = true
|
20
|
+
FakeWeb.clean_registry
|
21
|
+
end
|
22
|
+
|
23
|
+
context "#all_as_hashes" do
|
24
|
+
setup do
|
25
|
+
@records = Resource::User.all
|
26
|
+
@hashes = Resource::User.all_as_hashes
|
27
|
+
end
|
28
|
+
|
29
|
+
should "return an array of attribute hashes" do
|
30
|
+
assert_equal Array, @hashes.class
|
31
|
+
assert @hashes.all? { |hash| hash.class == Hash }
|
32
|
+
assert @records.all? { |record| record.class != Hash }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "#first_as_hash" do
|
37
|
+
setup do
|
38
|
+
@record = Resource::User.first
|
39
|
+
@hash = Resource::User.first_as_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
should "return a single hash" do
|
43
|
+
assert_equal Hash, @hash.class
|
44
|
+
assert_equal Resource::User, @record.class
|
45
|
+
assert_equal @hash["user"]["name"], User.first.name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "#last_as_hash" do
|
50
|
+
setup do
|
51
|
+
@record = Resource::User.last
|
52
|
+
@hash = Resource::User.last_as_hash
|
53
|
+
end
|
54
|
+
|
55
|
+
should "return a single hash" do
|
56
|
+
assert_equal Hash, @hash.class
|
57
|
+
assert_equal Resource::User, @record.class
|
58
|
+
assert_equal @hash["user"]["name"], User.last.name
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,128 +1,160 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: find_as_hashes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.0
|
5
4
|
prerelease:
|
5
|
+
version: 0.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Patrick Byrne
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
none: false
|
21
|
+
prerelease: false
|
15
22
|
name: activerecord
|
16
23
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
24
|
requirements:
|
19
25
|
- - ! '>='
|
20
26
|
- !ruby/object:Gem::Version
|
21
27
|
version: '0'
|
28
|
+
none: false
|
22
29
|
type: :runtime
|
23
|
-
|
30
|
+
- !ruby/object:Gem::Dependency
|
24
31
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
32
|
requirements:
|
27
33
|
- - ! '>='
|
28
34
|
- !ruby/object:Gem::Version
|
29
35
|
version: '0'
|
30
|
-
|
36
|
+
none: false
|
37
|
+
prerelease: false
|
31
38
|
name: bundler
|
32
39
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
40
|
requirements:
|
35
41
|
- - ! '>='
|
36
42
|
- !ruby/object:Gem::Version
|
37
43
|
version: '0'
|
44
|
+
none: false
|
38
45
|
type: :development
|
39
|
-
|
46
|
+
- !ruby/object:Gem::Dependency
|
40
47
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
48
|
requirements:
|
43
49
|
- - ! '>='
|
44
50
|
- !ruby/object:Gem::Version
|
45
51
|
version: '0'
|
46
|
-
|
52
|
+
none: false
|
53
|
+
prerelease: false
|
47
54
|
name: rake
|
48
55
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
56
|
requirements:
|
51
57
|
- - ! '>='
|
52
58
|
- !ruby/object:Gem::Version
|
53
59
|
version: '0'
|
60
|
+
none: false
|
54
61
|
type: :development
|
55
|
-
|
62
|
+
- !ruby/object:Gem::Dependency
|
56
63
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
64
|
requirements:
|
59
65
|
- - ! '>='
|
60
66
|
- !ruby/object:Gem::Version
|
61
67
|
version: '0'
|
62
|
-
|
68
|
+
none: false
|
69
|
+
prerelease: false
|
63
70
|
name: turn
|
64
71
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
73
|
- - ! '>='
|
68
74
|
- !ruby/object:Gem::Version
|
69
75
|
version: '0'
|
76
|
+
none: false
|
70
77
|
type: :development
|
71
|
-
|
78
|
+
- !ruby/object:Gem::Dependency
|
72
79
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
80
|
requirements:
|
75
81
|
- - ! '>='
|
76
82
|
- !ruby/object:Gem::Version
|
77
83
|
version: '0'
|
78
|
-
|
84
|
+
none: false
|
85
|
+
prerelease: false
|
79
86
|
name: shoulda
|
80
87
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
88
|
requirements:
|
83
89
|
- - ! '>='
|
84
90
|
- !ruby/object:Gem::Version
|
85
91
|
version: '0'
|
92
|
+
none: false
|
86
93
|
type: :development
|
87
|
-
|
94
|
+
- !ruby/object:Gem::Dependency
|
88
95
|
version_requirements: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
89
100
|
none: false
|
101
|
+
prerelease: false
|
102
|
+
name: fakeweb
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
90
104
|
requirements:
|
91
105
|
- - ! '>='
|
92
106
|
- !ruby/object:Gem::Version
|
93
107
|
version: '0'
|
108
|
+
none: false
|
109
|
+
type: :development
|
94
110
|
- !ruby/object:Gem::Dependency
|
111
|
+
version_requirements: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
none: false
|
117
|
+
prerelease: false
|
95
118
|
name: activesupport
|
96
119
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
120
|
requirements:
|
99
121
|
- - ! '>='
|
100
122
|
- !ruby/object:Gem::Version
|
101
123
|
version: '0'
|
124
|
+
none: false
|
102
125
|
type: :development
|
103
|
-
|
126
|
+
- !ruby/object:Gem::Dependency
|
104
127
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
128
|
requirements:
|
107
129
|
- - ! '>='
|
108
130
|
- !ruby/object:Gem::Version
|
109
131
|
version: '0'
|
110
|
-
|
132
|
+
none: false
|
133
|
+
prerelease: false
|
111
134
|
name: sqlite3
|
112
135
|
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
136
|
requirements:
|
115
137
|
- - ! '>='
|
116
138
|
- !ruby/object:Gem::Version
|
117
139
|
version: '0'
|
140
|
+
none: false
|
118
141
|
type: :development
|
119
|
-
|
142
|
+
- !ruby/object:Gem::Dependency
|
120
143
|
version_requirements: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
121
148
|
none: false
|
149
|
+
prerelease: false
|
150
|
+
name: activeresource
|
151
|
+
requirement: !ruby/object:Gem::Requirement
|
122
152
|
requirements:
|
123
153
|
- - ! '>='
|
124
154
|
- !ruby/object:Gem::Version
|
125
155
|
version: '0'
|
156
|
+
none: false
|
157
|
+
type: :development
|
126
158
|
description: Provides ActiveRecord methods to return results as attribute hashes rather
|
127
159
|
than instantiated ActiveRecord objects. Useful when working with very large sets
|
128
160
|
of results to improve performance.
|
@@ -139,8 +171,10 @@ files:
|
|
139
171
|
- README.markdown
|
140
172
|
- Rakefile
|
141
173
|
- find_as_hashes.gemspec
|
174
|
+
- lib/active_resource/find_as_hashes.rb
|
142
175
|
- lib/find_as_hashes.rb
|
143
176
|
- lib/find_as_hashes/version.rb
|
177
|
+
- test/active_resource_test.rb
|
144
178
|
- test/find_as_hashes_test.rb
|
145
179
|
- test/fixtures/roles.yml
|
146
180
|
- test/fixtures/users.yml
|
@@ -152,23 +186,23 @@ rdoc_options: []
|
|
152
186
|
require_paths:
|
153
187
|
- lib
|
154
188
|
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
-
none: false
|
156
189
|
requirements:
|
157
190
|
- - ! '>='
|
158
191
|
- !ruby/object:Gem::Version
|
159
192
|
version: '0'
|
160
193
|
segments:
|
161
194
|
- 0
|
162
|
-
hash: -
|
163
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
|
+
hash: -2382583336894726517
|
164
196
|
none: false
|
197
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
198
|
requirements:
|
166
199
|
- - ! '>='
|
167
200
|
- !ruby/object:Gem::Version
|
168
201
|
version: '0'
|
169
202
|
segments:
|
170
203
|
- 0
|
171
|
-
hash: -
|
204
|
+
hash: -2382583336894726517
|
205
|
+
none: false
|
172
206
|
requirements: []
|
173
207
|
rubyforge_project:
|
174
208
|
rubygems_version: 1.8.24
|
@@ -177,6 +211,7 @@ specification_version: 3
|
|
177
211
|
summary: Provides ActiveRecord methods to return results as attribute hashes rather
|
178
212
|
than instantiated ActiveRecord objects.
|
179
213
|
test_files:
|
214
|
+
- test/active_resource_test.rb
|
180
215
|
- test/find_as_hashes_test.rb
|
181
216
|
- test/fixtures/roles.yml
|
182
217
|
- test/fixtures/users.yml
|