Module: Eco::API::MicroCases::People::Fetch::WithEach

Included in:
Eco::API::MicroCases::People::Fetch
Defined in:
lib/eco/api/microcases/people/fetch/with_each.rb

Instance Method Summary collapse

Instance Method Details

#with_each(entries, people, options, append_created: true) {|entry, person| ... } ⇒ Eco::API::Organization::People

Note:
  • it also links to person.entry the input data entry.

Finds each entry of entries in people and runs a block.

Parameters:

Yields:

  • (entry, person)

    gives each entry, and the paired person thereof (new or existing).

Yield Parameters:

  • entry (PersonEntry)

    the input entry with the data we should set on person.

  • person (Ecoportal::API::V1::Person)

    the found person that matches entry, or a new person otherwise.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/eco/api/microcases/people/fetch/with_each.rb', line 18

def with_each(entries, people, options, append_created: true)
  @_skip_all_multiple_results = false
  people_copy = people.newFrom(people.to_a)

  entries.each_with_object([]) do |entry, scoped|
    begin
      person   = people_copy.find(entry, strict: micro.strict_search?(options))
      person ||= session.new_person.tap do |pers|
        people << pers if append_created
      end
    rescue Eco::API::Organization::People::MultipleSearchResults => e
      unless @_skip_all_multiple_results
        msg    = "\n * When searching this Entry: #{entry.to_s(:identify)}"
        person = _with_each_prompt_to_select_user(e.append_message(msg), entry: entry)
      end
    end

    next unless person

    person.entry = entry
    yield(entry, person) if block_given?

    scoped << person
  end.then do |all_people|
    people.newFrom all_people.uniq
  end
end