Class: PPCurses::MenuItem

Inherits:
Object
  • Object
show all
Defined in:
lib/ppcurses/menu/menu_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title) ⇒ MenuItem

Returns a new instance of MenuItem.



21
22
23
24
25
# File 'lib/ppcurses/menu/menu_item.rb', line 21

def initialize( title )
  @title = title
  @state = PP_OFF_STATE
  @selectable = false
end

Instance Attribute Details

#selectableObject

Returns the value of attribute selectable.



19
20
21
# File 'lib/ppcurses/menu/menu_item.rb', line 19

def selectable
  @selectable
end

#stateObject

Returns the value of attribute state.



18
19
20
# File 'lib/ppcurses/menu/menu_item.rb', line 18

def state
  @state
end

#targetObject

Who to notify when menu is selected chosen? The target should be a method selector, and



16
17
18
# File 'lib/ppcurses/menu/menu_item.rb', line 16

def target
  @target
end

#titleObject

Returns the value of attribute title.



12
13
14
# File 'lib/ppcurses/menu/menu_item.rb', line 12

def title
  @title
end

Instance Method Details

#call_targetObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ppcurses/menu/menu_item.rb', line 48

def call_target
  unless @target.nil?
    case @target.arity
      when 0
        @target.call
      when 1
        @target.call(self)
      else
        raise ArgumentError, "Too many parameters for target don't know what to do"
    end
  end
end

#display_stringObject



27
28
29
30
31
32
33
# File 'lib/ppcurses/menu/menu_item.rb', line 27

def display_string
  if @state == PP_OFF_STATE
    return '  ' + @title
  end

  SELECTED_CHAR + ' ' + @title
end

#handle_key(key) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ppcurses/menu/menu_item.rb', line 63

def handle_key(key)

  if key == ' ' and @selectable
    toggle_on_off_state
    call_target
    return true
  end

  if key == ENTER
    call_target
    return true
  end

  false
end

#toggle_on_off_stateObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ppcurses/menu/menu_item.rb', line 36

def toggle_on_off_state
  if @state == PP_OFF_STATE
    @state = PP_ON_STATE
    return
  end

  if @state == PP_ON_STATE
    @state = PP_OFF_STATE
  end

end