Class: Syrup::Institutions::InstitutionBase
- Inherits:
-
Object
- Object
- Syrup::Institutions::InstitutionBase
- Defined in:
- lib/syrup/institutions/institution_base.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
Returns the value of attribute password.
-
#secret_questions ⇒ Object
Returns the value of attribute secret_questions.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
This method is called whenever a class inherits from this class.
-
.subclasses ⇒ Object
Returns an array of all classes that inherit from this class.
Instance Method Summary collapse
- #accounts ⇒ Object
-
#find_account_by_id(account_id) ⇒ Object
Returns an account with the specified
account_id
. -
#initialize ⇒ InstitutionBase
constructor
A new instance of InstitutionBase.
-
#populate_account(account_id) ⇒ Object
Populates an account given an ‘account_id`.
-
#populate_accounts(populated_accounts = nil) ⇒ Object
Populates all of the user’s accounts at this institution.
- #populated=(value) ⇒ Object
- #populated? ⇒ Boolean
-
#setup {|_self| ... } ⇒ Object
This method allows you to setup an institution with block syntax.
Constructor Details
#initialize ⇒ InstitutionBase
Returns a new instance of InstitutionBase.
41 42 43 |
# File 'lib/syrup/institutions/institution_base.rb', line 41 def initialize @accounts = [] end |
Instance Attribute Details
#password ⇒ Object
Returns the value of attribute password.
39 40 41 |
# File 'lib/syrup/institutions/institution_base.rb', line 39 def password @password end |
#secret_questions ⇒ Object
Returns the value of attribute secret_questions.
39 40 41 |
# File 'lib/syrup/institutions/institution_base.rb', line 39 def secret_questions @secret_questions end |
#username ⇒ Object
Returns the value of attribute username.
39 40 41 |
# File 'lib/syrup/institutions/institution_base.rb', line 39 def username @username end |
Class Method Details
.inherited(subclass) ⇒ Object
This method is called whenever a class inherits from this class. We keep track of all of them because they should all be institutions. This way we can provide a list of supported institutions via code.
11 12 13 14 |
# File 'lib/syrup/institutions/institution_base.rb', line 11 def inherited(subclass) @subclasses ||= [] @subclasses << subclass end |
.subclasses ⇒ Object
Returns an array of all classes that inherit from this class. Or, in other words, an array of all supported institutions
18 19 20 |
# File 'lib/syrup/institutions/institution_base.rb', line 18 def subclasses @subclasses end |
Instance Method Details
#accounts ⇒ Object
64 65 66 67 |
# File 'lib/syrup/institutions/institution_base.rb', line 64 def accounts populate_accounts @accounts end |
#find_account_by_id(account_id) ⇒ Object
Returns an account with the specified account_id
. Always use this method to create a new ‘Account` object. If you do, it will get populated correctly whenever the population occurs.
72 73 74 75 76 77 78 79 |
# File 'lib/syrup/institutions/institution_base.rb', line 72 def find_account_by_id(account_id) account = @accounts.find { |a| a.id == account_id } unless account || populated? account = Account.new(:id => account_id, :institution => self) @accounts << account end account end |
#populate_account(account_id) ⇒ Object
Populates an account given an ‘account_id`. The implementing institution may populate all accounts when this is called if there isn’t a way to only request one account’s information.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/syrup/institutions/institution_base.rb', line 84 def populate_account(account_id) unless populated? result = fetch_account(account_id) return nil if result.nil? if result.respond_to?(:each) populate_accounts(result) find_account_by_id(account_id) else result.populated = true account = find_account_by_id(account_id) account.merge! result if account end end end |
#populate_accounts(populated_accounts = nil) ⇒ Object
Populates all of the user’s accounts at this institution.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/syrup/institutions/institution_base.rb', line 101 def populate_accounts(populated_accounts = nil) unless populated? all_accounts = populated_accounts || fetch_accounts # Remove any accounts that were added, that don't actually exist @accounts.delete_if do |a| if all_accounts.include?(a) false else a.valid = false true end end # Add any additional account information new_accounts = [] all_accounts.each do |filled_account| account = @accounts.find { |a| a.id == filled_account.id } filled_account.populated = true # If we already had an account with this id, fill it with data if account account.merge! filled_account else new_accounts << filled_account end end @accounts |= new_accounts # Uses set union self.populated = true end end |
#populated=(value) ⇒ Object
60 61 62 |
# File 'lib/syrup/institutions/institution_base.rb', line 60 def populated=(value) @populated = value end |
#populated? ⇒ Boolean
56 57 58 |
# File 'lib/syrup/institutions/institution_base.rb', line 56 def populated? @populated end |
#setup {|_self| ... } ⇒ Object
This method allows you to setup an institution with block syntax
InstitutionBase.setup do |config|
config.username = 'my_user"
...
end
51 52 53 54 |
# File 'lib/syrup/institutions/institution_base.rb', line 51 def setup yield self self end |