Class: TableAnalysis::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/table_analysis/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, table, body_tds) ⇒ Core

Returns a new instance of Core.



9
10
11
12
13
14
15
16
# File 'lib/table_analysis/core.rb', line 9

def initialize(header, table, body_tds)
  @header = header
  @table = table
  @body_tds = body_tds
  @pointer = [0, 0]
  @x_max = table.size - 1
  @y_max = header.size - 1
end

Instance Attribute Details

#body_tdsObject

Returns the value of attribute body_tds.



7
8
9
# File 'lib/table_analysis/core.rb', line 7

def body_tds
  @body_tds
end

#headerObject

Returns the value of attribute header.



7
8
9
# File 'lib/table_analysis/core.rb', line 7

def header
  @header
end

#pointerObject

Returns the value of attribute pointer.



7
8
9
# File 'lib/table_analysis/core.rb', line 7

def pointer
  @pointer
end

#tableObject

Returns the value of attribute table.



7
8
9
# File 'lib/table_analysis/core.rb', line 7

def table
  @table
end

#x_maxObject

Returns the value of attribute x_max.



7
8
9
# File 'lib/table_analysis/core.rb', line 7

def x_max
  @x_max
end

#y_maxObject

Returns the value of attribute y_max.



7
8
9
# File 'lib/table_analysis/core.rb', line 7

def y_max
  @y_max
end

Instance Method Details

#entranceObject

入场



19
20
21
22
23
24
25
# File 'lib/table_analysis/core.rb', line 19

def entrance
  @body_tds.each do |body_td|
    current_seat_position = seat_down
    reserved_seat(current_seat_position, body_td.rowspan, body_td.colspan) if body_td.reserved_seat?
  end
  @table
end

#is_unreserved_seat?Boolean

空座位

Returns:

  • (Boolean)


28
29
30
# File 'lib/table_analysis/core.rb', line 28

def is_unreserved_seat?
  @table[@pointer[0]][@pointer[1]].nil? ? true : false
end

#pointer_increaseObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/table_analysis/core.rb', line 63

def pointer_increase
  if @pointer[1] < @y_max
    @pointer[1] += 1
  elsif @pointer[1] == @y_max && @pointer[0] < @x_max
    @pointer[1] = 0
    @pointer[0] += 1
  elsif @pointer[1] > @y_max && @pointer[0] > @x_max
    raise 'header_start_row取值错误'
  end
end

#reserved_seat(current_seat_position, rowspan, colspan) ⇒ Object

占座



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/table_analysis/core.rb', line 46

def reserved_seat(current_seat_position, rowspan, colspan)
  (rowspan - 1).times do |n|
    @table[current_seat_position[0] + n + 1][current_seat_position[1]] = -1
  end
  (colspan - 1).times do |m|
    @table[current_seat_position[0]][current_seat_position[1] + m + 1] = -1
    (rowspan - 1).times do |n|
      @table[current_seat_position[0] + n + 1][current_seat_position[1] + m + 1] = -1
    end
  end
end

#seat_downObject

坐下



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/table_analysis/core.rb', line 33

def seat_down
  if is_unreserved_seat?
    @table[@pointer[0]][@pointer[1]] = seat_down_value
    current_seat_position = @pointer.dup
    pointer_increase
    current_seat_position
  else
    pointer_increase
    seat_down
  end
end

#seat_down_valueObject

身份值



59
60
61
# File 'lib/table_analysis/core.rb', line 59

def seat_down_value
  @header[@pointer[1]]
end