Class: Jah::Opt

Inherits:
Object
  • Object
show all
Defined in:
lib/jah/opt.rb

Class Method Summary collapse

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(options)
  conf = "jah.yaml"
  unless file = options[:config]
    if file = [nil, HOME, "/etc/"].select { |c| File.exists? "#{c}#{conf}" }[0]
      file << conf
      options[:config] = file
    end
  end
  options = YAML.load(File.read(file)).merge!(options) if file rescue nil

  I18n.locale = options[:i18n] if options[:i18n]
  options[:groups] ||= []

  @hash.merge!(defaults )
  @hash.merge!(options)
  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

.defaultsObject



27
28
29
# File 'lib/jah/opt.rb', line 27

def defaults
  { :mode => "xmpp", :daemon => false, :port => 5222 }
end

.hashObject



23
24
25
# File 'lib/jah/opt.rb', line 23

def hash
  @hash
end

.hostnameObject



31
32
33
# File 'lib/jah/opt.rb', line 31

def hostname
  @hostame ||= `hostname`.chomp.gsub(/\W/, "") rescue "jah#{rand(42000)}"
end

.localeObject



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