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



50
51
52
# File 'motion-prime/services/table_data_indexes.rb', line 50

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

#flat_data?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'motion-prime/services/table_data_indexes.rb', line 58

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

#greater_than(a, b) ⇒ Object



17
18
19
# File 'motion-prime/services/table_data_indexes.rb', line 17

def greater_than(a, b)
  compare_indexes(a, b) > 0
end

#less_than(a, b) ⇒ Object



21
22
23
# File 'motion-prime/services/table_data_indexes.rb', line 21

def less_than(a, b)
  compare_indexes(a, b) < 0
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



54
55
56
# File 'motion-prime/services/table_data_indexes.rb', line 54

def sections_count
  flat_data? ? 1 : data.count
end

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'motion-prime/services/table_data_indexes.rb', line 25

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

    max_section = sections_count - 1
    if section < 0 || section > max_section
      edge_section = [[section, 0].max, max_section].min
      return crop_to_edges ? NSIndexPath.indexPathForRow(edge_row, inSection: edge_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