Class: HammerCLIForeman::OptionSources::IdsParams

Inherits:
HammerCLI::Options::Sources::Base
  • Object
show all
Defined in:
lib/hammer_cli_foreman/option_sources/ids_params.rb

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ IdsParams

Returns a new instance of IdsParams.



4
5
6
# File 'lib/hammer_cli_foreman/option_sources/ids_params.rb', line 4

def initialize(command)
  @command = command
end

Instance Method Details

#available_ids_paramsObject



12
13
14
# File 'lib/hammer_cli_foreman/option_sources/ids_params.rb', line 12

def available_ids_params
  IdArrayParamsFilter.new(:only_required => false).for_action(@command.resource.action(@command.action))
end

#get_options(_defined_options, result) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hammer_cli_foreman/option_sources/ids_params.rb', line 38

def get_options(_defined_options, result)
  # resolve all '<resource_name>_ids' parameters if they are defined as options
  return result if @command.action.nil?

  available_ids_params.each do |api_param|
    param_resource = HammerCLIForeman.param_to_resource(api_param.name)
    param_option = HammerCLI.option_accessor_name(api_param.name)
    next unless needs_resolving?(param_option, param_resource, result)

    resource_ids = @command.get_resource_ids(
      param_resource, scoped: true, required: api_param.required?, all_options: result
    )
    result[param_option] = resource_ids if resource_ids
  end
  result
end

#needs_resolving?(param_option, param_resource, all_opts) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hammer_cli_foreman/option_sources/ids_params.rb', line 16

def needs_resolving?(param_option, param_resource, all_opts)
  return false unless param_updatable?(param_resource)

  cli_searchables_set = @command.searchables.for(param_resource).any? do |s|
    option = HammerCLI.option_accessor_name("#{param_resource.singular_name}_#{s.plural_name}")
    next false unless @command.respond_to?(option)

    !@command.send(option).nil?
  end
  if cli_searchables_set
    # Remove set '<resource_name>_ids' option to force resolving in case of
    # '<resource_name>_[names|titles]' were set from CLI
    all_opts.delete(param_option)
    true
  else
    # Don't resolve if resource ids were set via CLI
    return false if @command.send(cli_option_resource_ids(param_resource))

    all_opts[param_option].nil?
  end
end

#param_updatable?(param_resource) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/hammer_cli_foreman/option_sources/ids_params.rb', line 8

def param_updatable?(param_resource)
  param_resource && @command.respond_to?(cli_option_resource_ids(param_resource))
end