find_as_hashes 0.1.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 +4 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/README.markdown +47 -0
- data/Rakefile +11 -0
- data/find_as_hashes.gemspec +27 -0
- data/lib/find_as_hashes.rb +13 -0
- data/lib/find_as_hashes/version.rb +3 -0
- data/test/find_as_hashes_test.rb +44 -0
- data/test/fixtures/users.yml +6 -0
- data/test/test_helper.rb +21 -0
- metadata +145 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
Adds methods to return attribute hashes rather than instantiated
|
2
|
+
ActiveRecord objects. This is useful when working with large sets of
|
3
|
+
records and you need the performance more than you need the convenience
|
4
|
+
of your ActiveRecord methods.
|
5
|
+
|
6
|
+
## Methods
|
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
|
+
The hashes that return are similar to the hash that returns with
|
13
|
+
[`ActiveRecord::Base#attributes`][attrs_method], except values are not
|
14
|
+
coerced (e.g., a tinyint column containing `1` will not be coerced into
|
15
|
+
`true`). Strings, dates, and numbers will return as their appropriate
|
16
|
+
Ruby object. Booleans will return the underlying representation and
|
17
|
+
serialized objects will return as strings.
|
18
|
+
|
19
|
+
* `all_as_hashes` performs the search that you've set up and returns the
|
20
|
+
results as an array of hashes.
|
21
|
+
* `first_as_hash` performs the search that you've set up and returns a
|
22
|
+
hash of the first matching record.
|
23
|
+
|
24
|
+
[attrs_method]:http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-i-attributes
|
25
|
+
|
26
|
+
## Examples
|
27
|
+
|
28
|
+
> User.where(:email => nil).all_as_hashes
|
29
|
+
=> [
|
30
|
+
{:id => 123, :name => "Joe User", :email => nil},
|
31
|
+
{:id => 456, :name => "Jane User", :email => nil}
|
32
|
+
]
|
33
|
+
|
34
|
+
> User.where(:email => nil).first_as_hash
|
35
|
+
=> {:id => 123, :name => "Joe User", :email => nil}
|
36
|
+
|
37
|
+
## Effects
|
38
|
+
|
39
|
+
These methods are not for everyday use, but for when speed of execution
|
40
|
+
is more important than convenience, like heavy-duty reporting or
|
41
|
+
processing lots of existing records.
|
42
|
+
|
43
|
+
In one example, looping through 36,400 User records and accessing an
|
44
|
+
attribute used 32% less memory and performed 17% quicker using
|
45
|
+
`all_as_hashes` over `all`.
|
46
|
+
|
47
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "find_as_hashes/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "find_as_hashes"
|
7
|
+
s.version = FindAsHashes::VERSION
|
8
|
+
s.authors = ["Patrick Byrne"]
|
9
|
+
s.email = ["patrick.byrne@tstmedia.com"]
|
10
|
+
s.homepage = "https://github.com/pbyrne/find_as_hashes"
|
11
|
+
s.summary = %q{Provides ActiveRecord methods to return results as attribute hashes rather than instantiated ActiveRecord objects.}
|
12
|
+
s.description = %q{Provides ActiveRecord methods to return results as attribute hashes rather than instantiated ActiveRecord objects. Useful when working with very large sets of results to improve performance.}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_runtime_dependency "activerecord"
|
20
|
+
|
21
|
+
s.add_development_dependency "bundler"
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "turn"
|
24
|
+
s.add_development_dependency "shoulda"
|
25
|
+
s.add_development_dependency "activesupport"
|
26
|
+
s.add_development_dependency "sqlite3"
|
27
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/test_helper"
|
2
|
+
|
3
|
+
class FindAsHashesTest < ActiveRecord::TestCase
|
4
|
+
context "#all_as_hashes" do
|
5
|
+
setup do
|
6
|
+
relation = User.where(:active => true)
|
7
|
+
@records = relation.all
|
8
|
+
@hashes = relation.all_as_hashes
|
9
|
+
end
|
10
|
+
|
11
|
+
should "return an array of attribute hashes" do
|
12
|
+
assert_equal Array, @hashes.class
|
13
|
+
assert @hashes.all? { |hash| hash.class == Hash }
|
14
|
+
end
|
15
|
+
|
16
|
+
should "return the same number of results as `all`" do
|
17
|
+
assert_equal @records.size, @hashes.size
|
18
|
+
end
|
19
|
+
|
20
|
+
should "return the same results as `all`" do
|
21
|
+
assert_equal @records.first.name_before_type_cast, @hashes.first["name"]
|
22
|
+
assert_equal @records.first.birthday_before_type_cast, @hashes.first["birthday"]
|
23
|
+
assert_equal @records.first.active_before_type_cast, @hashes.first["active"]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "#first_as_hash" do
|
28
|
+
setup do
|
29
|
+
relation = User.where(:active => false)
|
30
|
+
@record = relation.first
|
31
|
+
@hash = relation.first_as_hash
|
32
|
+
end
|
33
|
+
|
34
|
+
should "return a hash of attributes" do
|
35
|
+
assert_equal Hash, @hash.class
|
36
|
+
end
|
37
|
+
|
38
|
+
should "return the same result as `first`" do
|
39
|
+
assert_equal @record.name_before_type_cast, @hash["name"]
|
40
|
+
assert_equal @record.birthday_before_type_cast, @hash["birthday"]
|
41
|
+
assert_equal @record.active_before_type_cast, @hash["active"]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'turn'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'active_record'
|
5
|
+
require 'active_record/fixtures'
|
6
|
+
require 'sqlite3'
|
7
|
+
require 'find_as_hashes'
|
8
|
+
|
9
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
10
|
+
ActiveRecord::Schema.define do
|
11
|
+
create_table :users, :force => true do |t|
|
12
|
+
t.string :name
|
13
|
+
t.date :birthday
|
14
|
+
t.boolean :active
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
end
|
20
|
+
|
21
|
+
Fixtures.create_fixtures "test/fixtures", :users
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: find_as_hashes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Patrick Byrne
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-14 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &2153703340 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2153703340
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bundler
|
27
|
+
requirement: &2153702700 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2153702700
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &2153702120 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2153702120
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: turn
|
49
|
+
requirement: &2153701380 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2153701380
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: shoulda
|
60
|
+
requirement: &2153700760 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *2153700760
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: &2153699800 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *2153699800
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: sqlite3
|
82
|
+
requirement: &2153699080 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *2153699080
|
91
|
+
description: Provides ActiveRecord methods to return results as attribute hashes rather
|
92
|
+
than instantiated ActiveRecord objects. Useful when working with very large sets
|
93
|
+
of results to improve performance.
|
94
|
+
email:
|
95
|
+
- patrick.byrne@tstmedia.com
|
96
|
+
executables: []
|
97
|
+
extensions: []
|
98
|
+
extra_rdoc_files: []
|
99
|
+
files:
|
100
|
+
- .gitignore
|
101
|
+
- .travis.yml
|
102
|
+
- Gemfile
|
103
|
+
- README.markdown
|
104
|
+
- Rakefile
|
105
|
+
- find_as_hashes.gemspec
|
106
|
+
- lib/find_as_hashes.rb
|
107
|
+
- lib/find_as_hashes/version.rb
|
108
|
+
- test/find_as_hashes_test.rb
|
109
|
+
- test/fixtures/users.yml
|
110
|
+
- test/test_helper.rb
|
111
|
+
homepage: https://github.com/pbyrne/find_as_hashes
|
112
|
+
licenses: []
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ! '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
hash: -2470055047066117578
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
hash: -2470055047066117578
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.6
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Provides ActiveRecord methods to return results as attribute hashes rather
|
141
|
+
than instantiated ActiveRecord objects.
|
142
|
+
test_files:
|
143
|
+
- test/find_as_hashes_test.rb
|
144
|
+
- test/fixtures/users.yml
|
145
|
+
- test/test_helper.rb
|