lib/eco/cli_default/people.rb in eco-helpers-3.0.20 vs lib/eco/cli_default/people.rb in eco-helpers-3.0.21

- old
+ new

@@ -1,70 +1,125 @@ -MAX_GET_PARTIAL = 12_000 +class Eco::CliDefault::People < Eco::API::Common::Loaders::CliConfig + MAX_GET_PARTIAL = 12_000 -ASSETS.cli.config do |cnf| - cnf.people do |input, session, options| - get = options.dig(:people, :get) - get = {} if get.nil? + class << self + attr_reader :options, :session + attr_writer :get_full, :get_partial - from_remote = get && get[:from] == :remote - from_local = get && get[:from] == :local + def get_options # rubocop:disable Naming/AccessorMethodName + get = options.dig(:people, :get) + get = {} if get.nil? + get + end - get_full = from_remote && get[:type] == :full - get_partial = from_remote && get[:type] == :partial - get_by_file = from_local && get[:type] == :file + def no_get? + get_options == false + end - # -get-partial: validate input present and under max - if get_partial + def from_remote? + return false unless get_options + + get_options[:from] == :remote + end + + def from_local? + return false unless get_options + + get_options[:from] == :local + end + + def get_full? + return @get_full if instance_variable_defined?(:@get_full) + return false unless from_remote? + + get_options[:type] == :full + end + + def get_partial? + return @get_partial if instance_variable_defined?(:@get_partial) + return false unless from_remote? + + @get_partial = (get_options[:type] == :partial) + end + + def get_by_file? + return false unless from_local? + + get_options[:type] == :file + end + + def source_file + return unless get_by_file? + + get_options[:file] + end + + def switch_to_full_remote! + self.get_full = true + self.get_partial = false + + options.deep_merge!(people: { + get: { + from: :remote, + type: :full + } + }) + end + + def switch_to_full_local! + self.get_full = true + self.get_partial = false + + options.deep_merge!(people: { + get: { + from: :local, + type: :full + } + }) + end + + def optimize_get_partial!(input) + return unless get_partial? + msg = "To use -get-partial (partial updates), you need to use -entries-from" raise msg unless input.is_a?(Enumerable) - if input.count > MAX_GET_PARTIAL - get_full = true - get_partial = false + return unless input.count > MAX_GET_PARTIAL - msg = "(Optimization) " - msg << "Switching from partial to full people download. " - msg << "Input (#{input.count}) surpases MAX_GET_PARTIAL " - msg << "(#{MAX_GET_PARTIAL}) entries." - session.log(:info) { msg } + msg = "(Optimization) " + msg << "Switching from partial to full people download. " + msg << "Input (#{input.count}) surpases MAX_GET_PARTIAL " + msg << "(#{MAX_GET_PARTIAL}) entries." + session.log(:info) { msg } - options.deep_merge!(people: { - get: { - from: :remote, - type: :full - } - }) - end + switch_to_full_remote! end + end - if get == false + people do |input, session, options| + @options = options + @session = session + + # -get-partial: validate input present and under max + optimize_get_partial!(input) if get_partial? + + if no_get? Eco::API::Organization::People.new([]) - elsif get_full + elsif get_full? # -get-people session.micro.people_cache - elsif get_partial + elsif get_partial? # -get-partial session.micro.people_search(input, options: options) - elsif get_by_file + elsif get_by_file? # -people-from-backup - session.micro.people_load(get[:file], modifier: :file) + session.micro.people_load(source_file, modifier: :file) else - options.deep_merge!(people: { - get: { - from: :local, - type: :full - } - }) - + switch_to_full_local! people = session.micro.people_load(modifier: %i[newest save]) if people.empty? - options.deep_merge!(people: { - get: { - from: :remote, - type: :full - } - }) + switch_to_full_remote! people = session.micro.people_cache end people end