Class: HammerCLIForeman::IdResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli_foreman/id_resolver.rb

Constant Summary collapse

ALL_PER_PAGE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, searchables) ⇒ IdResolver

Returns a new instance of IdResolver.



87
88
89
90
91
# File 'lib/hammer_cli_foreman/id_resolver.rb', line 87

def initialize(api, searchables)
  @api = api
  @searchables = searchables
  define_id_finders
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



85
86
87
# File 'lib/hammer_cli_foreman/id_resolver.rb', line 85

def api
  @api
end

Instance Method Details

#scoped_options(scope, options, mode = nil) ⇒ Object

Parameters:

  • mode (Symbol) (defaults to: nil)

    mode in which ids are searched :single, :multi, nil for old beahvior



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hammer_cli_foreman/id_resolver.rb', line 94

def scoped_options(scope, options, mode = nil)
  scoped_options = options.dup

  resource = HammerCLIForeman.param_to_resource(scope)
  return scoped_options unless resource

  option_names = []
  if (mode.nil? || mode == :single)
    option_names += searchables(resource).map { |s| s.name }
    option_names << "id" unless option_names.include?("id")
  end
  if (mode.nil? || mode == :multi)
    option_names << "ids"
    option_names += searchables(resource).map { |s| s.plural_name }
  end
  option_names.each do |name|
    option = HammerCLI.option_accessor_name(name)
    scoped_option = HammerCLI.option_accessor_name("#{scope}_#{name}")
    # remove the scope
    # e.g. option_architecture_id -> option_id
    if scoped_options[scoped_option]
      scoped_options[option] = scoped_options.delete(scoped_option)
    else
      scoped_options.delete(option)
    end
  end
  scoped_options
end

#searchables(resource) ⇒ Object



123
124
125
126
# File 'lib/hammer_cli_foreman/id_resolver.rb', line 123

def searchables(resource)
  resource = @api.resource(resource) if resource.is_a? Symbol
  @searchables.for(resource)
end

#template_kind_id(options = {}) ⇒ Object



128
129
130
131
132
# File 'lib/hammer_cli_foreman/id_resolver.rb', line 128

def template_kind_id(options = {})
  name = options['option_type'] == 'snippet' ? nil : options['option_type']
  options['option_name'] = name
  get_id(:template_kinds, options)
end