activerecord-lazy-attributes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +34 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/activerecord-lazy-attributes.gemspec +64 -0
- data/init.rb +1 -0
- data/lib/lazy_attributes.rb +54 -0
- data/rails/init.rb +1 -0
- data/spec/lazy_attributes_spec.rb +26 -0
- data/spec/models/image.rb +3 -0
- data/spec/schema.rb +8 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +12 -0
- metadata +92 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Moritz Heidkamp
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
= activerecord-lazy-attributes
|
2
|
+
|
3
|
+
This ActiveRecord extension allows to define attributes to be
|
4
|
+
lazy-loaded. It's main purpose is to avoid loading large columns (such
|
5
|
+
as BLOBs) with every SELECT.
|
6
|
+
|
7
|
+
== Example
|
8
|
+
|
9
|
+
class Image < ActiveRecord::Base
|
10
|
+
attr_lazy :data
|
11
|
+
end
|
12
|
+
|
13
|
+
i = Image.first # this selects all columns except "data"
|
14
|
+
i.data # "data" column is selected separately
|
15
|
+
|
16
|
+
== Credit
|
17
|
+
|
18
|
+
This is based on
|
19
|
+
http://refactormycode.com/codes/219-activerecord-lazy-attribute-loading-plugin-for-rails
|
20
|
+
(the code here is the last contribution to this refactoring).
|
21
|
+
|
22
|
+
== Note on Patches/Pull Requests
|
23
|
+
|
24
|
+
* Fork the project.
|
25
|
+
* Make your feature addition or bug fix.
|
26
|
+
* Add tests for it. This is important so I don't break it in a
|
27
|
+
future version unintentionally.
|
28
|
+
* Commit, do not mess with rakefile, version, or history.
|
29
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
30
|
+
* Send me a pull request. Bonus points for topic branches.
|
31
|
+
|
32
|
+
== Copyright
|
33
|
+
|
34
|
+
Copyright (c) 2010 Moritz Heidkamp. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "activerecord-lazy-attributes"
|
8
|
+
gem.summary = %Q{An ActiveRecord extension which allows attributes to be lazy-loaded}
|
9
|
+
gem.description = %Q{A useful ActiveRecord extension to handle columns containing large amounts of data}
|
10
|
+
gem.email = "moritz@twoticketsplease.de"
|
11
|
+
gem.homepage = "http://github.com/DerGuteMoritz/activerecord-lazy-attributes"
|
12
|
+
gem.authors = ["Moritz Heidkamp"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency 'activerecord', '>= 2.3.5'
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "activerecord-lazy-attributes #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{activerecord-lazy-attributes}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Moritz Heidkamp"]
|
12
|
+
s.date = %q{2010-02-18}
|
13
|
+
s.description = %q{A useful ActiveRecord extension to handle columns containing large amounts of data}
|
14
|
+
s.email = %q{moritz@twoticketsplease.de}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"activerecord-lazy-attributes.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"lib/lazy_attributes.rb",
|
29
|
+
"rails/init.rb",
|
30
|
+
"spec/lazy_attributes_spec.rb",
|
31
|
+
"spec/models/image.rb",
|
32
|
+
"spec/schema.rb",
|
33
|
+
"spec/spec.opts",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/DerGuteMoritz/activerecord-lazy-attributes}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{An ActiveRecord extension which allows attributes to be lazy-loaded}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/spec_helper.rb",
|
43
|
+
"spec/models/image.rb",
|
44
|
+
"spec/schema.rb",
|
45
|
+
"spec/lazy_attributes_spec.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
54
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.3.5"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
57
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
61
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'lazy_attributes'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module DerGuteMoritz
|
4
|
+
module ActiveRecord
|
5
|
+
module LazyAttributes
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.class_eval do
|
9
|
+
class_inheritable_accessor :lazy_attributes
|
10
|
+
class_inheritable_accessor :eager_attributes
|
11
|
+
self.lazy_attributes = []
|
12
|
+
extend ClassMethods
|
13
|
+
metaclass.alias_method_chain :default_select, :lazy_attributes
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
|
19
|
+
def attr_lazy(*attrs)
|
20
|
+
normalized_attrs = attrs.map { |a| a.to_s }
|
21
|
+
self.lazy_attributes += normalized_attrs
|
22
|
+
self.eager_attributes = (column_names - lazy_attributes).map { |a| connection.quote_column_name(a) }
|
23
|
+
|
24
|
+
normalized_attrs.each do |attr|
|
25
|
+
define_method attr do
|
26
|
+
write_attribute(attr, connection.select_value('SELECT %s FROM %s WHERE %s = %s' % [connection.quote_column_name(attr), self.class.quoted_table_name, self.class.primary_key, quoted_id])) unless has_attribute?(attr)
|
27
|
+
read_attribute(attr)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def default_select_with_lazy_attributes(qualified)
|
36
|
+
if lazy_attributes.empty?
|
37
|
+
default_select_without_lazy_attributes(qualified)
|
38
|
+
else
|
39
|
+
if qualified
|
40
|
+
eager_attributes.map { |a| "#{quoted_table_name}.#{a}" }
|
41
|
+
else
|
42
|
+
eager_attributes
|
43
|
+
end.join(', ')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
ActiveRecord::Base.send :include, DerGuteMoritz::ActiveRecord::LazyAttributes
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require Pathname.new(__FILE__).dirname.join('..', 'init')
|
@@ -0,0 +1,26 @@
|
|
1
|
+
root = Pathname.new(__FILE__).dirname
|
2
|
+
|
3
|
+
require root.join('spec_helper')
|
4
|
+
require root.join('models/image')
|
5
|
+
|
6
|
+
describe DerGuteMoritz::ActiveRecord::LazyAttributes do
|
7
|
+
|
8
|
+
before :each do
|
9
|
+
@id = Image.create!(:name => 'an image', :data => 'a lot of data').id
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should eagerly load attributes not defined lazy' do
|
13
|
+
i = Image.find(@id)
|
14
|
+
i.should have_attribute('name')
|
15
|
+
i.name.should == 'an image'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should lazily load attributes defined as lazy' do
|
19
|
+
i = Image.find(@id)
|
20
|
+
i.should_not have_attribute('data')
|
21
|
+
i.data
|
22
|
+
i.data.should == 'a lot of data'
|
23
|
+
i.should have_attribute('data')
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/schema.rb
ADDED
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'lazy_attributes'
|
6
|
+
require 'spec'
|
7
|
+
require 'spec/autorun'
|
8
|
+
|
9
|
+
Spec::Runner.configure do |config|
|
10
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
11
|
+
load 'schema.rb'
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-lazy-attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Moritz Heidkamp
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-18 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.5
|
34
|
+
version:
|
35
|
+
description: A useful ActiveRecord extension to handle columns containing large amounts of data
|
36
|
+
email: moritz@twoticketsplease.de
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- activerecord-lazy-attributes.gemspec
|
52
|
+
- init.rb
|
53
|
+
- lib/lazy_attributes.rb
|
54
|
+
- rails/init.rb
|
55
|
+
- spec/lazy_attributes_spec.rb
|
56
|
+
- spec/models/image.rb
|
57
|
+
- spec/schema.rb
|
58
|
+
- spec/spec.opts
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://github.com/DerGuteMoritz/activerecord-lazy-attributes
|
62
|
+
licenses: []
|
63
|
+
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --charset=UTF-8
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: An ActiveRecord extension which allows attributes to be lazy-loaded
|
88
|
+
test_files:
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/models/image.rb
|
91
|
+
- spec/schema.rb
|
92
|
+
- spec/lazy_attributes_spec.rb
|