Module: HammerCLIKatello::IdNameOptionsValidator

Included in:
Filter::DeleteCommand, Filter::InfoCommand, Filter::ListCommand, Filter::UpdateCommand
Defined in:
lib/hammer_cli_katello/id_name_options_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_child_options(record_name) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/hammer_cli_katello/id_name_options_validator.rb', line 47

def self.build_child_options(record_name)
  if record_name.nil?
    %w(option_id option_name)
  else
    IdNameOptionsValidator.build_options(record_name, %w(id name))
  end
end

.build_options(record_name, options) ⇒ Object



67
68
69
70
71
# File 'lib/hammer_cli_katello/id_name_options_validator.rb', line 67

def self.build_options(record_name, options)
  options.map do |opt|
    "option_#{record_name}_#{opt}"
  end
end

.build_parent_options(parent) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hammer_cli_katello/id_name_options_validator.rb', line 55

def self.build_parent_options(parent)
  if parent.is_a?(String) || parent.is_a?(Symbol)
    opts = parent.to_s == 'organization' ? %w(id name label) : %w(id name)
  elsif parent.is_a?(Hash)
    parent, opts = parent.first.to_a
  else
    raise "Unkown parent class: #{parent.class} for #{parent}"
  end

  IdNameOptionsValidator.build_options(parent, opts)
end

Instance Method Details

#validate_id_or_name(record_name = nil) ⇒ Object

This method simply checks that either id or name is supplied

Some examples:

# checks for a –id or –name option validate_id_or_name # checks for –content-view-id or –content-view-name validate_id_or_name :content_view



39
40
41
42
43
44
45
# File 'lib/hammer_cli_katello/id_name_options_validator.rb', line 39

def validate_id_or_name(record_name = nil)
  child_options = IdNameOptionsValidator.build_child_options(record_name)

  validate_options do
    any(*child_options).required
  end
end

#validate_id_or_name_with_parent(record_name = nil, parent: 'organization', required: true) ⇒ Object

This is a method that requires:

  1. either name or id has been supplied

  2. if name is supplied, parent id/name/etc is also required

Normally the parent will be organization as with products, content views, etc but this could also apply to repos for example whose names are unique per product

Some examples:

validate_id_or_name_with_parent validate_id_or_name_with_parent :content_view validate_id_or_name_with_parent :repository, parent: ‘product’, required: false validate_id_or_name_with_parent :content_view, parent: [‘id’, ‘name’]



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hammer_cli_katello/id_name_options_validator.rb', line 17

def validate_id_or_name_with_parent(record_name = nil, parent: 'organization', required: true)
  child_options = IdNameOptionsValidator.build_child_options(record_name)
  parent_options = IdNameOptionsValidator.build_parent_options(parent)

  validate_options do
    any(*child_options).required if required

    if (name_option = child_options.detect { |opt| opt.end_with?('_name') }) &&
       option(name_option).exist?
      any(*parent_options).required
    end
  end
end