Module: LeapSalesforce::Users
- Defined in:
- lib/leap_salesforce/users/users.rb
Overview
Where test users are defined
Class Attribute Summary collapse
-
.list ⇒ Array
List of test users.
Class Method Summary collapse
- .add(user) ⇒ Object
-
.any?(filter) ⇒ Boolean
Whether any user matches filter.
-
.each(filter = nil, &block) ⇒ Object
Iterate through each user, narrowing on filter if provided.
-
.execute_as(filter) ⇒ Object
Result of block.
-
.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.
-
.match_params(hash_filter) ⇒ LeapSalesforce::User, Array
A user that meets the criteria.
-
.where(filter) ⇒ LeapSalesforce::User, Array
(also: [])
A user that meets the criteria.
Class Attribute Details
Class Method Details
.add(user) ⇒ Object
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.
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.
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
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.
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.
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 |