summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2014-07-13 10:15:12 -0600
committermo khan <mo@mokhan.ca>2014-07-13 10:15:12 -0600
commitca1e0cc7a7ee2c578e5ecdcc373edc7bd03ea48d (patch)
tree67ca4754414d9096a56db321dfbba3c6fbe276c9
parentd7bae1a6fb76230f7ddf1fa6e848f055ac53da6a (diff)
use an array proxy to lay load collections.main
-rw-r--r--lib/humble/column.rb34
1 files changed, 31 insertions, 3 deletions
diff --git a/lib/humble/column.rb b/lib/humble/column.rb
index 7582a00..2a3b238 100644
--- a/lib/humble/column.rb
+++ b/lib/humble/column.rb
@@ -75,13 +75,41 @@ module Humble
end
def apply(row, entity, session)
- puts "#{@attribute} #{@type} #{row} #{entity}"
- items = session.find_all(@type)
- entity.public_send("#{@attribute}=", items)
+ proxy = ArrayProxy.new(session, @type, @attribute, entity)
+ entity.public_send("#{@attribute}=", proxy)
end
def prepare(entity)
{ }
end
end
+
+ class ArrayProxy
+ include Enumerable
+
+ def initialize(session, type, attribute, parent_entity)
+ @session = session
+ @type = type
+ @attribute = attribute
+ @parent = parent_entity
+ end
+
+ def each
+ items.each do |item|
+ yield item
+ end
+ end
+
+ def inspect
+ "[#{map { |x| x.inspect }.join(", ")}]"
+ end
+
+ private
+
+ def items
+ @items ||= @session.find_all(@type).find_all do |item|
+ item.movie && item.movie.id == @parent.id
+ end
+ end
+ end
end