Class: MediaWikiVagrant::Settings

Inherits:
Object
  • Object
show all
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.

Examples:

Define settings

Settings.define do
  setting :vagrant_ram,
    description: "Amount (in MB) of memory allocated to the VM",
    help: "Specify more memory for hungry tasks like browser tests",
    default: 1024,
    coercion: ->(setting, value) { [setting.default, value].max }

Load settings from a file

settings = Settings.new
settings.load("file/path.yaml")

Load settings from all .yaml files in a directory

settings = Settings.new
settings.load("settings.d")

Change a setting and save to a file

settings = Settings.new
settings[:vagrant_ram] = 2048
settings.save("file/path.yaml")

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SettingsDefiner

included

Constructor Details

#initializeSettings

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.

Parameters:

  • path (String)

    Path to settings file.



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.

Parameters:

Returns:

  • (Hash)

    Hash of { setting => [current, new] }



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.

Parameters:

  • other (Hash)

    Other settings.



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.

Yields:

Yield Parameters:



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.

Parameters:

  • path_or_io (String, IO)

    Path to file, open IO, or directory



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

#requiredHash

Returns only those of the current settings that are required (have no default value).

Returns:

  • (Hash)


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.

Parameters:

  • path_or_io (String, #write)

    File path or open IO object.

Returns:

  • (Integer)

    Number of bytes written.



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.

Parameters:

  • name (String, Symbol)

    Setting name

Returns:



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.

Parameters:

  • name (String)

    Setting name.



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.

Parameters:

  • other (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