Module: LeapSalesforce::Users

Defined in:
lib/leap_salesforce/users/users.rb

Overview

Where test users are defined

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.listArray

Returns List of test users.

Returns:

  • (Array)

    List of test users



14
15
16
# File 'lib/leap_salesforce/users/users.rb', line 14

def list
  @list.empty? ? [User.new(:admin, ENV['SF_USERNAME'])] : @list
end

Class Method Details

.add(user) ⇒ Object

Parameters:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/leap_salesforce/users/users.rb', line 19

def add(user)
  return @list << user if user.is_a? LeapSalesforce::User

  unless user.is_a? Array
    raise LeapSalesforce::UserError, "Incorrect type #{user.class} for user. Expected either " \
    'type LeapSalesforce::User or Array'
  end
  if user[2]
    desc = user[2].is_a?(Hash) ? user[2].values[0] : user[2].dup
    user[2] = nil
  end

  @list << User.new(*user, description: desc)
end

.any?(filter) ⇒ Boolean

Returns Whether any user matches filter.

Returns:

  • (Boolean)

    Whether any user matches filter



107
108
109
110
111
112
# File 'lib/leap_salesforce/users/users.rb', line 107

def any?(filter)
  where(filter)
  true
rescue LeapSalesforce::UserError
  false
end

.each(filter = nil, &block) ⇒ Object

Iterate through each user, narrowing on filter if provided



63
64
65
66
67
68
69
# File 'lib/leap_salesforce/users/users.rb', line 63

def each(filter = nil, &block)
  if filter
    where({ **filter, all: true }).each(&block)
  else
    list.each(&block)
  end
end

.execute_as(filter) ⇒ Object

Returns Result of block.

Parameters:

Returns:

  • (Object)

    Result of block



84
85
86
87
88
89
90
# File 'lib/leap_salesforce/users/users.rb', line 84

def execute_as(filter)
  current_user = LeapSalesforce.api_user
  LeapSalesforce.api_user = LeapSalesforce::Users.where filter
  result = yield
  LeapSalesforce.api_user = current_user
  result
end

.execute_as_if_present(user_filter, &block) ⇒ Object

Execute block as user matching filter if that user is present If user is not present the current user will be used

Returns:

  • (Object)

    Result of block

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
# File 'lib/leap_salesforce/users/users.rb', line 95

def execute_as_if_present(user_filter, &block)
  raise ArgumentError, 'Pass block to :execute_as_if_present method' unless block_given?

  if any? user_filter
    execute_as(user_filter, &block)
  else
    LeapSalesforce.logger.warn "No user found user filter #{user_filter}, using '#{LeapSalesforce.api_user}'"
    yield
  end
end

.match_params(hash_filter) ⇒ LeapSalesforce::User, Array

Returns A user that meets the criteria.

Returns:



72
73
74
75
76
77
78
79
80
# File 'lib/leap_salesforce/users/users.rb', line 72

def match_params(hash_filter)
  lookup_key, lookup_value = hash_filter.first
  users = list.find_all { |user_queried| user_queried.match?(lookup_key, lookup_value) }
  if users.empty?
    raise UserError, "Unable to find user based on filer #{hash_filter}. " \
    "Users are: #{list}"
  end
  @all ? users : users.first
end

.where(filter) ⇒ LeapSalesforce::User, Array Also known as: []

Returns A user that meets the criteria. If all is true this returns array of users.

Examples:

Filter user by key of :admin

where(key: :admin)

Filter user by description containing Payroll

where(description: /Payroll/)

Filter user by username

where(username: '[email protected]')

Parameters:

Returns:

  • (LeapSalesforce::User, Array)

    A user that meets the criteria. If all is true this returns array of users



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/leap_salesforce/users/users.rb', line 44

def where(filter)
  @all = false
  case filter
  when :default then list.first # Will then use first user defined
  when Hash
    @all = filter[:all] || false
    match_params filter
  when String then match_params(username: filter)
  when Symbol then match_params(key: filter)
  when Regexp then match_params(description: filter)
  when User then filter
  else
    raise UserError, "Unable to find user using class #{filter.class}"
  end
end