Module: Eco::API::MicroCases::People::Manage::Load

Included in:
Eco::API::MicroCases::People::Manage
Defined in:
lib/eco/api/microcases/people/manage/load.rb

Instance Method Summary collapse

Instance Method Details

#people_load(filename = enviro.config.people.cache, modifier: %i[newest api])) ⇒ Eco::API::Organization::People

Note:
  • filename will be relative to the working directory (the one of the session enviro set by the user).

Helper to load People that works in different phases:

  1. first tries to get the newest cached file that follows filename pattern
    • if not the newest, it tries to find the specific filename
  2. if it succeeds to identify a cached file, it loads it
    • if it fails, it tries to get people from the server

Parameters:

  • filename (String) (defaults to: enviro.config.people.cache)

    the name of the file where the cached data is to be found.

  • modifier (Array<Symbol>) (defaults to: %i[newest api]))

    modifiers to specify how this function should proceed:

    • :newest if it should try to find the newest file (pattern alike).
    • :api if it should try to get people from the server in case there's no cache.
    • :file if it is supposed to load people from a file.
    • :save if it is supposed to cache/save the data locally once obtained people from the server (:api)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/eco/api/microcases/people/manage/load.rb', line 21

def people_load(filename = enviro.config.people.cache, modifier: %i[newest api]) # rubocop:disable Metrics/AbcSize
  modifier  = [modifier].flatten
  load_file = %i[file newest].any? {|flag| modifier.include?(flag)}

  if filename && load_file
    load_cache(filename, newest: modifier.include?(:newest)).tap do |people|
      next if people.is_a?(Eco::API::Organization::People)

      log(:error) {
        "could not find the file #{file_manager.dir.file(filename)}"
      }

      exit unless modifier.include?(:api)

      people_load(modifier: modifier - %i[newest file])
    end
  elsif modifier.include?(:api)
    people_cache(filename, modifier: modifier)
  end.then do |people|
    Eco::API::Organization::People.new(people.to_a)
  end
end