Sha256: a30ca78f4faeaf338f244e3b0f15647848676ddc569047155d30a58b85cc21d0

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

require 'dry-initializer'
require 'gm/notepad/container'

module Gm
  module Notepad
    TABLE_ENTRY_RANGE_MARKER = "-".freeze
    class TableEntry
      extend Dry::Initializer
      option :line, proc(&:to_s)
      option :table
      option :column_delimiter, default: -> { Container.resolve(:config).column_delimiter }

      def initialize(*args)
        super
        row = line.split(column_delimiter)
        self.index = row.shift
        self.cells = row
      end

      include Comparable
      def <=>(other)
        to_str <=> String(other)
      end

      def lookup(cell:)
        index = table.column_index_for(cell: cell)
        cells[index] || cells[0]
      end

      NUMBER_RANGE_REGEXP = %r{(?<left>\d+) *- *(?<right>\d+)}
      def lookup_range
        if match = NUMBER_RANGE_REGEXP.match(index)
          (match[:left].to_i..match[:right].to_i).map(&:to_s)
        else
          [index]
        end
      end

      attr_reader :index, :cells

      def entry
        cells.join("\t")
      end
      alias entry_column entry

      def to_s
        "[#{index}]\t#{entry}"
      end
      alias to_str entry

      private

      def index=(input)
        @index = input.strip.downcase.freeze
      end

      def cells=(input)
        @cells = Array(input).map { |i| i.strip.freeze }.freeze
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gm-notepad-0.0.16 lib/gm/notepad/table_entry.rb
gm-notepad-0.0.15 lib/gm/notepad/table_entry.rb
gm-notepad-0.0.14 lib/gm/notepad/table_entry.rb
gm-notepad-0.0.13 lib/gm/notepad/table_entry.rb
gm-notepad-0.0.12 lib/gm/notepad/table_entry.rb
gm-notepad-0.0.11 lib/gm/notepad/table_entry.rb