Class: Jah::Opt
- Inherits:
-
Object
- Object
- Jah::Opt
- Defined in:
- lib/jah/opt.rb
Class Method Summary collapse
- .[](val) ⇒ Object
- .[]=(key, val) ⇒ Object
-
.autoload_config(options) ⇒ Object
Load config [., ~/.jah, /etc].
- .closing_time! ⇒ Object
- .defaults ⇒ Object
- .hash ⇒ Object
- .hostname ⇒ Object
- .locale ⇒ Object
- .merge!(val) ⇒ Object
- .method_missing(*meth) ⇒ Object
Class Method Details
.[](val) ⇒ Object
68 |
# File 'lib/jah/opt.rb', line 68 def [](val); @hash[val]; end |
.[]=(key, val) ⇒ Object
69 |
# File 'lib/jah/opt.rb', line 69 def []=(key, val); @hash[key] = val; end |
.autoload_config(options) ⇒ Object
Load config [., ~/.jah, /etc]
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jah/opt.rb', line 40 def autoload_config() conf = "jah.yaml" unless file = [:config] if file = [nil, HOME, "/etc/"].select { |c| File.exists? "#{c}#{conf}" }[0] file << conf [:config] = file end end = YAML.load(File.read(file)).merge!() if file rescue nil I18n.locale = [:i18n] if [:i18n] [:groups] ||= [] @hash.merge!(defaults ) @hash.merge!() OptBackup.merge!(@hash) end |
.closing_time! ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/jah/opt.rb', line 58 def closing_time! if @hash != OptBackup puts "Writing config..." File.open(@hash[:config], "w+") do |f| f.write "# Auto generated on #{Time.now}" f.write @hash.to_yaml end end end |
.defaults ⇒ Object
27 28 29 |
# File 'lib/jah/opt.rb', line 27 def defaults { :mode => "xmpp", :daemon => false, :port => 5222 } end |
.hash ⇒ Object
23 24 25 |
# File 'lib/jah/opt.rb', line 23 def hash @hash end |
.hostname ⇒ Object
31 32 33 |
# File 'lib/jah/opt.rb', line 31 def hostname @hostame ||= `hostname`.chomp.gsub(/\W/, "") rescue "jah#{rand(42000)}" end |
.locale ⇒ Object
35 36 37 |
# File 'lib/jah/opt.rb', line 35 def locale @locale ||= `locale | grep LANG`.scan(/LANG=(.*)\./)[0][0].downcase rescue "en_us" end |
.merge!(val) ⇒ Object
70 |
# File 'lib/jah/opt.rb', line 70 def merge!(val); @hash.merge!(val); end |
.method_missing(*meth) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jah/opt.rb', line 72 def method_missing(*meth) key = meth[0].to_s.gsub(/\?|=/, "").to_sym if val = meth[1] @hash[key] = val elsif !(val = @hash[key]).nil? val else raise "No method #{val} #{key}" end end |