Class: Canis::Circular

Inherits:
Struct
  • Object
show all
Defined in:
lib/canis/core/widgets/table.rb

Overview

a structure that maintains position and gives next and previous taking max index into account. it also circles. Can be used for traversing next component in a form, or container, or columns in a table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, c = 0) ⇒ Circular

Returns a new instance of Circular.



53
54
55
56
57
58
# File 'lib/canis/core/widgets/table.rb', line 53

def initialize  m, c=0
  raise "max index cannot be nil" unless m
  @max_index = m
  @current_index = c
  @last_index = c
end

Instance Attribute Details

#current_indexObject

Returns the value of attribute current_index

Returns:

  • (Object)

    the current value of current_index



50
51
52
# File 'lib/canis/core/widgets/table.rb', line 50

def current_index
  @current_index
end

#last_indexObject (readonly)

Returns the value of attribute last_index.



51
52
53
# File 'lib/canis/core/widgets/table.rb', line 51

def last_index
  @last_index
end

#max_indexObject

Returns the value of attribute max_index

Returns:

  • (Object)

    the current value of max_index



50
51
52
# File 'lib/canis/core/widgets/table.rb', line 50

def max_index
  @max_index
end

Instance Method Details

#is_last?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/canis/core/widgets/table.rb', line 75

def is_last?
  @current_index == @max_index
end

#nextObject



59
60
61
62
63
64
65
66
# File 'lib/canis/core/widgets/table.rb', line 59

def next
  @last_index = @current_index
  if @current_index + 1 > @max_index
    @current_index = 0
  else
    @current_index += 1
  end
end

#previousObject



67
68
69
70
71
72
73
74
# File 'lib/canis/core/widgets/table.rb', line 67

def previous
  @last_index = @current_index
  if @current_index - 1 < 0
    @current_index = @max_index
  else
    @current_index -= 1
  end
end