Class: CommonConfig
- Inherits:
-
Object
- Object
- CommonConfig
- Defined in:
- lib/ruby-app/common_config.rb
Defined Under Namespace
Classes: Scope
Constant Summary collapse
- @@configs =
{}
Class Method Summary collapse
- .[]=(option, value) ⇒ Object
- .define(name, &block) ⇒ Object
- .has_key?(key) ⇒ Boolean
- .load(config_file, shouldbe = false) ⇒ Object
- .method_missing(name, *args) ⇒ Object
- .save(filename) ⇒ Object
- .try(method) ⇒ Object
Class Method Details
.[]=(option, value) ⇒ Object
54 55 56 |
# File 'lib/ruby-app/common_config.rb', line 54 def self.[]=(option, value) @@configs[option.to_sym] = value end |
.define(name, &block) ⇒ Object
6 7 8 9 10 |
# File 'lib/ruby-app/common_config.rb', line 6 def self.define(name, &block) s = Scope.new s.instance_eval(&block) @@configs.merge!(s.configs) end |
.has_key?(key) ⇒ Boolean
58 59 60 |
# File 'lib/ruby-app/common_config.rb', line 58 def self.has_key?(key) @@configs.key?(key.to_sym) end |
.load(config_file, shouldbe = false) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ruby-app/common_config.rb', line 12 def self.load(config_file, shouldbe = false) if File.exists?(config_file) require 'yaml' h = YAML.load_file(config_file) if h.is_a?(Hash) h.symbolize_keys! @@configs.merge!(h) else raise "config should be a Hash, but not #{h.inspect}" end else if shouldbe raise "config file not found, create! #{config_file.inspect}" end end end |
.method_missing(name, *args) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/ruby-app/common_config.rb', line 66 def self.method_missing(name, *args) if has_key?(name) res = @@configs[name.to_sym] res.is_a?(Proc) ? res.call : res else super end end |
.save(filename) ⇒ Object
30 31 32 |
# File 'lib/ruby-app/common_config.rb', line 30 def self.save(filename) File.open(filename, 'w'){|f| f.write YAML.dump(@@configs) } end |
.try(method) ⇒ Object
62 63 64 |
# File 'lib/ruby-app/common_config.rb', line 62 def self.try(method) self.send(method) rescue nil end |