Class: Gm::Notepad::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/gm/notepad/table.rb

Instance Method Summary collapse

Instance Method Details

#allObject



23
24
25
# File 'lib/gm/notepad/table.rb', line 23

def all
  @table.values.uniq
end

#append(line:, write: false) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/gm/notepad/table.rb', line 37

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

#grep(expression) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/gm/notepad/table.rb', line 27

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

#lookup(index: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gm/notepad/table.rb', line 11

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