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
65 66 67 |
# File 'lib/ruby-app/common_config.rb', line 65 def self.[]=(option, value) @@configs[option.to_sym] = value end |
.define(name, &block) ⇒ Object
18 19 20 21 22 |
# File 'lib/ruby-app/common_config.rb', line 18 def self.define(name, &block) s = Scope.new s.instance_eval(&block) @@configs.merge!(s.configs) end |
.has_key?(key) ⇒ Boolean
69 70 71 |
# File 'lib/ruby-app/common_config.rb', line 69 def self.has_key?(key) @@configs.key?(key.to_sym) end |
.load(config_file, shouldbe = false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby-app/common_config.rb', line 24 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
77 78 79 80 81 82 83 84 |
# File 'lib/ruby-app/common_config.rb', line 77 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
42 43 44 |
# File 'lib/ruby-app/common_config.rb', line 42 def self.save(filename) File.open(filename, 'w'){|f| f.write YAML.dump(@@configs) } end |
.try(method) ⇒ Object
73 74 75 |
# File 'lib/ruby-app/common_config.rb', line 73 def self.try(method) self.send(method) rescue nil end |