Class: Etapper::JournalEntryCollection
- Inherits:
-
Enumerator
- Object
- Enumerator
- Etapper::JournalEntryCollection
- 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
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
-
#<<(addendum) ⇒ Object
Adds a journal entry of the appropriate type to the account, and to the end of this collection.
- #complete? ⇒ Boolean
-
#initialize(account = nil, options = {}) ⇒ JournalEntryCollection
constructor
Returns a new collection of journal entries.
- #size ⇒ Object
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.
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(account=nil, ={}) raise RequiredFieldError, "An account is required!" unless @account = account # Set up our request @request = API::PagedJournalEntriesRequest.new @request.accountRef = account.ref @request.count = [:count] || 100 # Default to the largest request we can make @request.startDate = [:startDate].to_date if [:startDate] @request.endDate = [:endDate].to_date if [:endDate] @request.baseQuery = [:baseQuery] @request.start = [:start] if [:type] @request.types = [JournalEntry::TYPES[[:type]]] elsif [:types] @request.types = [: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
#account ⇒ Object (readonly)
Returns the value of attribute account.
6 7 8 |
# File 'lib/etapper/classes/journal_entry_collection.rb', line 6 def account @account end |
#request ⇒ Object (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 = @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
55 56 57 |
# File 'lib/etapper/classes/journal_entry_collection.rb', line 55 def complete? @records.count == size end |
#size ⇒ Object
51 52 53 |
# File 'lib/etapper/classes/journal_entry_collection.rb', line 51 def size @total end |