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
128 129 130 131 132 133 134 135 136 |
# File 'lib/middleman-core/configuration.rb', line 128 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_obj = setting(key) setting_obj ? setting_obj.value : nil end |
#[]=(key, val) ⇒ Object
Set the value of a setting by key. Creates the setting if it doesn’t exist. rubocop:disable UselessSetterCall
122 123 124 125 |
# File 'lib/middleman-core/configuration.rb', line 122 def []=(key, val) setting_obj = setting(key) || define_setting(key) setting_obj.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.
157 158 159 160 161 162 163 |
# File 'lib/middleman-core/configuration.rb', line 157 def define_setting(key, default=nil, description=nil) raise "Setting #{key} doesn't exist" if @finalized raise "Setting #{key} already defined" if @settings.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?
146 147 148 |
# File 'lib/middleman-core/configuration.rb', line 146 def defines_setting?(key) @settings.key?(key) end |
#dup ⇒ Object
Deep duplicate of the configuration manager
173 174 175 |
# File 'lib/middleman-core/configuration.rb', line 173 def dup ConfigurationManager.new.tap { |c| c.load_settings(all_settings) } end |
#finalize! ⇒ Object
Switch the configuration manager is finalized, it switches to read-only mode and no new settings may be defined.
167 168 169 170 |
# File 'lib/middleman-core/configuration.rb', line 167 def finalize! @finalized = true self end |
#load_settings(other_settings) ⇒ Object
Load in a list of settings
178 179 180 181 182 183 |
# File 'lib/middleman-core/configuration.rb', line 178 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
139 140 141 |
# File 'lib/middleman-core/configuration.rb', line 139 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
185 186 187 188 189 190 191 |
# File 'lib/middleman-core/configuration.rb', line 185 def to_h hash = {} @settings.each do |key, setting| hash[key] = setting.value end hash end |
#to_s ⇒ Object
193 194 195 |
# File 'lib/middleman-core/configuration.rb', line 193 def to_s to_h.inspect end |