Permalink
Browse files

lazy load records for the collection

  • Loading branch information...
Subhash Chandra
Subhash Chandra committed Jul 3, 2012
1 parent 82029ee commit b46c103bd64f86fb12cf88b918e6afc3082d3964
Showing with 11 additions and 7 deletions.
  1. +10 −5 lib/sheet_mapper/collection.rb
  2. +1 −2 test/spreadsheet_test.rb
@@ -4,15 +4,20 @@ module SheetMapper
class CollectionNotFound < StandardError; end
class Collection < SimpleDelegator
- attr_reader :records, :worksheet
+ attr_reader :worksheet
# spreadsheet is a SheetMapper::Spreadsheet
# SheetMapper::Collection.new(@sheet, @work)
def initialize(spreadsheet, worksheet)
@spreadsheet = spreadsheet
@worksheet = worksheet
@mapper = @spreadsheet.mapper
- @records = process_records!
+ end
+
+ # Returns the processed records
+ # @collection.records => [<SheetMapper::Base>, ...]
+ def records
+ @records ||= process_records!
end
# Returns an array of mapped records
@@ -38,11 +43,11 @@ def save(records=@records)
# Reload worksheet discarding changes not saved
def reload
@worksheet.reload
- @records = process_records!
+ @records = nil
end
def __getobj__
- @records
+ records
end
protected
@@ -58,4 +63,4 @@ def process_records!
records
end
end
-end
+end
View
@@ -29,7 +29,6 @@
@sheet = SheetMapper::Spreadsheet.new(:mapper => Object, :key => 'foo', :login => 'login', :password => 'pass')
@work_stub = stub(:worksheet)
@work_stub.expects(:title).returns("FOO")
- @work_stub.expects(:rows).returns([])
@sheet_stub.expects(:worksheets).returns([@work_stub])
@collection = @sheet.find_collection_by_title("foo")
end
@@ -39,4 +38,4 @@
assert_equal @work_stub, @collection.worksheet
end
end # find_collection_by_title
-end # Spreadsheet
+end # Spreadsheet

0 comments on commit b46c103

Please sign in to comment.