class HammerCLIForeman::Output::Formatters::ReferenceFormatter
Public Instance Methods
format(data, field_params={})
click to toggle source
Parameters: :display_field_key - key where the formmatter will look for the main field to display, default is :name :details - detail fields to be displayed
example format: :details => [ { :label => _('Type'), :key => :provider_friendly_name, :structured_label => _('Type') }, { :label => _('Id'), :key => :id } ]
# File lib/hammer_cli_foreman/output/formatters.rb, line 16 def format(data, field_params={}) return "" if data.nil? name = get_value(data, field_params[:display_field_key] || :name) context = field_params[:context] || {} details = format_details(data, field_params[:details] || [], context[:show_ids]) if details.empty? "#{name}" if name else "#{name} (#{details.join(', ')})" if name end end
required_features()
click to toggle source
# File lib/hammer_cli_foreman/output/formatters.rb, line 4 def required_features %i[serialized] end
Protected Instance Methods
format_details(data, details, show_ids)
click to toggle source
# File lib/hammer_cli_foreman/output/formatters.rb, line 31 def format_details(data, details, show_ids) details = [details] unless details.is_a?(Array) details.map do |detail| if detail.is_a?(Hash) next if detail[:id] && !show_ids if detail[:label] "#{detail[:label]}: #{get_value(data, detail[:key])}" else get_value(data, detail[:key]) end else get_value(data, detail) end end.compact end
get_value(data, key)
click to toggle source
# File lib/hammer_cli_foreman/output/formatters.rb, line 48 def get_value(data, key) data[key] || data[key.to_s] end