Class: Middleman::Configuration::ConfigurationManager
- Inherits:
-
Object
- Object
- Middleman::Configuration::ConfigurationManager
- Defined in:
- lib/middleman-core/configuration.rb
Overview
A class that manages a collection of documented settings. Can be used by extensions as well as the main Middleman application. Extensions should probably finalize their instance after defining all the settings they want to expose.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get the value of a setting by key.
-
#[]=(key, val) ⇒ Object
Set the value of a setting by key.
-
#all_settings ⇒ Array<ConfigSetting>
Get all settings, sorted by key, as ConfigSetting objects.
-
#define_setting(key, default = nil, description = nil) ⇒ ConfigSetting
Define a new setting, with optional default and user-friendly description.
-
#defines_setting?(key) ⇒ Boolean
Does this configuration manager know about the setting identified by key?.
-
#dup ⇒ Object
Deep duplicate of the configuration manager.
-
#finalize! ⇒ Object
Switch the configuration manager is finalized, it switches to read-only mode and no new settings may be defined.
-
#initialize ⇒ ConfigurationManager
constructor
A new instance of ConfigurationManager.
-
#load_settings(other_settings) ⇒ Object
Load in a list of settings.
-
#method_missing(method, *args) ⇒ Object
Allow configuration settings to be read and written via methods.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Needed so that method_missing makes sense.
-
#setting(key) ⇒ ConfigSetting
Get a full ConfigSetting object for the setting with the give key.
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ ConfigurationManager
Returns a new instance of ConfigurationManager.
93 94 95 96 97 |
# File 'lib/middleman-core/configuration.rb', line 93 def initialize # A hash from setting key to ConfigSetting instance. @settings = {} @finalized = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Allow configuration settings to be read and written via methods
127 128 129 130 131 132 133 134 135 |
# File 'lib/middleman-core/configuration.rb', line 127 def method_missing(method, *args) if defines_setting?(method) && args.size == 0 self[method] elsif method.to_s =~ /^(\w+)=$/ && args.size == 1 self[$1.to_sym] = args[0] else super end end |
Instance Method Details
#[](key) ⇒ Object
Get the value of a setting by key. Returns nil if there is no such setting.
113 114 115 116 |
# File 'lib/middleman-core/configuration.rb', line 113 def [](key) setting = @settings[key] setting ? setting.value : nil end |
#[]=(key, val) ⇒ Object
Set the value of a setting by key. Creates the setting if it doesn’t exist.
121 122 123 124 |
# File 'lib/middleman-core/configuration.rb', line 121 def []=(key, val) setting = @settings[key] || define_setting(key) setting.value = val end |
#all_settings ⇒ Array<ConfigSetting>
Get all settings, sorted by key, as ConfigSetting objects.
101 102 103 |
# File 'lib/middleman-core/configuration.rb', line 101 def all_settings @settings.values.sort_by(&:key) end |
#define_setting(key, default = nil, description = nil) ⇒ ConfigSetting
Define a new setting, with optional default and user-friendly description. Once the configuration manager is finalized, no new settings may be defined.
156 157 158 159 160 161 162 |
# File 'lib/middleman-core/configuration.rb', line 156 def define_setting(key, default=nil, description=nil) raise "Setting #{key} doesn't exist" if @finalized raise "Setting #{key} already defined" if @settings.has_key?(key) raise "Setting key must be a Symbol" unless key.is_a? Symbol @settings[key] = ConfigSetting.new(key, default, description) end |
#defines_setting?(key) ⇒ Boolean
Does this configuration manager know about the setting identified by key?
145 146 147 |
# File 'lib/middleman-core/configuration.rb', line 145 def defines_setting?(key) @settings.has_key?(key) end |
#dup ⇒ Object
Deep duplicate of the configuration manager
172 173 174 |
# File 'lib/middleman-core/configuration.rb', line 172 def dup ConfigurationManager.new.tap {|c| c.load_settings(self.all_settings) } end |
#finalize! ⇒ Object
Switch the configuration manager is finalized, it switches to read-only mode and no new settings may be defined.
166 167 168 169 |
# File 'lib/middleman-core/configuration.rb', line 166 def finalize! @finalized = true self end |
#load_settings(other_settings) ⇒ Object
Load in a list of settings
177 178 179 180 181 182 |
# File 'lib/middleman-core/configuration.rb', line 177 def load_settings(other_settings) other_settings.each do |setting| new_setting = define_setting(setting.key, setting.default, setting.description) new_setting.value = setting.value if setting.value_set? end end |
#respond_to?(method, include_private = false) ⇒ Boolean
Needed so that method_missing makes sense
138 139 140 |
# File 'lib/middleman-core/configuration.rb', line 138 def respond_to?(method, include_private = false) super || defines_setting?(method) || (method =~ /^(\w+)=$/ && defines_setting?($1)) end |
#setting(key) ⇒ ConfigSetting
Get a full ConfigSetting object for the setting with the give key.
107 108 109 |
# File 'lib/middleman-core/configuration.rb', line 107 def setting(key) @settings[key] end |
#to_h ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/middleman-core/configuration.rb', line 184 def to_h hash = {} @settings.each do |key, setting| hash[key] = setting.value end hash end |
#to_s ⇒ Object
192 193 194 |
# File 'lib/middleman-core/configuration.rb', line 192 def to_s to_h.inspect end |