Class: Eco::API::Common::People::Entries

Inherits:
Language::Models::Collection show all
Defined in:
lib/eco/api/common/people/entries.rb

Overview

Class meant to offer a collection of entries, normally used to get parsed input data.

Defined Under Namespace

Classes: MultipleSearchResults

Constant Summary

Constants inherited from Language::Models::Collection

Language::Models::Collection::BASIC_METHODS, Language::Models::Collection::EXTENDED_METHODS

Main identifier helpers collapse

Special filters collapse

Searchers collapse

Basic Collection Methods collapse

Groupping methods collapse

Instance Method Summary collapse

Methods inherited from Language::Models::Collection

#<, #<<, #attr, #attr?, attr_collection, attr_presence, #attrs, attrs_create_method, #contains, #delete!, #empty, #empty?, #group_by, #length, #merge, #new, #newFrom, #present, #present_all?, #present_some?, #remove, #to_c, #unique_attrs, #update

Constructor Details

#initialize(data = [], klass:, factory:) ⇒ Entries

Returns a new instance of Entries.



54
55
56
57
58
# File 'lib/eco/api/common/people/entries.rb', line 54

def initialize(data = [], klass:, factory:)
  super

  @caches_init = false
end

Instance Method Details

#[](id_or_ext) ⇒ Object



70
71
72
# File 'lib/eco/api/common/people/entries.rb', line 70

def [](id_or_ext)
  id(id_or_ext) || external_id(id_or_ext)
end

#each(&block) ⇒ Object

Override each to make it work with supervisor hiearchy



147
148
149
150
151
# File 'lib/eco/api/common/people/entries.rb', line 147

def each(&block)
  return to_enum(:each) unless block

  @items.each(&block)
end

#email_id_mapsObject



187
188
189
# File 'lib/eco/api/common/people/entries.rb', line 187

def email_id_maps
  email_present.group_by(:email).transform_values(&:id)
end

#entry(id: nil, external_id: nil, email: nil, strict: false) ⇒ Entry?

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

Search function to find an entry based on one of different options It searches an entry using the parameters given.

Parameters:

  • id (String) (defaults to: nil)

    the internal id of the person

  • external_id (String) (defaults to: nil)

    the exernal_id of the person

  • email (String) (defaults to: nil)

    the email of the person

  • strict (Boolean) (defaults to: false)

    if should perform a :soft or a :strict search. strict will avoid repeated email addresses.

Returns:

  • (Entry, nil)

    the entry we were searching, or nil if not found.

Raises:

  • 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.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/eco/api/common/people/entries.rb', line 115

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) ⇒ Object



153
154
155
# File 'lib/eco/api/common/people/entries.rb', line 153

def exclude(object)
  exclude_people(into_a(object))
end

#exclude_people(list) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/eco/api/common/people/entries.rb', line 157

def exclude_people(list)
  discarded = list.map do |person|
    find(person)
  end.compact

  newFrom to_a - discarded
end

#export(filename, parsing_phase: :internal) ⇒ Object

TODO:

could it somehow rather use the people-to-csv case?

Helper to dump the entries into a CSV

Parameters:

  • filename (String)

    the destination file

  • parsing_phase (Symbol) (defaults to: :internal)

    data as per specific parsing stage.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/eco/api/common/people/entries.rb', line 169

def export(filename, parsing_phase: :internal)
  header = each_with_object([]) do |entry, hds|
    hds.push(
      *entry.entry(parsing_phase).keys
    ).uniq!
  end

  CSV.open(filename, 'w') do |csv|
    csv << header

    each do |entry|
      csv << entry.entry(parsing_phase).values_at(*header)
    end
  end
end

#external_id(*args) ⇒ Object



66
67
68
# File 'lib/eco/api/common/people/entries.rb', line 66

def external_id(*args)
  attr('external_id', *args).first
end

#filter_tags_all(tags) ⇒ Object



81
82
83
# File 'lib/eco/api/common/people/entries.rb', line 81

def filter_tags_all(tags)
  attr('filter_tags', tags, default_modifier.all.insensitive)
end

#filter_tags_any(tags) ⇒ Object



77
78
79
# File 'lib/eco/api/common/people/entries.rb', line 77

def filter_tags_any(tags)
  attr('filter_tags', tags, default_modifier.any.insensitive)
end

#find(object, strict: false) ⇒ Object

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



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/eco/api/common/people/entries.rb', line 130

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_supervisorObject



191
192
193
# File 'lib/eco/api/common/people/entries.rb', line 191

def group_by_supervisor
  to_h(:supervisor_id)
end

#id(*args) ⇒ Object



62
63
64
# File 'lib/eco/api/common/people/entries.rb', line 62

def id(*args)
  attr('id', *args).first
end

#policy_group_ids_all(ids) ⇒ Object



89
90
91
# File 'lib/eco/api/common/people/entries.rb', line 89

def policy_group_ids_all(ids)
  attr('policy_group_ids', ids, default_modifier.all.insensitive)
end

#policy_group_ids_any(ids) ⇒ Object



85
86
87
# File 'lib/eco/api/common/people/entries.rb', line 85

def policy_group_ids_any(ids)
  attr('policy_group_ids', ids, default_modifier.any.insensitive)
end

#to_h(attr = 'id') ⇒ Object



195
196
197
# File 'lib/eco/api/common/people/entries.rb', line 195

def to_h(attr = 'id')
  super(attr || 'id')
end