Class: TableDataIndexes

Inherits:
Object
  • Object
show all
Defined in:
motion-prime/services/table_data_indexes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TableDataIndexes

Returns a new instance of TableDataIndexes.



4
5
6
# File 'motion-prime/services/table_data_indexes.rb', line 4

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'motion-prime/services/table_data_indexes.rb', line 2

def data
  @data
end

Instance Method Details

#compare_indexes(a, b) ⇒ Object



12
13
14
15
# File 'motion-prime/services/table_data_indexes.rb', line 12

def compare_indexes(a, b)
  return 0 if a == b
  a.section > b.section || a.row > b.row ? 1 : -1
end

#count_in_section(id) ⇒ Object



40
41
42
# File 'motion-prime/services/table_data_indexes.rb', line 40

def count_in_section(id)
  flat_data? ? data.count : data[id].count
end

#flat_data?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'motion-prime/services/table_data_indexes.rb', line 48

def flat_data?
  !data.first.is_a?(Array)
end

#max_index(*indexes) ⇒ Object



8
9
10
# File 'motion-prime/services/table_data_indexes.rb', line 8

def max_index(*indexes)
  [*indexes].compact.max &method(:compare_indexes)
end

#sections_countObject



44
45
46
# File 'motion-prime/services/table_data_indexes.rb', line 44

def sections_count
  flat_data? ? 1 : data.count
end

#sum_index(a, rows, crop_to_edges = true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'motion-prime/services/table_data_indexes.rb', line 17

def sum_index(a, rows, crop_to_edges = true)
  row = a.row + rows
  section = a.section

  max_row = count_in_section(a.section) - 1
  if row < 0 || row > max_row
    direction = row < 0 ? -1 : 1

    section = a.section + direction
    edge_row = [[0, row].max, max_row].min

    if section < 0 || section >= sections_count
      return crop_to_edges ? NSIndexPath.indexPathForRow(edge_row, inSection: a.section) : false
    end

    start_row = edge_row.zero? ? count_in_section(section) - 1 : 0
    rows_left = rows - (edge_row - a.row) - direction
    sum_index(NSIndexPath.indexPathForRow(start_row, inSection: section), rows_left)
  else
    NSIndexPath.indexPathForRow(row, inSection: section)
  end
end