Sha256: bfb3562bb0c2feb22052f8078ff1977ccad45b689ee5b3d65d56f2852c7948dd

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require "gm/notepad/exceptions"
require "gm/notepad/table_entry"

module Gm
  module Notepad
    class Table
      Configuration.init!(target: self, additional_params: [:table_name, :filename, :lines]) do
        process(lines: lines)
      end

      def lookup(index: false)
        if index
          begin
            @table.fetch(index.to_s)
          rescue KeyError
            raise MissingTableEntryError.new(table_name: table_name, index: index.to_s)
          end
        else
          @table.values[random_index]
        end
      end

      def all
        @table.values.uniq
      end

      def grep(expression)
        returning_value = []
        @table.each_value do |entry|
          if expression.match(entry.entry_column)
            returning_value << entry
          end
        end
        returning_value
      end

      def append(line:, write: false)
        process(lines: [line])
        return unless filename
        return unless write
        File.open(filename, "a") do |file|
          file.puts(line)
        end
      end

      private

      attr_accessor :filename, :config
      attr_reader :table_name

      def table_name=(input)
        @table_name = input.downcase
      end

      def random_index
        rand(@table.size)
      end

      def process(lines:)
        @table = {}
        lines.each do |line|
          next if line[0] == '#'
          entry = TableEntry.new(line: line, **config)
          entry.lookup_range.each do |i|
            key = i.to_s
            raise DuplicateKeyError.new(key: table_name, object: self) if @table.key?(key)
            @table[key] = entry
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gm-notepad-0.0.6 lib/gm/notepad/table.rb