Class: EatMySoul::Settings
- Inherits:
-
Object
- Object
- EatMySoul::Settings
- Defined in:
- lib/eatmysoul.rb
Instance Method Summary collapse
- #init_logger ⇒ Object
-
#initialize ⇒ Settings
constructor
edit the next line to change the default path.
- #method_missing(sym) ⇒ Object
- #save(path) ⇒ Object
- #validates ⇒ Object
Constructor Details
#initialize ⇒ Settings
edit the next line to change the default path
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/eatmysoul.rb', line 44 def initialize() begin @options = { :server => "ns-server.epita.fr", :port => 4242, :login => "login_x", :passwd => "pwd_socks", :logfile => "STDOUT", :loglevel => "WARN", :location => "somewhere", :status => "chatless client" } parser = Trollop::Parser.new parser.opt(:config_file, "Configuration file path", :default => (if `id` =~ /root/ then "/etc/eatmysoul.yml" else "#{ENV['HOME']}/.eatmysoul.yml" end)) @options.each { |k,v| parser.opt(k, k.to_s, :type => :string) } cli_opts = parser.parse.delete_if { |k,v| not v or k =~ /_given/ } puts cli_opts path = cli_opts.delete :config_file if File.exists? path yaml = YAML::load_file(path) @options.merge! yaml end @options.merge! cli_opts save path rescue => e $stderr.write "Error with settings parsing: #{e}\n" $stderr.write "#{e.backtrace}\n" end init_logger validates end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/eatmysoul.rb', line 111 def method_missing(sym) if @options.has_key? sym @options[sym] else raise NoMethodError end end |
Instance Method Details
#init_logger ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/eatmysoul.rb', line 93 def init_logger if logfile == "STDOUT" or logfile == "STDERR" @options[:logger] = Logger.new Kernel.const_get logfile.to_sym else @options[:logger] = Logger.new logfile end logger.level = Logger.const_get loglevel.to_sym logger.progname = "eatmysoul" logger.warn "Started EatMySoul" end |
#save(path) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/eatmysoul.rb', line 83 def save(path) yaml = YAML::dump(@options) if ((File.exists? path and File.writable? path) or not File.exists?(path)) File.open(path, "w") do |f| f << yaml end end end |
#validates ⇒ Object
104 105 106 107 108 109 |
# File 'lib/eatmysoul.rb', line 104 def validates if location.length > 64 or status.length > 64 logger.fatal "location and status lengths must be < 64 characters" exit 42 end end |