Class: Gloo::Objs::Menu

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/cli/menu.rb

Constant Summary collapse

KEYWORD =
'menu'.freeze
KEYWORD_SHORT =
'menu'.freeze
PROMPT =
'prompt'.freeze
ITEMS =
'items'.freeze
LOOP =
'loop'.freeze
HIDE_ITEMS =
'hide_items'.freeze
BEFORE_MENU =
'before_menu'.freeze
DEFAULT =
'default'.freeze

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #msg_unload, #multiline_value?, #pn, #remove_child, #root?, #send_message, #set_parent, #set_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



91
92
93
# File 'lib/gloo/objs/cli/menu.rb', line 91

def self.messages
  return super + [ 'run' ]
end

.short_typenameObject

The short name of the object type.



32
33
34
# File 'lib/gloo/objs/cli/menu.rb', line 32

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



25
26
27
# File 'lib/gloo/objs/cli/menu.rb', line 25

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



67
68
69
# File 'lib/gloo/objs/cli/menu.rb', line 67

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



76
77
78
79
80
81
82
# File 'lib/gloo/objs/cli/menu.rb', line 76

def add_default_children
  fac = $engine.factory
  fac.create_string PROMPT, '> ', self
  fac.create_can ITEMS, self
  fac.create_bool LOOP, true, self
  fac.create_script DEFAULT, '', self
end

#begin_menuObject

Begin the menu execution. Run the before menu script if there is one, then show options unless we are hiding them by default.



123
124
125
126
127
128
129
130
131
# File 'lib/gloo/objs/cli/menu.rb', line 123

def begin_menu
  run_before_menu

  # Check to see if we should show items at all.
  o = find_child HIDE_ITEMS
  return if o && o.value == true

  show_options
end

#find_cmd(cmd) ⇒ Object

Find the command matching user input.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gloo/objs/cli/menu.rb', line 159

def find_cmd( cmd )
  o = find_child ITEMS
  return nil unless o

  o.children.each do |mitem|
    mitem = Gloo::Objs::Alias.resolve_alias( mitem )
    return mitem if mitem.shortcut_value.downcase == cmd.downcase
  end

  return nil
end

#loop?Boolean

Get the value of the loop child object. Should we keep looping or should we stop?

Returns:



51
52
53
54
55
56
# File 'lib/gloo/objs/cli/menu.rb', line 51

def loop?
  o = find_child LOOP
  return false unless o

  return o.value
end

#msg_runObject

Show the menu options, and prompt for user input.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gloo/objs/cli/menu.rb', line 98

def msg_run
  loop do
    begin_menu
    if prompt_value.empty?
      dt = DateTime.now
      d = dt.strftime( '%Y.%m.%d' )
      t = dt.strftime( '%I:%M:%S' )
      cmd = $prompt.ask( "#{d.yellow} #{t.white} >" )
    else
      cmd = $prompt.ask( prompt_value )
    end
    cmd ? run_command( cmd ) : run_default
    break unless loop?
  end
end

#prompt_valueObject

Get the value of the prompt child object. Returns nil if there is none.



40
41
42
43
44
45
# File 'lib/gloo/objs/cli/menu.rb', line 40

def prompt_value
  o = find_child PROMPT
  return '' unless o

  return o.value
end

#run_before_menuObject

If there is a before menu script, run it now.



136
137
138
139
140
141
# File 'lib/gloo/objs/cli/menu.rb', line 136

def run_before_menu
  o = find_child BEFORE_MENU
  return unless o

  Gloo::Exec::Dispatch.message 'run', o
end

#run_command(cmd) ⇒ Object

Run the selected command.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gloo/objs/cli/menu.rb', line 185

def run_command( cmd )
  obj = find_cmd cmd

  unless obj
    if cmd == '?'
      show_options
    else
      puts "#{cmd} is not a valid option"
    end
    return
  end

  script = obj.do_script
  return unless script

  s = Gloo::Exec::Script.new script
  s.run
end

#run_defaultObject

Run the default option.



174
175
176
177
178
179
180
# File 'lib/gloo/objs/cli/menu.rb', line 174

def run_default
  obj = find_child DEFAULT
  return unless obj

  s = Gloo::Exec::Script.new obj
  s.run
end

#show_optionsObject

Show the list of menu options.



146
147
148
149
150
151
152
153
154
# File 'lib/gloo/objs/cli/menu.rb', line 146

def show_options
  o = find_child ITEMS
  return unless o

  o.children.each do |mitem|
    mitem = Gloo::Objs::Alias.resolve_alias( mitem )
    puts "  #{mitem.shortcut_value} - #{mitem.description_value}"
  end
end