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::ATTR_COLLECTION_METHODS, Language::Models::Collection::ATTR_PRESENCE_METHODS

Instance Method Summary collapse

Methods included from SupervisorHelpers

#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



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

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

#each(params: {}, &block) ⇒ Object

Override each to make it work with supervisor hiearchy



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

def each(params: {}, &block)
  return to_enum(:each) unless block
  @array_supers = sort_by_supervisors(@items) unless @caches_init
  @array_supers.each(&block)
end

#email_id_mapsObject



83
84
85
# File 'lib/eco/api/common/people/entries.rb', line 83

def email_id_maps
  email_present.group_by(:email).transform_values { |person| person.id }
end

#entry(id: nil, external_id: nil, email: nil) ⇒ Object

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



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

def entry(id: nil, external_id: nil, email: nil)
  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?
  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?
  pers
end

#exclude(object) ⇒ Object



72
73
74
# File 'lib/eco/api/common/people/entries.rb', line 72

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

#exclude_people(list) ⇒ Object



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

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



41
42
43
44
45
46
47
48
49
50
# File 'lib/eco/api/common/people/entries.rb', line 41

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



35
36
37
# File 'lib/eco/api/common/people/entries.rb', line 35

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

#find(object) ⇒ Object

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



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

def find(object)
  id          = object.respond_to?("id")?          object.send("id") : nil
  external_id = object.respond_to?("external_id")? object.send("external_id") : nil
  email       = object.respond_to?("email")?       object.send("email") : nil
  entry(id: id, external_id: external_id, email: email)
end

#group_by_supervisorObject



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

def group_by_supervisor
  to_h(:supervisor_id)
end

#id(*args) ⇒ Object



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

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

#to_h(attr = "id") ⇒ Object



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

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