zeal 0.0.1

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 ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in zeal.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 by Academia, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ Zeal: eager loading (but not too eager) for ActiveRecord collections
2
+ ====================================================================
3
+
4
+ Sometimes you need to eager load some associations, but it's not
5
+ convenient to do so at the same time as loading the original records.
6
+ Zeal is a simple gem that offers a couple of different ways to trigger
7
+ ActiveRecord's built-in eager loading on an array of already-retrieved
8
+ records.
9
+
10
+ ### For Example
11
+
12
+ Let's say you want to do the equivalent of this:
13
+
14
+ ``` ruby
15
+ User.find(:all, :limit => 10, :include => {:posts => :comments})
16
+ ```
17
+
18
+ If you're reducing code duplication by loading Users in a before filter
19
+ that's shared between multiple actions, it may not make sense to eager
20
+ load the same associations for all of those pages. Instead, you can do
21
+ this:
22
+
23
+ ``` ruby
24
+ # before_filters.rb
25
+ @users = User.find(:all, :limit => 10)
26
+
27
+ # users_controller.rb
28
+ def users_and_posts_and_stuff
29
+ @users.eager_load(:posts => :comments)
30
+ end
31
+
32
+ def users_and_friends_and_stuff
33
+ @users.eager_load(:friends, :countrymen)
34
+ end
35
+ ```
36
+
37
+ You can now avoid both N+1 and unnecessary preloading, while keeping
38
+ your code as DRY as possible.
39
+
40
+ ### Usage
41
+
42
+ There are two ways to use Zeal: the nicer, more intrusive way, and the
43
+ more explicit, less intrusive way.
44
+
45
+ ``` ruby
46
+ # more_intrusive.rb
47
+ class Array
48
+ include Zeal
49
+ end
50
+
51
+ @users.eager_load(:friends, :countrymen)
52
+
53
+ # less_intrusive.rb
54
+ Zeal.eager_load(@users, :friends, :countrymen)
55
+
56
+ # or alternately
57
+ @users.extend(Zeal).eager_load(@users, :friends, :countrymen)
58
+ ```
59
+
60
+ Your choice!
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/zeal.rb ADDED
@@ -0,0 +1,39 @@
1
+ require "zeal/version"
2
+
3
+ module Zeal
4
+ class EagerLoadError < RuntimeError; end
5
+
6
+ class << self
7
+ if Rails.version.to_s =~ /^3/
8
+ def eager_load(collection, *args)
9
+ ActiveRecord::Associations::Preloader.new(collection, args).run
10
+ collection
11
+ end
12
+ else
13
+ def eager_load(collection, *args)
14
+ class_of_collection(collection).send(:preload_associations, collection, args)
15
+ collection
16
+ end
17
+ end
18
+
19
+ private
20
+ def class_of_collection(collection)
21
+ klasses = collection.map(&:class).uniq
22
+ if klasses.count > 1
23
+ raise EagerLoadError, "Collection includes objects of multiple classes: #{klasses.inspect}"
24
+ end
25
+
26
+ klass = klasses.first
27
+ unless klass < ActiveRecord::Base
28
+ raise EagerLoadError, "#{klass} isn't an ActiveRecord class."
29
+ end
30
+
31
+ klass
32
+ end
33
+ end
34
+
35
+ def eager_load(*args)
36
+ Zeal.eager_load(self, *args)
37
+ self
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Zeal
2
+ VERSION = "0.0.1"
3
+ end
data/zeal.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "zeal/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "zeal"
7
+ s.version = Zeal::VERSION
8
+ s.authors = ["Ryan Fitzgerald"]
9
+ s.email = ["rfitz@academia.edu"]
10
+ s.homepage = ""
11
+ s.summary = %q{eager loading (but not too eager) for ActiveRecord collections}
12
+ s.description = %q{Zeal allows you to eager-load associations on ActiveRecord objects that have already been loaded from the database.}
13
+
14
+ s.rubyforge_project = "zeal"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zeal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Fitzgerald
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Zeal allows you to eager-load associations on ActiveRecord objects that
15
+ have already been loaded from the database.
16
+ email:
17
+ - rfitz@academia.edu
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/zeal.rb
28
+ - lib/zeal/version.rb
29
+ - zeal.gemspec
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project: zeal
50
+ rubygems_version: 1.8.10
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: eager loading (but not too eager) for ActiveRecord collections
54
+ test_files: []
55
+ has_rdoc: