Class: HammerCLIForeman::ForemanOptionBuilder

Inherits:
HammerCLI::OptionBuilderContainer
  • Object
show all
Defined in:
lib/hammer_cli_foreman/option_builders.rb

Instance Method Summary collapse

Constructor Details

#initialize(searchables) ⇒ ForemanOptionBuilder

Returns a new instance of ForemanOptionBuilder.



98
99
100
# File 'lib/hammer_cli_foreman/option_builders.rb', line 98

def initialize(searchables)
  @searchables = searchables
end

Instance Method Details

#build(builder_params = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hammer_cli_foreman/option_builders.rb', line 102

def build(builder_params={})
  expansion_options = builder_params[:expand] || {}

  allowed_resources = expansion_options[:only] || default_dependent_resources
  allowed_resources -= expansion_options[:except] || []
  allowed_resources += expansion_options[:including] || []
  allowed_resources.uniq!

  primary_resource = expansion_options[:primary]

  to_remove = default_dependent_resources - allowed_resources
  to_add = allowed_resources - default_dependent_resources

  builders.reject! do |b|
    b.class <= DependentSearchablesOptionBuilder && to_remove.include?(b.resource.name)
  end
  to_add.each do |resource_name|
    builders << DependentSearchablesOptionBuilder.new(HammerCLIForeman.foreman_resource(resource_name), @searchables)
  end

  if !primary_resource.nil?
    builders.reject! do |b|
      b.class <= SearchablesOptionBuilder
    end
    builders << SearchablesOptionBuilder.new(HammerCLIForeman.foreman_resource(primary_resource), @searchables) if primary_resource != false
  end

  super
end

#default_dependent_resourcesObject



132
133
134
# File 'lib/hammer_cli_foreman/option_builders.rb', line 132

def default_dependent_resources
  dependent_searchable_builders.map(&:resource).map(&:name)
end

#dependent_searchable_buildersObject



136
137
138
# File 'lib/hammer_cli_foreman/option_builders.rb', line 136

def dependent_searchable_builders
  self.builders.select{|b| b.class <= DependentSearchablesOptionBuilder }
end