Sha256: b60113f9320e3c96b63406d3733459baddf201ebfa4f4ec2e2d7cece3e54aa3a

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 KB

Contents

module HammerCLIForeman::Output
  module Formatters

    class SingleReferenceFormatter < HammerCLI::Output::Formatters::FieldFormatter

      def tags
        [:flat]
      end

      def format(resource, field_params={})
        return "" if resource.nil?

        key = field_params[:key]

        id_key = "#{key}_id"
        name_key = "#{key}_name"

        name = resource[name_key.to_sym] || resource[name_key]
        id = resource[id_key.to_sym] || resource[id_key]

        context = field_params[:context] || {}

        out = "#{name}"
        out += " (id: #{id})" if context[:show_ids] && id
        out
      end

    end

    class ReferenceFormatter < HammerCLI::Output::Formatters::FieldFormatter

      def tags
        [:flat]
      end

      def format(reference, field_params={})
        return "" if reference.nil?

        id_key = field_params[:id_key] || :id
        name_key = field_params[:name_key] || :name

        name = reference[name_key] || reference[name_key.to_s]
        id = reference[id_key] || reference[id_key.to_s]

        context = field_params[:context] || {}

        details = field_params[:details] || []
        details = [details] unless details.is_a? Array
        values = details.collect do |key|
          reference[key] || reference[key.to_s]
        end
        values << "id: #{id}" if context[:show_ids]

        if values.empty?
          "#{name}" if name
        else
          "#{name} (#{values.join(', ')})" if name && !values.empty?
        end
      end

    end

    HammerCLI::Output::Output.register_formatter(SingleReferenceFormatter.new, :SingleReference)
    HammerCLI::Output::Output.register_formatter(ReferenceFormatter.new, :Reference)
    HammerCLI::Output::Output.register_formatter(ReferenceFormatter.new, :Template)

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hammer_cli_foreman-0.9.0 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.8.0 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.7.0 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.6.2 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.6.1 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.6.0 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.5.1 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.5.0 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.4.0 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.3.0 lib/hammer_cli_foreman/output/formatters.rb
hammer_cli_foreman-0.2.0 lib/hammer_cli_foreman/output/formatters.rb