Class: NonConfig::Base
- Inherits:
-
Object
- Object
- NonConfig::Base
- Defined in:
- lib/non_config/base.rb
Overview
Base class you need to inherit
Constant Summary collapse
- ENVIRONMENTS =
[:development, :test, :production].freeze
- @@files =
{}
- @@env =
nil
Class Method Summary collapse
-
.[](key) ⇒ Object
Tries to find object.
-
.[]=(key, value) ⇒ Object
alias for #set key, value.
- .config_file(file_alias = nil) ⇒ Object
-
.env(env) ⇒ Object
sets environment.
-
.method_missing(m, *args) ⇒ Object
If ‘args` empty it will try to find `m.to_s` and `m` in config else (or if not found) it will call super.
- .save(file_alias = nil) ⇒ Object
-
.set(key, value, file_alias = nil) ⇒ Object
Sets value to one of config files.
-
.source(file_alias, path) ⇒ Object
Loads file.
Class Method Details
.[](key) ⇒ Object
Tries to find object.
Unlike Base.key it won’t raise exception on not found
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/non_config/base.rb', line 75 def self.[](key) default = nil @@files.reverse_each do |_name, file| default = file[key] if default.nil? return file[@@env][key] if file[@@env] && file[@@env][key] return file[key] if !@@env && file[key] end default end |
.[]=(key, value) ⇒ Object
alias for #set key, value
64 65 66 |
# File 'lib/non_config/base.rb', line 64 def self.[]=(key, value) set(key, value) end |
.config_file(file_alias = nil) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/non_config/base.rb', line 34 def self.config_file(file_alias = nil) file_alias = @@files.to_a.last && @@files.to_a.last[0] unless file_alias fail FileNotRegistered unless file_alias && @@files.key?(file_alias) @@files[file_alias] end |
.env(env) ⇒ Object
sets environment. See Base::ENVIRONMENTS
42 43 44 45 46 47 48 49 |
# File 'lib/non_config/base.rb', line 42 def self.env(env) return @@env = nil unless env env = env.to_sym fail UnsupportedEnvironment unless ENVIRONMENTS.include? env @@env = env.to_s end |
.method_missing(m, *args) ⇒ Object
If ‘args` empty it will try to find `m.to_s` and `m` in config else (or if not found) it will call super
89 90 91 92 93 94 95 96 97 |
# File 'lib/non_config/base.rb', line 89 def self.method_missing(m, *args) result = self[m.to_s] return result if result result = self[m] return result if result super end |
.save(file_alias = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/non_config/base.rb', line 99 def self.save(file_alias = nil) fail FileNotRegistered if file_alias && !@@files.key?(file_alias) if file_alias @@files[file_alias].save else @@files.values.each(&:save) end end |
.set(key, value, file_alias = nil) ⇒ Object
Sets value to one of config files
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/non_config/base.rb', line 52 def self.set(key, value, file_alias = nil) file = config_file file_alias if @@env file[@@env] ||= {} file[@@env][key] = value else file[key] = value end end |
.source(file_alias, path) ⇒ Object
Loads file.
Files that were added last are considered to be more important i.e. if you have
source :file1, '...'
source :file2, '...'
source :file3, '...'
all operations will be executed from file3 to file1
28 29 30 31 32 |
# File 'lib/non_config/base.rb', line 28 def self.source(file_alias, path) fail('file alias is already taken') if @@files[file_alias] @@files[file_alias] = ConfigFile.new(path) end |