Method: Etapper::JournalEntryCollection#initialize

Defined in:
lib/etapper/classes/journal_entry_collection.rb

#initialize(account = nil, options = {}) ⇒ JournalEntryCollection

Returns a new collection of journal entries. Requires an Etapper::Account object as the first parameter. Also allows options (:type, :count, etc) to be passed to the journal entry request.

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/etapper/classes/journal_entry_collection.rb', line 10

def initialize(=nil, options={})
  raise RequiredFieldError, "An account is required!" unless @account = 
  
  # Set up our request
  @request = API::PagedJournalEntriesRequest.new
  @request.accountRef = .ref
  @request.count = options[:count] || 100   # Default to the largest request we can make
  @request.startDate = options[:startDate].to_date if options[:startDate]
  @request.endDate = options[:endDate].to_date if options[:endDate]
  @request.baseQuery = options[:baseQuery]
  @request.start = options[:start]
  if options[:type]
    @request.types = [JournalEntry::TYPES[options[:type]]]
  elsif options[:types]
    @request.types = options[:types].collect {|t| JournalEntry::TYPES[t]}
  end
  
  # Make the request
  @response = Etapper.client.getJournalEntries(@request)
  @total = @response.total  # Do we have all the records?
  @records = @response.data
  
  enum = Proc.new do |yielder|
    counter = 0
    while counter < @total
      if counter == @records.length  # Retrieve more records
        @request.start = counter
        more = Etapper.client.getJournalEntries(@request)
        @records += more.data
      end
      record = @records[counter]
      etapper_class = Etapper.etapper_class(record.class)
      
      yielder.yield etapper_class.new(record)
      counter += 1
    end
  end
  
  super(&enum)
end