Skip to content

Instantly share code, notes, and snippets.

@probablykabari
Created December 14, 2010 05:22
Show Gist options
  • Save probablykabari/740035 to your computer and use it in GitHub Desktop.
Save probablykabari/740035 to your computer and use it in GitHub Desktop.
This fixes lazy loading in Datamapper
From 8e634002d6d79e2c5e2b599e69edd867628364d3 Mon Sep 17 00:00:00 2001
From: Kabari Hendrick <[email protected]>
Date: Mon, 13 Dec 2010 23:10:33 -0600
Subject: [PATCH] fixing lazy load
Signed-off-by: Kabari Hendrick <[email protected]>
---
lib/dm-core/model/property.rb | 2 +-
spec/public/property_spec.rb | 14 +++++++++++++-
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/lib/dm-core/model/property.rb b/lib/dm-core/model/property.rb
index 7fbf867..c1d221e 100644
--- a/lib/dm-core/model/property.rb
+++ b/lib/dm-core/model/property.rb
@@ -90,7 +90,7 @@ module DataMapper
context = :default if context == true
Array(context).each do |context|
- properties.lazy_context(context) << self
+ properties.lazy_context(context) << property
end
end
diff --git a/spec/public/property_spec.rb b/spec/public/property_spec.rb
index 20bc7bc..5c8ac1b 100644
--- a/spec/public/property_spec.rb
+++ b/spec/public/property_spec.rb
@@ -22,6 +22,8 @@ describe DataMapper::Property do
property :md5hash, String, :key => true, :length => 32
property :title, String, :required => true, :unique => true
property :description, Text, :length => 1..1024, :lazy => [ :detail ]
+ property :width, Integer, :lazy => [:dimensions]
+ property :height, Integer, :lazy => [:dimensions]
property :format, String, :default => 'jpeg'
property :taken_at, Time, :default => proc { Time.now }
end
@@ -149,7 +151,17 @@ describe DataMapper::Property do
Track.properties[:artist].lazy?.should be(false)
end
end
-
+
+ describe "#lazy_load_properties" do
+ it "returns all lazy properties in the same context" do
+ Image.properties[:width].__send__(:lazy_load_properties).should == Image.properties.values_at(:width, :height)
+ end
+
+ it "returns all properties by default" do
+ Track.properties[:artist].__send__(:lazy_load_properties).should == Track.properties
+ end
+ end
+
describe '#length' do
it 'returns upper bound for Range values' do
Image.properties[:description].length.should eql(1024)
--
1.7.3.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment