find_as_hashes 0.1.1 → 0.2.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/.gitignore +0 -1
- data/.travis.yml +5 -0
- data/Gemfile.lock +45 -0
- data/README.markdown +7 -8
- data/lib/find_as_hashes.rb +12 -8
- data/lib/find_as_hashes/version.rb +1 -1
- data/test/find_as_hashes_test.rb +78 -24
- data/test/fixtures/roles.yml +12 -0
- data/test/fixtures/users.yml +1 -0
- data/test/test_helper.rb +18 -3
- metadata +21 -18
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
find_as_hashes (0.2.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.1.0)
|
11
|
+
activesupport (= 3.1.0)
|
12
|
+
bcrypt-ruby (~> 3.0.0)
|
13
|
+
builder (~> 3.0.0)
|
14
|
+
i18n (~> 0.6)
|
15
|
+
activerecord (3.1.0)
|
16
|
+
activemodel (= 3.1.0)
|
17
|
+
activesupport (= 3.1.0)
|
18
|
+
arel (~> 2.2.1)
|
19
|
+
tzinfo (~> 0.3.29)
|
20
|
+
activesupport (3.1.0)
|
21
|
+
multi_json (~> 1.0)
|
22
|
+
ansi (1.3.0)
|
23
|
+
arel (2.2.1)
|
24
|
+
bcrypt-ruby (3.0.1)
|
25
|
+
builder (3.0.0)
|
26
|
+
i18n (0.6.0)
|
27
|
+
multi_json (1.0.3)
|
28
|
+
rake (0.9.2)
|
29
|
+
shoulda (2.11.3)
|
30
|
+
sqlite3 (1.3.4)
|
31
|
+
turn (0.8.2)
|
32
|
+
ansi (>= 1.2.2)
|
33
|
+
tzinfo (0.3.29)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
ruby
|
37
|
+
|
38
|
+
DEPENDENCIES
|
39
|
+
activesupport
|
40
|
+
bundler
|
41
|
+
find_as_hashes!
|
42
|
+
rake
|
43
|
+
shoulda
|
44
|
+
sqlite3
|
45
|
+
turn
|
data/README.markdown
CHANGED
@@ -5,10 +5,6 @@ of your ActiveRecord methods.
|
|
5
5
|
|
6
6
|
## Methods
|
7
7
|
|
8
|
-
Both methods assume that you've constructed some sort of ARel-style
|
9
|
-
query (e.g., regular queries like `Customer.where("balance" > 5000)` or
|
10
|
-
scopes like `User.active`).
|
11
|
-
|
12
8
|
The hashes that return are similar to the hash that returns with
|
13
9
|
[`ActiveRecord::Base#attributes`][attrs_method], except values are not
|
14
10
|
coerced (e.g., a tinyint column containing `1` will not be coerced into
|
@@ -16,10 +12,10 @@ coerced (e.g., a tinyint column containing `1` will not be coerced into
|
|
16
12
|
Ruby object. Booleans will return the underlying representation and
|
17
13
|
serialized objects will return as strings.
|
18
14
|
|
19
|
-
* `all_as_hashes`
|
20
|
-
|
21
|
-
* `first_as_hash`
|
22
|
-
|
15
|
+
* `all_as_hashes` works similarly to `all`, but returns the results as
|
16
|
+
an array of hashes.
|
17
|
+
* `first_as_hash` works similarly to `first`, but returns a hash of the
|
18
|
+
first matching record.
|
23
19
|
|
24
20
|
[attrs_method]:http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-i-attributes
|
25
21
|
|
@@ -44,4 +40,7 @@ In one example, looping through 36,400 User records and accessing an
|
|
44
40
|
attribute used 32% less memory and performed 17% quicker using
|
45
41
|
`all_as_hashes` over `all`.
|
46
42
|
|
43
|
+
## Tests
|
47
44
|
|
45
|
+
Run tests with `bundle exec rake test` (or just `rake test` if you're
|
46
|
+
daring).
|
data/lib/find_as_hashes.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
require "find_as_hashes/version"
|
2
|
+
require 'active_record'
|
2
3
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
module FindAsHashes
|
5
|
+
def all_as_hashes
|
6
|
+
# TODO figure out a less hacky way than where(nil) of retrieveing existing relation stack without modifying it
|
7
|
+
relation_stack = where(nil)
|
8
|
+
connection.select_all(relation_stack.joins(relation_stack.includes_values).to_sql)
|
9
|
+
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
def first_as_hash
|
12
|
+
relation_stack = limit(1)
|
13
|
+
connection.select_one(relation_stack.joins(relation_stack.includes_values).to_sql)
|
12
14
|
end
|
13
15
|
end
|
16
|
+
|
17
|
+
ActiveRecord::Base.extend FindAsHashes
|
data/test/find_as_hashes_test.rb
CHANGED
@@ -2,43 +2,97 @@ require File.dirname(__FILE__) + "/test_helper"
|
|
2
2
|
|
3
3
|
class FindAsHashesTest < ActiveRecord::TestCase
|
4
4
|
context "#all_as_hashes" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
context "on a relation" do
|
6
|
+
setup do
|
7
|
+
relation = User.where(:active => true)
|
8
|
+
@records = relation.all
|
9
|
+
@hashes = relation.all_as_hashes
|
10
|
+
end
|
11
|
+
|
12
|
+
should "return an array of attribute hashes" do
|
13
|
+
assert_equal Array, @hashes.class
|
14
|
+
assert @hashes.all? { |hash| hash.class == Hash }
|
15
|
+
end
|
16
|
+
|
17
|
+
should "return the same number of results as `all`" do
|
18
|
+
assert_equal @records.size, @hashes.size
|
19
|
+
end
|
10
20
|
|
11
|
-
|
12
|
-
|
13
|
-
|
21
|
+
should "return the same results as `all`" do
|
22
|
+
assert_equal @records.first.name_before_type_cast, @hashes.first["name"]
|
23
|
+
assert_equal @records.first.birthday_before_type_cast, @hashes.first["birthday"]
|
24
|
+
assert_equal @records.first.active_before_type_cast, @hashes.first["active"]
|
25
|
+
end
|
14
26
|
end
|
15
27
|
|
16
|
-
|
17
|
-
|
28
|
+
context "on a model" do
|
29
|
+
setup do
|
30
|
+
@records = User.all
|
31
|
+
@hashes = User.all_as_hashes
|
32
|
+
end
|
33
|
+
|
34
|
+
should "return the same results as `all`" do
|
35
|
+
assert_equal @records.first.name_before_type_cast, @hashes.first["name"]
|
36
|
+
assert_equal @records.first.birthday_before_type_cast, @hashes.first["birthday"]
|
37
|
+
assert_equal @records.first.active_before_type_cast, @hashes.first["active"]
|
38
|
+
end
|
18
39
|
end
|
19
40
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
41
|
+
context "using `includes`" do
|
42
|
+
setup do
|
43
|
+
end
|
44
|
+
|
45
|
+
should "use the included relation in the query" do
|
46
|
+
assert_nothing_raised do
|
47
|
+
@hashes = User.includes(:role).where("roles.admin" => true).all_as_hashes
|
48
|
+
assert Role.where(@hashes.first["role_id"]).first.admin?
|
49
|
+
end
|
50
|
+
end
|
24
51
|
end
|
25
52
|
end
|
26
53
|
|
27
54
|
context "#first_as_hash" do
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
55
|
+
context "on a relation" do
|
56
|
+
setup do
|
57
|
+
relation = User.where(:active => false)
|
58
|
+
@record = relation.first
|
59
|
+
@hash = relation.first_as_hash
|
60
|
+
end
|
61
|
+
|
62
|
+
should "return a hash of attributes" do
|
63
|
+
assert_equal Hash, @hash.class
|
64
|
+
end
|
65
|
+
|
66
|
+
should "return the same result as `first`" do
|
67
|
+
assert_equal @record.name_before_type_cast, @hash["name"]
|
68
|
+
assert_equal @record.birthday_before_type_cast, @hash["birthday"]
|
69
|
+
assert_equal @record.active_before_type_cast, @hash["active"]
|
70
|
+
end
|
32
71
|
end
|
33
72
|
|
34
|
-
|
35
|
-
|
73
|
+
context "on a model" do
|
74
|
+
setup do
|
75
|
+
@record = User.first
|
76
|
+
@hash = User.first_as_hash
|
77
|
+
end
|
78
|
+
|
79
|
+
should "return the same result as `first`" do
|
80
|
+
assert_equal @record.name_before_type_cast, @hash["name"]
|
81
|
+
assert_equal @record.birthday_before_type_cast, @hash["birthday"]
|
82
|
+
assert_equal @record.active_before_type_cast, @hash["active"]
|
83
|
+
end
|
36
84
|
end
|
37
85
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
86
|
+
context "using `includes`" do
|
87
|
+
setup do
|
88
|
+
end
|
89
|
+
|
90
|
+
should "use the included relation in the query" do
|
91
|
+
assert_nothing_raised do
|
92
|
+
@hash = User.includes(:role).where("roles.admin" => true).first_as_hash
|
93
|
+
assert Role.where(@hash["role_id"]).first.admin?
|
94
|
+
end
|
95
|
+
end
|
42
96
|
end
|
43
97
|
end
|
44
98
|
end
|
data/test/fixtures/users.yml
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
+
require 'find_as_hashes'
|
1
2
|
require 'test/unit'
|
2
3
|
require 'turn'
|
3
4
|
require 'shoulda'
|
4
|
-
require 'active_record'
|
5
5
|
require 'active_record/fixtures'
|
6
6
|
require 'sqlite3'
|
7
|
-
require 'find_as_hashes'
|
8
7
|
|
9
8
|
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
10
9
|
ActiveRecord::Schema.define do
|
@@ -12,10 +11,26 @@ ActiveRecord::Schema.define do
|
|
12
11
|
t.string :name
|
13
12
|
t.date :birthday
|
14
13
|
t.boolean :active
|
14
|
+
t.integer :role_id
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActiveRecord::Schema.define do
|
19
|
+
create_table :roles, :force => true do |t|
|
20
|
+
t.string :name
|
21
|
+
t.boolean :admin
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
18
25
|
class User < ActiveRecord::Base
|
26
|
+
belongs_to :role
|
27
|
+
end
|
28
|
+
|
29
|
+
class Role < ActiveRecord::Base
|
30
|
+
has_many :user
|
19
31
|
end
|
20
32
|
|
21
|
-
|
33
|
+
# Rails 3.0 has Fixtures, Rails 3.1 has ActiveRecord::Fixtures
|
34
|
+
fixtures_class = defined?(ActiveRecord::Fixtures) ? ActiveRecord::Fixtures : Fixtures
|
35
|
+
fixtures_class.create_fixtures "test/fixtures", :roles
|
36
|
+
fixtures_class.create_fixtures "test/fixtures", :users
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: find_as_hashes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &2151839880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2151839880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &2151839240 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2151839240
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &2151838320 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2151838320
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: turn
|
49
|
-
requirement: &
|
49
|
+
requirement: &2151837220 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2151837220
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: shoulda
|
60
|
-
requirement: &
|
60
|
+
requirement: &2151836140 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2151836140
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: activesupport
|
71
|
-
requirement: &
|
71
|
+
requirement: &2151834680 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2151834680
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: sqlite3
|
82
|
-
requirement: &
|
82
|
+
requirement: &2151833560 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *2151833560
|
91
91
|
description: Provides ActiveRecord methods to return results as attribute hashes rather
|
92
92
|
than instantiated ActiveRecord objects. Useful when working with very large sets
|
93
93
|
of results to improve performance.
|
@@ -100,12 +100,14 @@ files:
|
|
100
100
|
- .gitignore
|
101
101
|
- .travis.yml
|
102
102
|
- Gemfile
|
103
|
+
- Gemfile.lock
|
103
104
|
- README.markdown
|
104
105
|
- Rakefile
|
105
106
|
- find_as_hashes.gemspec
|
106
107
|
- lib/find_as_hashes.rb
|
107
108
|
- lib/find_as_hashes/version.rb
|
108
109
|
- test/find_as_hashes_test.rb
|
110
|
+
- test/fixtures/roles.yml
|
109
111
|
- test/fixtures/users.yml
|
110
112
|
- test/test_helper.rb
|
111
113
|
homepage: https://github.com/tstmedia/find_as_hashes
|
@@ -122,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
124
|
version: '0'
|
123
125
|
segments:
|
124
126
|
- 0
|
125
|
-
hash:
|
127
|
+
hash: -1576352960134467685
|
126
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
129
|
none: false
|
128
130
|
requirements:
|
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
133
|
version: '0'
|
132
134
|
segments:
|
133
135
|
- 0
|
134
|
-
hash:
|
136
|
+
hash: -1576352960134467685
|
135
137
|
requirements: []
|
136
138
|
rubyforge_project:
|
137
139
|
rubygems_version: 1.8.6
|
@@ -141,5 +143,6 @@ summary: Provides ActiveRecord methods to return results as attribute hashes rat
|
|
141
143
|
than instantiated ActiveRecord objects.
|
142
144
|
test_files:
|
143
145
|
- test/find_as_hashes_test.rb
|
146
|
+
- test/fixtures/roles.yml
|
144
147
|
- test/fixtures/users.yml
|
145
148
|
- test/test_helper.rb
|