Sha256: 95c9ddec8c66c33b4f862e38d69367bdb157c9330b19d78b39365ddb99f9fe35

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module HammerCLI::Output::Adapter

  class Abstract

    def tags
      []
    end

    def initialize(context={}, formatters={})
      @context = context
      @formatters = HammerCLI::Output::Formatters::FormatterLibrary.new(filter_formatters(formatters))
    end

    def print_message(msg, msg_params={})
      puts msg.format(msg_params)
    end

    def print_error(msg, details=nil, msg_params={})
      details = details.split("\n") if details.kind_of? String

      if details
        indent = "  "
        msg += ":\n"
        msg += indent + details.join("\n"+indent)
      end

      $stderr.puts msg.format(msg_params)
    end

    def print_record(fields, record)
      raise NotImplementedError
    end

    def print_collection(fields, collection)
      raise NotImplementedError
    end

    protected

    def field_filter
      HammerCLI::Output::FieldFilter.new
    end

    def data_for_field(field, record)
      path = field.path

      path.inject(record) do |record, path_key|
        if record.has_key? path_key.to_sym
          record[path_key.to_sym]
        elsif record.has_key? path_key.to_s
          record[path_key.to_s]
        else
          return nil
        end
      end
    end

    private

    def filter_formatters(formatters_map)
      formatters_map ||= {}
      formatters_map.inject({}) do |map, (type, formatter_list)|
        # remove incompatible formatters
        filtered = formatter_list.select { |f| f.match?(tags) }
        # put serializers first
        map[type] = filtered.sort_by { |f| f.tags.include?(:flat) ? 0 : 1 }
        map
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hammer_cli-0.1.0 lib/hammer_cli/output/adapter/abstract.rb