Class: Griddle::DataGrid

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/griddle/data_grid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_counting_at_zero = false) ⇒ DataGrid

Returns a new instance of DataGrid.



14
15
16
17
18
# 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

Instance Attribute Details

#case_sensitiveObject

Returns the value of attribute case_sensitive.



8
9
10
# File 'lib/griddle/data_grid.rb', line 8

def case_sensitive
  @case_sensitive
end

#gridObject

Returns the value of attribute grid.



8
9
10
# File 'lib/griddle/data_grid.rb', line 8

def grid
  @grid
end

#offsetObject

Returns the value of attribute offset.



8
9
10
# File 'lib/griddle/data_grid.rb', line 8

def offset
  @offset
end

Class Method Details

.from_csv(csv) ⇒ Object



10
11
12
# File 'lib/griddle/data_grid.rb', line 10

def self.from_csv(csv)
  DataGrid.new.populate_from_csv(csv)
end

Instance Method Details

#cut(top, left, width, height) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



93
94
95
# File 'lib/griddle/data_grid.rb', line 93

def cut_rectangle(rectangle)
  cut(rectangle.top, rectangle.left, rectangle.width, rectangle.height)
end

#each(&block) ⇒ Object

Enumerable



33
34
35
36
37
# File 'lib/griddle/data_grid.rb', line 33

def each(&block)
  grid.each do |row|
    block.call(row)
  end
end

#find(what) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# 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

#heightObject



28
29
30
# File 'lib/griddle/data_grid.rb', line 28

def height
  grid.size
end

#offset_down(*args) ⇒ Object



97
98
99
# File 'lib/griddle/data_grid.rb', line 97

def offset_down(*args)
  args.collect {|n| n - offset}
end

#offset_up(*args) ⇒ Object



101
102
103
# File 'lib/griddle/data_grid.rb', line 101

def offset_up(*args)
  args.collect {|n| n + offset}
end

#populate_from_csv(csv_file_path) ⇒ Object



43
44
45
46
# 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) ⇒ Object



48
49
50
51
# File 'lib/griddle/data_grid.rb', line 48

def populate_from_data(data)
  self.grid = data
  self
end

#to_rectangleObject



20
21
22
# File 'lib/griddle/data_grid.rb', line 20

def to_rectangle
  Rectangle.new(0, 0, width, height)
end

#to_regex(query) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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_sObject



39
40
41
# File 'lib/griddle/data_grid.rb', line 39

def to_s
  Terminal::Table.new(rows: grid)
end

#widthObject



24
25
26
# File 'lib/griddle/data_grid.rb', line 24

def width
  grid[0].size
end