Class: Gm::Notepad::Table

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

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, lines:, filename: nil) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
# File 'lib/gm/notepad/table.rb', line 7

def initialize(table_name:, lines:, filename: nil)
  self.table_name = table_name
  self.filename = filename
  process(lines: lines)
end

Instance Method Details

#allObject



21
22
23
# File 'lib/gm/notepad/table.rb', line 21

def all
  @table.values
end

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



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

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



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

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



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

def lookup(index: false)
  if index
    @table.fetch(index)
  else
    @table.values[random_index]
  end
end