Class: TerminalCalendar::Selection::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal_calendar/selection/cell.rb

Direct Known Subclasses

NullCell

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(underlying_object, selected: false, selected_style: DEFAULT_SELECTED_STYLE) ⇒ Cell

Returns a new instance of Cell.



22
23
24
25
26
# File 'lib/terminal_calendar/selection/cell.rb', line 22

def initialize(underlying_object, selected: false, selected_style: DEFAULT_SELECTED_STYLE)
  @underlying_object = underlying_object
  @selected = selected
  @selected_style = selected_style
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Calls the missing method on the underlying object.

Parameters:

  • method (Symbol)

    the name of the missing method

  • args (Array)

    the arguments passed to the missing method

  • block (Proc)

    the block passed to the missing method

Returns:

  • (Object)

    the result of calling the missing method on the underlying object

Raises:

  • (NoMethodError)

    if the underlying object does not respond to the missing method



60
61
62
# File 'lib/terminal_calendar/selection/cell.rb', line 60

def method_missing(method, *args, &block)
  underlying_object.send(method, *args, &block)
end

Instance Attribute Details

#selectedBoolean (readonly) Also known as: selected?

Returns whether this cell is currently selected.

Returns:

  • (Boolean)

    whether this cell is currently selected



10
11
12
# File 'lib/terminal_calendar/selection/cell.rb', line 10

def selected
  @selected
end

#selected_styleString (readonly)

Returns the rendered style of a cell that is selected.

Returns:

  • (String)

    the rendered style of a cell that is selected



12
13
14
# File 'lib/terminal_calendar/selection/cell.rb', line 12

def selected_style
  @selected_style
end

#underlying_objectObject (readonly)

Returns the object that this cell wraps.

Returns:

  • (Object)

    the object that this cell wraps



8
9
10
# File 'lib/terminal_calendar/selection/cell.rb', line 8

def underlying_object
  @underlying_object
end

Class Method Details

.build(obj) ⇒ Cell

Builds a new cell object based on the given object. If the underlying object is Null, a NullCell will be returned

Parameters:

  • obj (Object)

    The object to build the cell from.

Returns:

  • (Cell)

    The newly built cell object.



18
19
20
# File 'lib/terminal_calendar/selection/cell.rb', line 18

def self.build(obj)
  obj.null? ? NullCell.new(obj) : new(obj)
end

Instance Method Details

#null?Boolean

Checks if the object is null.

Returns:

  • (Boolean)

    Returns false.



41
42
43
# File 'lib/terminal_calendar/selection/cell.rb', line 41

def null?
  false
end

#renderString

Renders the selected_style string if it is selected, otherwise returns the result of rendering the underlying object.

Returns:

  • (String)

    The cell’s rendered content



32
33
34
35
36
# File 'lib/terminal_calendar/selection/cell.rb', line 32

def render
  return underlying_object.render unless selected?

  selected_style
end

#respond_to_missing?(method, include_all) ⇒ Boolean

Checks if the underlying object responds to the missing method.

Parameters:

  • method (Symbol)

    the name of the missing method

  • include_all (Boolean)

    whether to include private methods in the check

Returns:

  • (Boolean)

    true if the underlying object responds to the missing method, super otherwise



69
70
71
# File 'lib/terminal_calendar/selection/cell.rb', line 69

def respond_to_missing?(method, include_all)
  underlying_object.respond_to?(method) || super
end

#toggle_selected!Boolean

Toggles the selected state of the object.

Returns:

  • (Boolean)

    the new selected state of the object



47
48
49
# File 'lib/terminal_calendar/selection/cell.rb', line 47

def toggle_selected!
  @selected = !@selected
end