Sha256: ef994b40ff49c1e056c80e8cb8900108ef4cbc3a9d35f767aace2e0a302b12c9

Contents?: true

Size: 882 Bytes

Versions: 10

Compression:

Stored size: 882 Bytes

Contents

module Cp8Cli
  class Table
    class Row
      attr_reader :num

      COLOR_TRANSLATIONS = {
        "purple" => "magenta",
        "orange" => "yellow",
        "sky" => "cyan",
        "pink" => "red",
        "lime" => "green"
      }

      def initialize(record, index)
        @record = record
        @num = index + 1
      end

      def to_h
        result = { "#": num }
        fields.each do |field|
          result[field] = colorize record.send(field)
        end
        result
      end

      private

        attr_reader :num, :record

        def colorize(str)
          return str unless record.respond_to?(:color)
          "■".send(color) + " #{str}"
        end

        def color
          COLOR_TRANSLATIONS[record.color] || record.color || :white
        end

        def fields
          @_fields ||= record.class.fields
        end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cp8_cli-6.0.1 lib/cp8_cli/table/row.rb
cp8_cli-6.0.0 lib/cp8_cli/table/row.rb
cp8_cli-5.0.0 lib/cp8_cli/table/row.rb
cp8_cli-4.2.1 lib/cp8_cli/table/row.rb
cp8_cli-4.2.0 lib/cp8_cli/table/row.rb
cp8_cli-4.1.3 lib/cp8_cli/table/row.rb
cp8_cli-4.1.2 lib/cp8_cli/table/row.rb
cp8_cli-4.1.1 lib/cp8_cli/table/row.rb
cp8_cli-4.1.0 lib/cp8_cli/table/row.rb
cp8_cli-4.0.1 lib/cp8_cli/table/row.rb