class Eco::API::Common::People::Entries

Class meant to offer a collection of entries, normally used to get parsed input data. @attr_reader entries [Array<Eco::API::Common::PeopleEntry] a pure `Array` object.

Public Class Methods

new(data = [], klass:, factory:) click to toggle source
Calls superclass method Eco::Language::Models::Collection::new
# File lib/eco/api/common/people/entries.rb, line 51
def initialize(data = [], klass:, factory:)
  super(data, klass: klass, factory: factory)
  @caches_init = false
end

Public Instance Methods

[](id_or_ext) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 66
def [](id_or_ext)
  id(id_or_ext) || external_id(id_or_ext)
end
each(&block) click to toggle source

Override `each` to make it work with supervisor hiearchy

# File lib/eco/api/common/people/entries.rb, line 136
def each(&block)
  return to_enum(:each) unless block
  @array_supers = sort_by_supervisors(@items) unless @caches_init
  @array_supers.each(&block)
end
email_id_maps() click to toggle source

@!group Groupping methods

# File lib/eco/api/common/people/entries.rb, line 169
def email_id_maps
  email_present.group_by(:email).transform_values { |person| person.id }
end
entry(id: nil, external_id: nil, email: nil, strict: false) click to toggle source

Search function to find an `entry` based on one of different options

It searches an entry using the parameters given.

@note This is how the search function actually works:

1. if eP `id` is given, returns the entry (if found), otherwise...
2. if `external_id` is given, returns the entry (if found), otherwise...
3. if `strict` is `false` and `email` is given:
  - if there is only 1 entry with that email, returns that entry, otherwise...
  - if found but, there are many candidate entries, it raises MultipleSearchResults error
  - if entry `external_id` matches `email`, returns that entry

@raise MultipleSearchResults if there are multiple entries with the same `email`

and there's no other criteria to find the entry. It only gets to this point if
`external_id` was **not** provided and we are **not** in 'strict' search mode.
However, it could be we were in `strict` mode and `external_id` was not provided.

@param id [String] the `internal id` of the person @param external_id [String] the `exernal_id` of the person @param email [String] the `email` of the person @param strict [Boolean] if should perform a `:soft` or a `:strict` search. `strict` will avoid repeated email addresses. @return [Entry, nil] the entry we were searching, or `nil` if not found.

# File lib/eco/api/common/people/entries.rb, line 110
def entry(id: nil, external_id: nil, email: nil, strict: false)
  init_caches
  # normalize values
  ext_id   = !external_id.to_s.strip.empty? && external_id.strip
  email    = !email.to_s.strip.empty? && email.downcase.strip

  e   = nil
  e ||= @by_id[id]&.first
  e ||= @by_external_id[ext_id]&.first
  e ||= entry_by_email(email) unless strict && ext_id
  e
end
exclude(object) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 142
def exclude(object)
  exclude_people(into_a(object))
end
exclude_people(list) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 146
def exclude_people(list)
  discarded = list.map do |person|
    find(person)
  end.compact
  newFrom to_a - discarded
end
export(filename) click to toggle source

TODO: it should rather use the the people-to-csv case somehow Helper to dump the entries into a CSV @param filename [String] the destination file

# File lib/eco/api/common/people/entries.rb, line 156
def export(filename)
  CSV.open(filename, "w") do |csv|
    entry  = self.first
    header = entry.internal_entry.keys
    csv << header
    self.each do |entry|
      csv << entry.internal_entry.values
    end
  end
end
external_id(*args) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 62
def external_id(*args)
  attr('external_id', *args).first
end
filter_tags_all(tags) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 77
def filter_tags_all(tags)
  attr("filter_tags", tags, default_modifier.all.insensitive)
end
filter_tags_any(tags) click to toggle source

@!group Special filters

# File lib/eco/api/common/people/entries.rb, line 73
def filter_tags_any(tags)
  attr("filter_tags", tags, default_modifier.any.insensitive)
end
find(object, strict: false) click to toggle source

Search function to find an `entry` based on one of different options see Eco::API::Common::People::Entries#entry

# File lib/eco/api/common/people/entries.rb, line 125
def find(object, strict: false)
  id          = attr_value(object, "id")
  external_id = attr_value(object, "external_id")
  email       = attr_value(object, "email")
  entry(id: id, external_id: external_id, email: email, strict: strict)
end
group_by_supervisor() click to toggle source
# File lib/eco/api/common/people/entries.rb, line 173
def group_by_supervisor
  to_h(:supervisor_id)
end
id(*args) click to toggle source

@!group Main identifier helpers

# File lib/eco/api/common/people/entries.rb, line 58
def id(*args)
  attr('id', *args).first
end
policy_group_ids_all(ids) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 85
def policy_group_ids_all(ids)
  attr("policy_group_ids", tags, default_modifier.all.insensitive)
end
policy_group_ids_any(ids) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 81
def policy_group_ids_any(ids)
  attr("policy_group_ids", tags, default_modifier.any.insensitive)
end
to_h(attr = "id") click to toggle source
Calls superclass method Eco::Language::Models::Collection#to_h
# File lib/eco/api/common/people/entries.rb, line 177
def to_h(attr = "id")
  super(attr || "id")
end

Protected Instance Methods

on_change() click to toggle source

@!endgroup

# File lib/eco/api/common/people/entries.rb, line 184
def on_change
  @caches_init = false
end

Private Instance Methods

entry_by_email(email, prevent_multiple_match: false) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 190
def entry_by_email(email, prevent_multiple_match: false)
  return nil unless email

  candidates  = @by_email[email] || []
  return candidates.first if candidates.length == 1

  if prevent_multiple_match && !candidates.empty?
    msg = "Multiple search results match the criteria."
    raise MultipleSearchResults.new(msg, candidates: candidates, property: "email")
  end

  @by_external_id[email]&.first
end
init_caches() click to toggle source
# File lib/eco/api/common/people/entries.rb, line 204
def init_caches
  return if @caches_init
  @by_id          = no_nil_key(to_h)
  @by_external_id = no_nil_key(to_h('external_id'))
  @by_email       = no_nil_key(to_h('email'))
  @array_supers   = sort_by_supervisors(@items)
  @caches_init = true
end
no_nil_key(hash) click to toggle source
# File lib/eco/api/common/people/entries.rb, line 213
def no_nil_key(hash)
  hash.tap {|h| h.delete(nil)}
end