Class: Syrup::Account
- Inherits:
-
Object
- Object
- Syrup::Account
- Defined in:
- lib/syrup/account.rb
Overview
An account contains all the information related to the account. Information is loaded lazily so that you can use an account to get transactions without incurring the cost of getting any account information.
Instance Attribute Summary collapse
- #account_number ⇒ Object
- #available_balance ⇒ Object
- #current_balance ⇒ Object
-
#id ⇒ Object
Returns the value of attribute id.
- #name ⇒ Object
- #prior_day_balance ⇒ Object
- #type ⇒ Object
Instance Method Summary collapse
-
#==(other_account) ⇒ Object
Tests equality of this account with another account.
-
#find_transactions(starting_at, ending_at = Date.today) ⇒ Object
Returns an array of transactions from this account for the supplied date range.
-
#initialize(attr_hash = nil) ⇒ Account
constructor
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes (pass a hash with key names matching the associated attribute names).
-
#merge!(account_with_info) ⇒ Object
Merges this account information with another account.
-
#populate ⇒ Object
Populates this account with all of its information.
- #populated=(value) ⇒ Object
- #populated? ⇒ Boolean
- #valid=(validity) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(attr_hash = nil) ⇒ Account
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes (pass a hash with key names matching the associated attribute names).
80 81 82 83 84 85 86 87 88 |
# File 'lib/syrup/account.rb', line 80 def initialize(attr_hash = nil) if attr_hash attr_hash.each do |k, v| instance_variable_set "@#{k}", v end end @cached_transactions = [] end |
Instance Attribute Details
#account_number ⇒ Object
58 59 60 61 |
# File 'lib/syrup/account.rb', line 58 def account_number populate unless @account_number @account_number end |
#available_balance ⇒ Object
68 69 70 71 |
# File 'lib/syrup/account.rb', line 68 def available_balance populate unless @available_balance @available_balance end |
#current_balance ⇒ Object
63 64 65 66 |
# File 'lib/syrup/account.rb', line 63 def current_balance populate unless @current_balance @current_balance end |
#id ⇒ Object
Returns the value of attribute id.
44 45 46 |
# File 'lib/syrup/account.rb', line 44 def id @id end |
#name ⇒ Object
48 49 50 51 |
# File 'lib/syrup/account.rb', line 48 def name populate unless @name @name end |
#prior_day_balance ⇒ Object
73 74 75 76 |
# File 'lib/syrup/account.rb', line 73 def prior_day_balance populate unless @prior_day_balance @prior_day_balance end |
#type ⇒ Object
53 54 55 56 |
# File 'lib/syrup/account.rb', line 53 def type populate unless @type @type end |
Instance Method Details
#==(other_account) ⇒ Object
Tests equality of this account with another account. Accounts are considered equal if they have the same id.
108 109 110 |
# File 'lib/syrup/account.rb', line 108 def ==(other_account) other_account.id == self.id && other_account.is_a?(Account) end |
#find_transactions(starting_at, ending_at = Date.today) ⇒ Object
Returns an array of transactions from this account for the supplied date range.
113 114 115 116 117 |
# File 'lib/syrup/account.rb', line 113 def find_transactions(starting_at, ending_at = Date.today) return [] if starting_at > ending_at @institution.fetch_transactions(self.id, starting_at, ending_at) end |
#merge!(account_with_info) ⇒ Object
Merges this account information with another account. The other account’s information overrides this account’s.
121 122 123 124 125 126 127 128 |
# File 'lib/syrup/account.rb', line 121 def merge!(account_with_info) if account_with_info account_with_info.instance_variables.each do |filled_var| self.instance_variable_set(filled_var, account_with_info.instance_variable_get(filled_var)) end end self end |
#populate ⇒ Object
Populates this account with all of its information
99 100 101 102 103 104 |
# File 'lib/syrup/account.rb', line 99 def populate unless populated? || @institution.nil? raise "The account id must not be nil when populating an account" if id.nil? @institution.populate_account(id) end end |
#populated=(value) ⇒ Object
94 95 96 |
# File 'lib/syrup/account.rb', line 94 def populated=(value) @populated = value end |
#populated? ⇒ Boolean
90 91 92 |
# File 'lib/syrup/account.rb', line 90 def populated? @populated end |
#valid=(validity) ⇒ Object
138 139 140 |
# File 'lib/syrup/account.rb', line 138 def valid=(validity) @valid = validity end |
#valid? ⇒ Boolean
130 131 132 133 134 135 136 |
# File 'lib/syrup/account.rb', line 130 def valid? if @valid.nil? populate @valid = populated? end @valid end |