Class: TerminalCalendar::Selection::Selector
- Inherits:
-
Object
- Object
- TerminalCalendar::Selection::Selector
- Extended by:
- Forwardable
- Defined in:
- lib/terminal_calendar/selection/selector.rb
Constant Summary collapse
- DIRECTIONS =
%i(up down left right).freeze
Instance Attribute Summary collapse
-
#selection_grid ⇒ Object
readonly
Returns the value of attribute selection_grid.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(x, y, page, wrap: []) ⇒ Selector
constructor
Initializes a new selector.
-
#leftmost_gridsquare ⇒ Integer
Returns the leftmost grid square.
-
#move(direction) ⇒ void
Moves the selector in the specified direction.
-
#on_grid? ⇒ Boolean
Determines if the selector is within the selection grid.
- #on_header? ⇒ Boolean
-
#toggle_selected! ⇒ void
Toggles the selected state of the cell at the current position on the grid.
Constructor Details
#initialize(x, y, page, wrap: []) ⇒ Selector
Initializes a new selector
31 32 33 34 35 36 37 38 |
# File 'lib/terminal_calendar/selection/selector.rb', line 31 def initialize(x, y, page, wrap: []) @x = x @page = page @top_of_grid = 0 @y = y @wrap_directions = wrap == :all ? DIRECTIONS : wrap post_move end |
Instance Attribute Details
#selection_grid ⇒ Object (readonly)
Returns the value of attribute selection_grid.
10 11 12 |
# File 'lib/terminal_calendar/selection/selector.rb', line 10 def selection_grid @selection_grid end |
#x ⇒ Object
Returns the value of attribute x.
10 11 12 |
# File 'lib/terminal_calendar/selection/selector.rb', line 10 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
10 11 12 |
# File 'lib/terminal_calendar/selection/selector.rb', line 10 def y @y end |
Class Method Details
.build(page, initial_spot) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/terminal_calendar/selection/selector.rb', line 15 def self.build(page, initial_spot) if initial_spot == :bottom x, y = page.selection_grid.bottom_right_live_cell_position else x, y = page.selection_grid.top_left_live_cell_position end new(x, y, page) end |
Instance Method Details
#leftmost_gridsquare ⇒ Integer
Returns the leftmost grid square.
64 65 66 |
# File 'lib/terminal_calendar/selection/selector.rb', line 64 def leftmost_gridsquare 0 end |
#move(direction) ⇒ void
This method returns an undefined value.
Moves the selector in the specified direction.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/terminal_calendar/selection/selector.rb', line 74 def move(direction) fail ArgumentError.new("Unknown direction #{direction}") unless DIRECTIONS.include?(direction) pre_move result = send("move_#{direction}") post_move result end |
#on_grid? ⇒ Boolean
Determines if the selector is within the selection grid.
56 57 58 59 |
# File 'lib/terminal_calendar/selection/selector.rb', line 56 def on_grid? x >= leftmost_gridsquare && x <= selection_grid.row_end && y >= selection_grid.top_of_grid && y <= selection_grid.bottom_of_grid end |
#on_header? ⇒ Boolean
40 41 42 |
# File 'lib/terminal_calendar/selection/selector.rb', line 40 def on_header? y == -1 end |
#toggle_selected! ⇒ void
This method returns an undefined value.
Toggles the selected state of the cell at the current position on the grid.
47 48 49 50 51 |
# File 'lib/terminal_calendar/selection/selector.rb', line 47 def toggle_selected! return unless on_grid? selection_grid.cell(@x, @y).toggle_selected! end |