Class: MediaWikiVagrant::Settings
- Includes:
- Enumerable, SettingsDefiner
- Defined in:
- lib/mediawiki-vagrant/settings.rb
Overview
A collection of MediaWiki-Vagrant settings that can be loaded from and written to a YAML-serialized file.
Predefined settings can be declared using define and given additional options such as default values, descriptions to be used by interactive prompts, and value coercion to enforce minimums, maximums, etc.
Class Method Summary collapse
-
.configure(path) ⇒ Object
Safely configure the settings stored in the given file using the provided block.
Instance Method Summary collapse
-
#-(other) ⇒ Hash
Computes differences between these and the given settings.
-
#[](key) ⇒ Object
The given setting's current value.
-
#[]=(key, value) ⇒ Object
Changes the given setting's value.
-
#combine(other) ⇒ Object
Combine the given settings with the current ones.
-
#each {|name, setting| ... } ⇒ Object
Iterate over each setting.
-
#initialize ⇒ Settings
constructor
A new instance of Settings.
-
#load(path_or_io) ⇒ Object
Load YAML-serialized settings from the given path.
-
#required ⇒ Hash
Returns only those of the current settings that are required (have no default value).
-
#save(path_or_io) ⇒ Integer
Serializes and saves the current settings to the given file path.
-
#setting(name) ⇒ Setting
Returns the setting with the given name.
-
#unset!(name) ⇒ Object
Unset the given setting.
-
#update(other) ⇒ Object
Updates settings from the given `{ name: value }` hash.
Methods included from SettingsDefiner
Constructor Details
#initialize ⇒ Settings
Returns a new instance of Settings.
70 71 72 |
# File 'lib/mediawiki-vagrant/settings.rb', line 70 def initialize @settings = self.class.definitions end |
Class Method Details
.configure(path) ⇒ Object
Safely configure the settings stored in the given file using the provided block. The file is locked for exclusive write access during evaluation.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mediawiki-vagrant/settings.rb', line 49 def self.configure(path) settings = Settings.new File.open(path, File::RDWR | File::CREAT) do |file| file.flock(File::LOCK_EX) begin settings.load(file) file.rewind yield settings settings.save(file) file.flush file.truncate(file.pos) ensure file.flock(File::LOCK_UN) end end end |
Instance Method Details
#-(other) ⇒ Hash
Computes differences between these and the given settings.
100 101 102 103 104 105 |
# File 'lib/mediawiki-vagrant/settings.rb', line 100 def -(other) each.with_object({}) do |(name, setting), changes| other_value = other.setting(name).value changes[setting] = [other_value, setting.value] if setting.value != other_value end end |
#[](key) ⇒ Object
The given setting's current value.
76 77 78 79 80 |
# File 'lib/mediawiki-vagrant/settings.rb', line 76 def [](key) key = normalize_key(key) @settings[key] && @settings[key].value end |
#[]=(key, value) ⇒ Object
Changes the given setting's value.
84 85 86 87 88 89 90 91 92 |
# File 'lib/mediawiki-vagrant/settings.rb', line 84 def []=(key, value) key = normalize_key(key) if @settings.include?(key) @settings[key].value = value else @settings[key] = Setting.new(key, value) end end |
#combine(other) ⇒ Object
Combine the given settings with the current ones.
111 112 113 |
# File 'lib/mediawiki-vagrant/settings.rb', line 111 def combine(other) other.each { |key, value| setting(key).combine!(value) } if other.is_a?(Hash) end |
#each {|name, setting| ... } ⇒ Object
Iterate over each setting.
121 122 123 |
# File 'lib/mediawiki-vagrant/settings.rb', line 121 def each(&blk) @settings.each(&blk) end |
#load(path_or_io) ⇒ Object
Load YAML-serialized settings from the given path. If a directory is given, all files matching *.yaml are loaded.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mediawiki-vagrant/settings.rb', line 130 def load(path_or_io) case path_or_io when String, Pathname path = Pathname.new(path_or_io) path = path.join('*.{yaml,yml}') if path.directory? Dir.glob(path).each { |f| load(File.new(f)) } else update(YAML.load(path_or_io)) end end |
#required ⇒ Hash
Returns only those of the current settings that are required (have no default value).
147 148 149 |
# File 'lib/mediawiki-vagrant/settings.rb', line 147 def required select { |_name, setting| setting.default.nil? } end |
#save(path_or_io) ⇒ Integer
Serializes and saves the current settings to the given file path.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mediawiki-vagrant/settings.rb', line 157 def save(path_or_io) yaml = YAML.dump(to_yaml_hash) case path_or_io when String, Pathname File.open(path_or_io, 'w') { |f| f.write(yaml) } else path_or_io.write(yaml) end end |
#setting(name) ⇒ Setting
Returns the setting with the given name.
174 175 176 |
# File 'lib/mediawiki-vagrant/settings.rb', line 174 def setting(name) @settings[normalize_key(name)] end |
#unset!(name) ⇒ Object
Unset the given setting.
191 192 193 |
# File 'lib/mediawiki-vagrant/settings.rb', line 191 def unset!(name) (s = setting(name)) && s.unset! end |
#update(other) ⇒ Object
Updates settings from the given `{ name: value }` hash.
182 183 184 185 |
# File 'lib/mediawiki-vagrant/settings.rb', line 182 def update(other) other.each { |key, value| self[key] = value } if other.is_a?(Hash) self end |