Class: Etapper::JournalEntryCollection

Inherits:
Enumerator
  • Object
show all
Defined in:
lib/etapper/classes/journal_entry_collection.rb

Overview

Retrieves journal entries consecutively from eTapestry, and allows us to enumerate over them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



6
7
8
# File 'lib/etapper/classes/journal_entry_collection.rb', line 6

def 
  @account
end

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/etapper/classes/journal_entry_collection.rb', line 6

def request
  @request
end

Instance Method Details

#<<(addendum) ⇒ Object

Adds a journal entry of the appropriate type to the account, and to the end of this collection. Note that no exceptional validations happen here, and the added record may not match the collection’s query parameters. Use with moderate caution.



62
63
64
65
66
67
68
# File 'lib/etapper/classes/journal_entry_collection.rb', line 62

def <<(addendum)
  addendum. = @account
  addendum.save
  @records.push addendum  # Add to the end of this collection
  @total += 1  # Increase the size of the collection
  self  # Return the object again so that << can be chained
end

#complete?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/etapper/classes/journal_entry_collection.rb', line 55

def complete?
  @records.count == size
end

#sizeObject



51
52
53
# File 'lib/etapper/classes/journal_entry_collection.rb', line 51

def size
  @total
end