Class: Settingslogic
- Inherits:
-
Hash
- Object
- Hash
- Settingslogic
- Includes:
- EigenMethodDefiner
- Defined in:
- lib/settingslogic.rb
Overview
A simple settings solution using a YAML file. See README for more information.
Defined Under Namespace
Modules: EigenMethodDefiner
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(hash_or_file = self.class.source) ⇒ Settingslogic
constructor
Initializes a new settings object.
Methods included from EigenMethodDefiner
Constructor Details
#initialize(hash_or_file = self.class.source) ⇒ Settingslogic
Initializes a new settings object. You can initialize an object in any of the following ways:
Settings.new(:application) # will look for config/application.yml
Settings.new("application.yaml") # will look for application.yaml
Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
Settings.new(:config1 => 1, :config2 => 2)
Basically if you pass a symbol it will look for that file in the configs directory of your rails app, if you are using this in rails. If you pass a string it should be an absolute path to your settings file. Then you can pass a hash, and it just allows you to access the hash via methods.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/settingslogic.rb', line 46 def initialize(hash_or_file = self.class.source) case hash_or_file when Hash self.update hash_or_file else hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash hash = hash[self.class.namespace] if self.class.namespace self.update hash end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Settingslogic::EigenMethodDefiner
Class Method Details
.name ⇒ Object
:nodoc:
7 8 9 |
# File 'lib/settingslogic.rb', line 7 def name # :nodoc: instance.key?("name") ? instance.name : super end |
.namespace(value = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/settingslogic.rb', line 19 def namespace(value = nil) if value.nil? @namespace else @namespace = value end end |
.source(value = nil) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/settingslogic.rb', line 11 def source(value = nil) if value.nil? @source else @source = value end end |