Class: Gloo::Objs::Menu

Inherits:
GlooLang::Core::Obj
  • Object
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

Class Method Summary collapse

Instance Method Summary collapse

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:

  • (Boolean)


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.



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

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.



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

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

  o.children.each do |mitem|
    mitem = GlooLang::Objs::Alias.resolve_alias( @engine, 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:

  • (Boolean)


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
113
# File 'lib/gloo/objs/cli/menu.rb', line 98

def msg_run
  run_default
  loop do
    begin_menu
    if prompt_value.empty?
      dt = DateTime.now
      d = dt.strftime( '%Y.%m.%d' )
      t = dt.strftime( '%I:%M:%S' )
      cmd = @engine.platform.prompt.ask( "#{d.yellow} #{t.white} >" )
    else
      cmd = @engine.platform.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.



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

def run_before_menu
  o = find_child BEFORE_MENU
  return unless o

  GlooLang::Exec::Dispatch.message( @engine, 'run', o )
end

#run_command(cmd) ⇒ Object

Run the selected command.



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

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 = GlooLang::Exec::Script.new( @engine, script )
  s.run
end

#run_defaultObject

Run the default option.



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

def run_default
  obj = find_child DEFAULT
  return unless obj

  s = GlooLang::Exec::Script.new( @engine, obj )
  s.run
end

#show_optionsObject

Show the list of menu options.



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

def show_options
  o = find_child ITEMS
  return unless o

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