Class: HammerCLIForeman::DependentSearchablesArrayOptionBuilder

Inherits:
DependentSearchablesOptionBuilder show all
Defined in:
lib/hammer_cli_foreman/option_builders.rb

Instance Attribute Summary

Attributes inherited from DependentSearchablesOptionBuilder

#resource

Instance Method Summary collapse

Methods inherited from DependentSearchablesOptionBuilder

#build, #initialize

Constructor Details

This class inherits a constructor from HammerCLIForeman::DependentSearchablesOptionBuilder

Instance Method Details

#dependent_options(resource, resource_name_map, command) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/hammer_cli_foreman/option_builders.rb', line 350

def dependent_options(resource, resource_name_map, command)
  options = []
  searchables = @searchables.for(resource)
  resource_name = resource.singular_name

  aliased_name = aliased(resource_name, resource_name_map)
  aliased_plural_name = aliased(resource.name, resource_name_map)
  unless searchables.empty?
    first = searchables[0]
    remaining = searchables[1..-1] || []
    # Find family created by HammerCLI::Apipie::OptionBuilder for *_ids options
    family = command.option_families.find do |f|
      f.head.referenced_resource == resource_name && f.formats.include?(HammerCLI::Options::Normalizers::List)
    end
    # First option is named by the resource
    # Eg. --organizations with accessor option_organization_names
    options << family.child(
      optionamize("--#{aliased_plural_name}"),
      "#{aliased_name}_#{first.plural_name}".upcase,
      ' ',
      attribute_name: HammerCLI.option_accessor_name("#{resource_name}_#{first.plural_name}"),
      format: HammerCLI::Options::Normalizers::List.new,
      referenced_resource: resource_name,
      aliased_resource: aliased_name
    )

    # Other options are named by the resource plus the searchable name
    # Eg. --organization-labels with accessor option_organization_labels
    remaining.each do |s|
      options << family.child(
        optionamize("--#{aliased_name}-#{s.plural_name}"),
        "#{aliased_name}_#{s.plural_name}".upcase,
        ' ',
        attribute_name: HammerCLI.option_accessor_name("#{resource_name}_#{s.plural_name}"),
        format: HammerCLI::Options::Normalizers::List.new,
        referenced_resource: resource_name,
        aliased_resource: aliased_name
      )
    end
  end

  options
end