Class: Dumon::App

Inherits:
Object
  • Object
show all
Includes:
Rrutils::Confdb, Rrutils::Options, Singleton
Defined in:
lib/dumon.rb

Overview

This class represents an entry point

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Rrutils::Options

#assert, #keys_to_sym, #verify_and_sanitize_options, #verify_options

Methods included from Rrutils::Confdb

#read, #write

Constructor Details

#initializeApp

Constructor.



43
44
45
46
# File 'lib/dumon.rb', line 43

def initialize
  @ui = new_ui
  Dumon::logger.debug "Used UI: #{ui.class.name}"
end

Instance Attribute Details

#current_profileObject

Currently used profile.



39
40
41
# File 'lib/dumon.rb', line 39

def current_profile
  @current_profile
end

#uiObject (readonly)

User interface of Dumon tool.



35
36
37
# File 'lib/dumon.rb', line 35

def ui
  @ui
end

Instance Method Details

#config_file(mode = 'r') ⇒ Object

Gets default config file.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dumon.rb', line 63

def config_file(mode='r')
  homedir = RUBY_VERSION < '1.9' ? ENV['HOME'] : Dir.home
  filename = "#{homedir}#{File::SEPARATOR}.config#{File::SEPARATOR}dumon.conf"

  # check and create directory structure
  dirname = File.dirname filename
  ::FileUtils.mkdir_p(dirname) unless File.exist?(dirname)

  # create file if does not exist
  File.open(filename, 'w').close unless File.exist? filename

  File.open(filename, mode)
end

#new_ui(with = Dumon::GtkTrayUi) ⇒ Object

Factory method to create a new object of UI.<p/> Can be used as Dependency Injection (DI) entry point: you can reopen Dumon:App and redefine ‘new_ui’ if you implement a new UI class. <pre> class Dumon::App

def new_ui; Dumon::XyUi.new; end

end </pre>



57
58
59
# File 'lib/dumon.rb', line 57

def new_ui(with=Dumon::GtkTrayUi)
  with.new
end

#quitObject

Quits cleanly the application.



127
128
129
130
# File 'lib/dumon.rb', line 127

def quit
  ui.quit
  Dumon::logger.info 'Terminted...'
end

#read_configObject

Reads Dumon’s configuration.



79
80
81
82
83
84
85
86
87
# File 'lib/dumon.rb', line 79

def read_config
  conf = read config_file
  conf = keys_to_sym conf

  # there can be a hook if config version is old

  conf = verify_and_sanitize_options conf, {:version => VERSION, :profiles => {}, :post_switch => :optional}
  conf
end

#run(daemon = false) ⇒ Object

Runs the application.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dumon.rb', line 98

def run(daemon=false)
  # profile
  prof_arg = ARGV.select {|i| i.start_with? 'profile:'}.first
  unless prof_arg.nil?
    prof_name = prof_arg.split(':')[1]
    conf = read_config
    raise "unknown profile, name=#{prof_name}" unless conf[:profiles][prof_name.to_sym]
    Dumon::logger.info "Started with profile '#{prof_name}'"
    ui.apply_profile(conf, prof_name)
  end

  # daemon mode
  if daemon
    if RUBY_VERSION < '1.9'
      Dumon::logger.warn 'Daemon mode supported only in Ruby >= 1.9'
    else
      # Daemonize the process
      # - stay in the current directory
      # - don't redirect standard input, standard output and standard error to /dev/null
      Dumon::logger.info 'Running as daemon...'
      Process.daemon(true, true)
    end
  end

  ui.render
end

#write_config(conf) ⇒ Object

Writes Dumon’s configuration.



91
92
93
94
# File 'lib/dumon.rb', line 91

def write_config(conf)
  conf[:version] = VERSION
  write(conf, config_file('w'))
end