Class: Gloo::App::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/app/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = []) ⇒ Engine

Set up the engine with basic elements.



26
27
28
29
30
31
32
33
# File 'lib/gloo/app/engine.rb', line 26

def initialize( params = [] )
  $engine = self
  @args = Args.new( params )
  $settings = Settings.new( ENV[ 'GLOO_ENV' ] )
  $log = Log.new( @args.quiet? )
  $prompt = TTY::Prompt.new
  $log.debug 'engine intialized...'
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



18
19
20
# File 'lib/gloo/app/engine.rb', line 18

def args
  @args
end

#converterObject

Returns the value of attribute converter.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def converter
  @converter
end

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



19
20
21
# File 'lib/gloo/app/engine.rb', line 19

def dictionary
  @dictionary
end

#event_managerObject

Returns the value of attribute event_manager.



20
21
22
# File 'lib/gloo/app/engine.rb', line 20

def event_manager
  @event_manager
end

#exec_envObject

Returns the value of attribute exec_env.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def exec_env
  @exec_env
end

#factoryObject (readonly)

Returns the value of attribute factory.



19
20
21
# File 'lib/gloo/app/engine.rb', line 19

def factory
  @factory
end

#heapObject (readonly)

Returns the value of attribute heap.



19
20
21
# File 'lib/gloo/app/engine.rb', line 19

def heap
  @heap
end

#helpObject

Returns the value of attribute help.



21
22
23
# File 'lib/gloo/app/engine.rb', line 21

def help
  @help
end

#last_cmdObject

Returns the value of attribute last_cmd.



20
21
22
# File 'lib/gloo/app/engine.rb', line 20

def last_cmd
  @last_cmd
end

#modeObject (readonly)

Returns the value of attribute mode.



18
19
20
# File 'lib/gloo/app/engine.rb', line 18

def mode
  @mode
end

#parserObject (readonly)

Returns the value of attribute parser.



19
20
21
# File 'lib/gloo/app/engine.rb', line 19

def parser
  @parser
end

#persist_manObject

Returns the value of attribute persist_man.



20
21
22
# File 'lib/gloo/app/engine.rb', line 20

def persist_man
  @persist_man
end

#runningObject (readonly)

Returns the value of attribute running.



18
19
20
# File 'lib/gloo/app/engine.rb', line 18

def running
  @running
end

Instance Method Details

#clear_screenObject

Clear the screen.



202
203
204
205
206
# File 'lib/gloo/app/engine.rb', line 202

def clear_screen
  @cursor ||= TTY::Cursor
  print @cursor.clear_screen
  print @cursor.move_to( 0, 0 )
end

#default_promptObject

Get the default prompt text.



126
127
128
129
130
131
# File 'lib/gloo/app/engine.rb', line 126

def default_prompt
  dt = DateTime.now
  d = dt.strftime( '%Y.%m.%d' )
  t = dt.strftime( '%I:%M:%S' )
  return "#{'gloo'.blue} #{d.yellow} #{t.white} >"
end

#err(msg) ⇒ Object

Report an error. Write it to the log and set the heap error value.



223
224
225
226
# File 'lib/gloo/app/engine.rb', line 223

def err( msg )
  $log.error msg
  self.heap.error.set_to msg
end

#error?Boolean

Did the last command result in an error?

Returns:

  • (Boolean)


215
216
217
# File 'lib/gloo/app/engine.rb', line 215

def error?
  return !@heap.error.value.nil?
end

#last_cmd_blank?Boolean

Is the last command entered blank?

Returns:

  • (Boolean)


136
137
138
139
140
141
# File 'lib/gloo/app/engine.rb', line 136

def last_cmd_blank?
  return true if @last_cmd.nil?
  return true if @last_cmd.strip.empty?

  return false
end

#loopObject

Prompt, Get input, process.



146
147
148
149
150
151
# File 'lib/gloo/app/engine.rb', line 146

def loop
  while @running
    prompt_cmd
    process_cmd
  end
end

#open_start_fileObject

Get the setting for the start_with file and open it.



111
112
113
114
# File 'lib/gloo/app/engine.rb', line 111

def open_start_file
  name = $settings.start_with
  @persist_man.load( name ) if name
end

#process_cmdObject

Process the command.



156
157
158
159
160
161
162
163
# File 'lib/gloo/app/engine.rb', line 156

def process_cmd
  if last_cmd_blank?
    clear_screen
    return
  end

  @parser.run @last_cmd
end

#prompt_cmdObject

Prompt for the next command.



119
120
121
# File 'lib/gloo/app/engine.rb', line 119

def prompt_cmd
  @last_cmd = $prompt.ask( default_prompt )
end

#quitObject

Do any clean up and quit.



175
176
177
# File 'lib/gloo/app/engine.rb', line 175

def quit
  $log.debug 'quitting...'
end

#runObject

Run in interactive mode.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gloo/app/engine.rb', line 94

def run
  # Open default file(s)
  self.open_start_file

  # TODO: open any files specifed in args

  unless @mode == Mode::SCRIPT || @args.quiet?
    @cursor = TTY::Cursor
    self.loop
  end

  quit
end

#run_filesObject

Run files specified on the CLI. Then quit.



86
87
88
89
# File 'lib/gloo/app/engine.rb', line 86

def run_files
  @args.files.each { |f| @persist_man.load( f ) }
  quit
end

#run_modeObject

Run gloo in the selected mode.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gloo/app/engine.rb', line 68

def run_mode
  $log.debug "running gloo in #{@mode} mode"

  if @mode == Mode::VERSION
    run_version
  elsif @mode == Mode::HELP
    show_help_and_quit
  elsif @mode == Mode::SCRIPT
    run_files
  else
    run
  end
end

#run_versionObject

Show the version information and then quit.



186
187
188
189
# File 'lib/gloo/app/engine.rb', line 186

def run_version
  puts Info.display_title unless @args.quiet?
  quit
end

#show_help_and_quitObject

Show the help information and then quit.



194
195
196
197
# File 'lib/gloo/app/engine.rb', line 194

def show_help_and_quit
  @help.show_app_help
  quit
end

#startObject

Start the engine. Load object and verb definitions and setup engine elements.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gloo/app/engine.rb', line 39

def start
  $log.debug 'starting the engine...'
  $log.debug Info.display_title
  @mode = @args.detect_mode
  @running = true

  @dictionary = Gloo::Core::Dictionary.instance
  @dictionary.init
  @parser = Gloo::Core::Parser.new
  @heap = Gloo::Core::Heap.new
  @factory = Gloo::Core::Factory.new
  @persist_man = Gloo::Persist::PersistMan.new
  @event_manager = Gloo::Core::EventManager.new

  @exec_env = Gloo::Exec::ExecEnv.new
  @help = Gloo::App::Help.new
  @converter = Gloo::Convert::Converter.new

  $log.debug 'the engine has started'
  run_mode
end

#stop_runningObject

Request the engine to stop running.



168
169
170
# File 'lib/gloo/app/engine.rb', line 168

def stop_running
  @running = false
end