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

Inherits:
Language::Models::Collection show all
Includes:
SupervisorHelpers
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.

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 included from SupervisorHelpers::ClassMethods

#print_tree, #sort_by_supervisors, #supervisors_tree, #tree_to_str

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.



15
16
17
18
# File 'lib/eco/api/common/people/entries.rb', line 15

def initialize(data = [], klass:, factory:)
  super(data, klass: klass, factory: factory)
  @caches_init = false
end

Instance Method Details

#[](id_or_ext) ⇒ Object



30
31
32
# File 'lib/eco/api/common/people/entries.rb', line 30

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



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

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_mapsObject



117
118
119
# File 'lib/eco/api/common/people/entries.rb', line 117

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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/eco/api/common/people/entries.rb', line 57

def entry(id: nil, external_id: nil, email: nil, strict: false)
  init_caches
  pers = nil
  pers = @by_id[id]&.first if id
  pers = @by_external_id[external_id&.strip]&.first    if !pers && !external_id.to_s.strip.empty?

  # strict prevents taking existing user for searched person with same email
  # specially useful if the organisation ensures all have external id (no need for email search)
  if !pers && (!strict || external_id.to_s.strip.empty?)
    pers = @by_email[email&.downcase.strip]&.first       if !pers && !email.to_s.strip.empty?
    pers = @by_external_id[email&.downcase.strip]&.first if !pers && !email.to_s.strip.empty?
  end
  pers
end

#exclude(object) ⇒ Object



91
92
93
# File 'lib/eco/api/common/people/entries.rb', line 91

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

#exclude_people(list) ⇒ Object



95
96
97
98
99
100
# File 'lib/eco/api/common/people/entries.rb', line 95

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

#export(filename) ⇒ Object

Helper to dump the entries into a CSV

Parameters:

  • filename (String)

    the destination file



104
105
106
107
108
109
110
111
112
113
# File 'lib/eco/api/common/people/entries.rb', line 104

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



26
27
28
# File 'lib/eco/api/common/people/entries.rb', line 26

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

#filter_tags_all(tags) ⇒ Object



41
42
43
# File 'lib/eco/api/common/people/entries.rb', line 41

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

#filter_tags_any(tags) ⇒ Object



37
38
39
# File 'lib/eco/api/common/people/entries.rb', line 37

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



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

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



121
122
123
# File 'lib/eco/api/common/people/entries.rb', line 121

def group_by_supervisor
  to_h(:supervisor_id)
end

#id(*args) ⇒ Object



22
23
24
# File 'lib/eco/api/common/people/entries.rb', line 22

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

#policy_group_ids_all(ids) ⇒ Object



49
50
51
# File 'lib/eco/api/common/people/entries.rb', line 49

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

#policy_group_ids_any(ids) ⇒ Object



45
46
47
# File 'lib/eco/api/common/people/entries.rb', line 45

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

#to_h(attr = "id") ⇒ Object



125
126
127
# File 'lib/eco/api/common/people/entries.rb', line 125

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