class Griddle::DataGrid
Attributes
case_sensitive[RW]
grid[RW]
offset[RW]
Public Class Methods
from_csv(csv)
click to toggle source
# File lib/griddle/data_grid.rb, line 10 def self.from_csv(csv) DataGrid.new.populate_from_csv(csv) end
new(start_counting_at_zero=false)
click to toggle source
# File lib/griddle/data_grid.rb, line 14 def initialize(start_counting_at_zero=false) @offset = start_counting_at_zero ? 0 : 1 @grid = [] @case_sensitive = false end
Public Instance Methods
cut(top, left, width, height)
click to toggle source
# File lib/griddle/data_grid.rb, line 76 def cut(top, left, width, height) top, left = offset_down(top, left) selection = [] for row in top...(top+height) row_data = [] for col in left...(left+width) row_data << grid[row][col] end selection << row_data end DataGrid.new.populate_from_data(selection) end
cut_rectangle(rectangle)
click to toggle source
# File lib/griddle/data_grid.rb, line 93 def cut_rectangle(rectangle) cut(rectangle.top, rectangle.left, rectangle.width, rectangle.height) end
each(&block)
click to toggle source
Enumerable
# File lib/griddle/data_grid.rb, line 33 def each(&block) grid.each do |row| block.call(row) end end
find(what)
click to toggle source
# File lib/griddle/data_grid.rb, line 63 def find(what) what = to_regex(what) matches = [] grid.each_with_index do |row, row_index| row.each_index.select {|n| what.match(row[n])}.each do |column| matches << Point.new(*offset_up(row_index, column)) end end matches end
height()
click to toggle source
# File lib/griddle/data_grid.rb, line 28 def height grid.size end
offset_down(*args)
click to toggle source
# File lib/griddle/data_grid.rb, line 97 def offset_down(*args) args.collect {|n| n - offset} end
offset_up(*args)
click to toggle source
# File lib/griddle/data_grid.rb, line 101 def offset_up(*args) args.collect {|n| n + offset} end
populate_from_csv(csv_file_path)
click to toggle source
# File lib/griddle/data_grid.rb, line 43 def populate_from_csv(csv_file_path) CSV.foreach(csv_file_path) {|row| self.grid << row} self end
populate_from_data(data)
click to toggle source
# File lib/griddle/data_grid.rb, line 48 def populate_from_data(data) self.grid = data self end
to_rectangle()
click to toggle source
# File lib/griddle/data_grid.rb, line 20 def to_rectangle Rectangle.new(0, 0, width, height) end
to_regex(query)
click to toggle source
# File lib/griddle/data_grid.rb, line 53 def to_regex(query) return query if query.is_a? Regexp if case_sensitive /#{Regexp.escape(query)}/ else /#{Regexp.escape(query)}/i end end
to_s()
click to toggle source
# File lib/griddle/data_grid.rb, line 39 def to_s Terminal::Table.new(rows: grid) end
width()
click to toggle source
# File lib/griddle/data_grid.rb, line 24 def width grid[0].size end