Class: Syrup::Account

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_numberObject



58
59
60
61
# File 'lib/syrup/account.rb', line 58

def 
  populate unless @account_number
  @account_number
end

#available_balanceObject



68
69
70
71
# File 'lib/syrup/account.rb', line 68

def available_balance
  populate unless @available_balance
  @available_balance
end

#current_balanceObject



63
64
65
66
# File 'lib/syrup/account.rb', line 63

def current_balance
  populate unless @current_balance
  @current_balance
end

#idObject

Returns the value of attribute id.



44
45
46
# File 'lib/syrup/account.rb', line 44

def id
  @id
end

#nameObject



48
49
50
51
# File 'lib/syrup/account.rb', line 48

def name
  populate unless @name
  @name
end

#prior_day_balanceObject



73
74
75
76
# File 'lib/syrup/account.rb', line 73

def prior_day_balance
  populate unless @prior_day_balance
  @prior_day_balance
end

#typeObject



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 ==()
  .id == self.id && .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!()
  if 
    .instance_variables.each do |filled_var|
      self.instance_variable_set(filled_var, .instance_variable_get(filled_var))
    end
  end
  self
end

#populateObject

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.(id)
  end
end

#populated=(value) ⇒ Object



94
95
96
# File 'lib/syrup/account.rb', line 94

def populated=(value)
  @populated = value
end

#populated?Boolean

Returns:

  • (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

Returns:

  • (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