hash_extension 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/lib/hash_extension.rb +85 -0
- data/lib/hash_extension/version.rb +1 -1
- data/website/index.html +26 -6
- data/website/index.txt +20 -4
- metadata +2 -2
data/History.txt
CHANGED
data/lib/hash_extension.rb
CHANGED
@@ -7,4 +7,89 @@ class Hash
|
|
7
7
|
self[method_string]
|
8
8
|
end
|
9
9
|
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ActiveRecord
|
13
|
+
class Base
|
14
|
+
class << self
|
15
|
+
|
16
|
+
def find_by_sql_as_hashes(sql)
|
17
|
+
connection.select_all(sanitize_sql(sql))
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_as_hashes(*args)
|
21
|
+
options = extract_options_from_args!(args)
|
22
|
+
if options.include?(:include)
|
23
|
+
raise ActiveRecord::StatementInvalid, "find_as_hashes cannot accept :include options!"
|
24
|
+
end
|
25
|
+
validate_find_options(options)
|
26
|
+
set_readonly_option!(options)
|
27
|
+
|
28
|
+
case args.first
|
29
|
+
when :first
|
30
|
+
find_initial_as_hashes(options)
|
31
|
+
when :all
|
32
|
+
find_every_as_hashes(options)
|
33
|
+
else
|
34
|
+
find_from_ids_as_hashes(args, options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def find_every_as_hashes(options)
|
41
|
+
connection.select_all(construct_finder_sql(options))
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_initial_as_hashes(options)
|
45
|
+
find_every_as_hashes(options.merge(:limit => 1))
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def find_from_ids_as_hashes(ids, options)
|
50
|
+
expects_array = ids.first.kind_of?(Array)
|
51
|
+
return ids.first if expects_array && ids.first.empty?
|
52
|
+
|
53
|
+
ids = ids.flatten.compact.uniq
|
54
|
+
|
55
|
+
case ids.size
|
56
|
+
when 0
|
57
|
+
raise RecordNotFound, "Couldn't find #{name} without an ID"
|
58
|
+
when 1
|
59
|
+
result = find_one_as_hash(ids.first, options)
|
60
|
+
expects_array ? [ result ] : result
|
61
|
+
else
|
62
|
+
find_some_as_hashes(ids, options)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def find_one_as_hash(id, options)
|
67
|
+
conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
|
68
|
+
options.update :conditions => "#{table_name}.#{connection.quote_column_name(primary_key)} = #{quote_value(id,columns_hash[primary_key])}#{conditions}"
|
69
|
+
|
70
|
+
# Use find_every(options).first since the primary key condition
|
71
|
+
# already ensures we have a single record. Using find_initial adds
|
72
|
+
# a superfluous :limit => 1.
|
73
|
+
if result = find_every_as_hashes(options).first
|
74
|
+
result
|
75
|
+
else
|
76
|
+
raise ActiveRecord::RecordNotFound, "Couldn't find #{name} with ID=#{id}#{conditions}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def find_some_as_hashes(ids, options)
|
81
|
+
conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
|
82
|
+
ids_list = ids.map { |id| quote_value(id,columns_hash[primary_key]) }.join(',')
|
83
|
+
options.update :conditions => "#{table_name}.#{connection.quote_column_name(primary_key)} IN (#{ids_list})#{conditions}"
|
84
|
+
|
85
|
+
result = find_every_as_hashes(options)
|
86
|
+
|
87
|
+
if result.size == ids.size
|
88
|
+
result
|
89
|
+
else
|
90
|
+
raise RecordNotFound, "Couldn't find all #{name.pluralize} with IDs (#{ids_list})#{conditions}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
10
95
|
end
|
data/website/index.html
CHANGED
@@ -33,12 +33,12 @@
|
|
33
33
|
<h1>hash_extension</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/hash_extension"; return false'>
|
35
35
|
<p>Get Version</p>
|
36
|
-
<a href="http://rubyforge.org/projects/hash_extension" class="numbers">0.0.
|
36
|
+
<a href="http://rubyforge.org/projects/hash_extension" class="numbers">0.0.2</a>
|
37
37
|
</div>
|
38
38
|
<h2>What</h2>
|
39
39
|
|
40
40
|
|
41
|
-
<p>hash_extension is a Ruby on Rails plug-in that makes hashes more object-like. When you request a lot of objects with an ActiveRecord query, there’s a lot of overhead in creating
|
41
|
+
<p>hash_extension is a Ruby on Rails plug-in that does two things. First, it provides ActiveRecord finder methods that return hashes. Second, it makes hashes more object-like. Why? When you request a lot of objects with an ActiveRecord query, there’s a lot of overhead in creating the objects that come back. If you’re not modifying the records, but just displaying them or performing local calculations before display, you don’t need to suffer the overhead of creating heavy ActiveRecord objects. It turns out it’s much faster to get back hash objects. This plug-in makes it a snap to replace a query that returns ActiveRecord objects with one that returns hashes.</p>
|
42
42
|
|
43
43
|
|
44
44
|
<h2>Installing</h2>
|
@@ -50,7 +50,7 @@
|
|
50
50
|
<h2>The basics</h2>
|
51
51
|
|
52
52
|
|
53
|
-
<p>The hash_extension plug-in allows you to treat hashes like objects. E.g,. instead of:</p>
|
53
|
+
<p>The hash_extension plug-in allows you to treat hashes like objects, and to get back hashes from (almost) standard ActiveRecord queries. E.g,. instead of:</p>
|
54
54
|
|
55
55
|
|
56
56
|
<p><pre class='syntax'><span class="ident">hash</span><span class="punct">['</span><span class="string">foo</span><span class="punct">']</span></pre></p>
|
@@ -74,6 +74,17 @@
|
|
74
74
|
<p><pre class='syntax'><span class="ident">hash</span><span class="punct">['</span><span class="string">foo</span><span class="punct">']</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">bar</span><span class="punct">'</span></pre></p>
|
75
75
|
|
76
76
|
|
77
|
+
<p>Two variants of basic ActiveRecord query types are supported—find and find_by_sql, inventively recast as find_as_hashes and find_by_sql_as_hashes. E.g.,</p>
|
78
|
+
|
79
|
+
|
80
|
+
<p><pre class='syntax'>
|
81
|
+
<span class="constant">Foo</span><span class="punct">.</span><span class="ident">find_as_hashes</span><span class="punct">(</span><span class="symbol">:all</span><span class="punct">)</span>
|
82
|
+
<span class="constant">Foo</span><span class="punct">.</span><span class="ident">find_as_hashes</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">)</span>
|
83
|
+
<span class="constant">Foo</span><span class="punct">.</span><span class="ident">find_as_hashes</span><span class="punct">(</span><span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:conditions</span> <span class="punct">=></span> <span class="punct">"</span><span class="string">bar = 'baz'</span><span class="punct">")</span>
|
84
|
+
<span class="constant">Foo</span><span class="punct">.</span><span class="ident">find_by_sql_as_hashes</span><span class="punct">("</span><span class="string">select * where fubar = 'some_complex_stuff'</span><span class="punct">")</span>
|
85
|
+
</pre></p>
|
86
|
+
|
87
|
+
|
77
88
|
<p>The trunk repository is <code>svn://rubyforge.org/var/svn/hash_extension/trunk</code> for anonymous access.</p>
|
78
89
|
|
79
90
|
|
@@ -95,10 +106,10 @@
|
|
95
106
|
<p>You can replace it with this:</p>
|
96
107
|
|
97
108
|
|
98
|
-
<p><pre class='syntax'><span class="constant">MyObject</span><span class="punct">.</span><span class="ident">
|
109
|
+
<p><pre class='syntax'><span class="constant">MyObject</span><span class="punct">.</span><span class="ident">find_as_hashes</span><span class="punct">("</span><span class="string">select * from my_objects</span><span class="punct">")</span></pre></p>
|
99
110
|
|
100
111
|
|
101
|
-
<p>The same data is returned, but without the overhead of creating large, complex ActiveRecord objects. The rest of your code will continue to behave as expected, because the object-like access will now work
|
112
|
+
<p>The same data is returned, but without the overhead of creating large, complex ActiveRecord objects. The rest of your code will continue to behave as expected, because the object-like access will now work correctly with the hashes returned by these methods.</p>
|
102
113
|
|
103
114
|
|
104
115
|
<h2>License</h2>
|
@@ -107,12 +118,21 @@
|
|
107
118
|
<p>This code is free to use under the terms of the <span class="caps">MIT</span> license.</p>
|
108
119
|
|
109
120
|
|
121
|
+
<h2>Credits.</h2>
|
122
|
+
|
123
|
+
|
124
|
+
<p>The initial release only provided the extension to hash that made them act more like objects. I posed the challenge: “If someone would like to contribute extensions to ActiveRecord that would create finders that automatically return hashes, e.g., “find_as_hashes(:all)”, I’d be thrilled to include it as part of this plug-in.” Elliot Laster rose to the challenge and provided the ActiveRecord extension available in the 0.0.2 release.</p>
|
125
|
+
|
126
|
+
|
127
|
+
<p>The new call to action is to provide some unit tests! Ideally, they should use migrations to set up dummy tables, test against them, then tear down those tables. Anyone up for it? :-)</p>
|
128
|
+
|
129
|
+
|
110
130
|
<h2>Contact</h2>
|
111
131
|
|
112
132
|
|
113
133
|
<p>Comments are welcome. Send an email to <a href="mailto:dan@chak.org">Dan Chak</a>.</p>
|
114
134
|
<p class="coda">
|
115
|
-
<a href="FIXME email">FIXME full name</a>,
|
135
|
+
<a href="FIXME email">FIXME full name</a>, 9th February 2008<br>
|
116
136
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
117
137
|
</p>
|
118
138
|
</div>
|
data/website/index.txt
CHANGED
@@ -2,7 +2,7 @@ h1. hash_extension
|
|
2
2
|
|
3
3
|
h2. What
|
4
4
|
|
5
|
-
hash_extension is a Ruby on Rails plug-in that makes hashes more object-like. When you request a lot of objects with an ActiveRecord query, there's a lot of overhead in creating
|
5
|
+
hash_extension is a Ruby on Rails plug-in that does two things. First, it provides ActiveRecord finder methods that return hashes. Second, it makes hashes more object-like. Why? When you request a lot of objects with an ActiveRecord query, there's a lot of overhead in creating the objects that come back. If you're not modifying the records, but just displaying them or performing local calculations before display, you don't need to suffer the overhead of creating heavy ActiveRecord objects. It turns out it's much faster to get back hash objects. This plug-in makes it a snap to replace a query that returns ActiveRecord objects with one that returns hashes.
|
6
6
|
|
7
7
|
h2. Installing
|
8
8
|
|
@@ -10,7 +10,7 @@ h2. Installing
|
|
10
10
|
|
11
11
|
h2. The basics
|
12
12
|
|
13
|
-
The hash_extension plug-in allows you to treat hashes like objects. E.g,. instead of:
|
13
|
+
The hash_extension plug-in allows you to treat hashes like objects, and to get back hashes from (almost) standard ActiveRecord queries. E.g,. instead of:
|
14
14
|
|
15
15
|
<pre syntax="ruby">hash['foo']</pre>
|
16
16
|
|
@@ -26,6 +26,16 @@ you can say:
|
|
26
26
|
|
27
27
|
<pre syntax="ruby">hash['foo'] = 'bar'</pre>
|
28
28
|
|
29
|
+
Two variants of basic ActiveRecord query types are supported -- find and find_by_sql, inventively recast as find_as_hashes and find_by_sql_as_hashes. E.g.,
|
30
|
+
|
31
|
+
<pre syntax="ruby">
|
32
|
+
Foo.find_as_hashes(:all)
|
33
|
+
Foo.find_as_hashes(:first)
|
34
|
+
Foo.find_as_hashes(:all, :conditions => "bar = 'baz'")
|
35
|
+
Foo.find_by_sql_as_hashes("select * where fubar = 'some_complex_stuff'")
|
36
|
+
</pre>
|
37
|
+
|
38
|
+
|
29
39
|
The trunk repository is <code>svn://rubyforge.org/var/svn/hash_extension/trunk</code> for anonymous access.
|
30
40
|
|
31
41
|
h2. From Rails
|
@@ -40,14 +50,20 @@ Anywhere you have syntax like this:
|
|
40
50
|
|
41
51
|
You can replace it with this:
|
42
52
|
|
43
|
-
<pre syntax="ruby">MyObject.
|
53
|
+
<pre syntax="ruby">MyObject.find_as_hashes("select * from my_objects")</pre>
|
44
54
|
|
45
|
-
The same data is returned, but without the overhead of creating large, complex ActiveRecord objects. The rest of your code will continue to behave as expected, because the object-like access will now work
|
55
|
+
The same data is returned, but without the overhead of creating large, complex ActiveRecord objects. The rest of your code will continue to behave as expected, because the object-like access will now work correctly with the hashes returned by these methods.
|
46
56
|
|
47
57
|
h2. License
|
48
58
|
|
49
59
|
This code is free to use under the terms of the MIT license.
|
50
60
|
|
61
|
+
h2. Credits.
|
62
|
+
|
63
|
+
The initial release only provided the extension to hash that made them act more like objects. I posed the challenge: "If someone would like to contribute extensions to ActiveRecord that would create finders that automatically return hashes, e.g., "find_as_hashes(:all)", I'd be thrilled to include it as part of this plug-in." Elliot Laster rose to the challenge and provided the ActiveRecord extension available in the 0.0.2 release.
|
64
|
+
|
65
|
+
The new call to action is to provide some unit tests! Ideally, they should use migrations to set up dummy tables, test against them, then tear down those tables. Anyone up for it? :-)
|
66
|
+
|
51
67
|
h2. Contact
|
52
68
|
|
53
69
|
Comments are welcome. Send an email to "Dan Chak":mailto:dan@chak.org.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_extension
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Chak
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-02-09 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|