Module: Stratagem::AutoMock::UserLoader

Included in:
Aquifer, Crawler::Authentication::Configured
Defined in:
lib/stratagem/auto_mock/user_loader.rb

Instance Method Summary collapse

Instance Method Details

#load_user_from_configuration(credentials) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stratagem/auto_mock/user_loader.rb', line 5

def load_user_from_configuration(credentials)
  possible_matches = []
  auth_creds = credentials.authentication_parameters
  model = credentials.model.constantize
  @all_user_objects ||= model.all
  log "\tloaded #{@all_user_objects.size} users from the database"
  @all_user_objects.each do |user|
    matching_attributes = matching_credentials(auth_creds, user)
    possible_matches << [user, matching_attributes] if matching_attributes > 0
  end

  match = possible_matches.sort {|a,b| a[1] <=> b[1] }.last
  if (match)
    user = match.first
    auth_creds.each {|k,v| user.stratagem.mock_attributes[k] = v }
    user
  else
    nil
  end
end

#matching_credentials(authentication_parameters, user) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/stratagem/auto_mock/user_loader.rb', line 26

def matching_credentials(authentication_parameters, user)
  count = 0
  authentication_parameters.each do |attribute, value|
    begin
      count += 1 if (value == user.send(attribute.to_sym))
    rescue
    end
  end
  count
end