Class: Middleman::Configuration::ConfigurationManager

Inherits:
Object
  • Object
show all
Defined in:
middleman-core/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

Constructor Details

#initializeConfigurationManager

Returns a new instance of ConfigurationManager


10
11
12
13
14
# File 'middleman-core/lib/middleman-core/configuration.rb', line 10

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


44
45
46
47
48
49
50
51
52
# File 'middleman-core/lib/middleman-core/configuration.rb', line 44

def method_missing(method, *args)
  if defines_setting?(method) && args.empty?
    self[method]
  elsif method.to_s =~ /^(\w+)=$/ && args.size == 1
    self[Regexp.last_match(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.

Returns:

  • (Object)

30
31
32
33
# File 'middleman-core/lib/middleman-core/configuration.rb', line 30

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.

Parameters:

  • key (Symbol)
  • val (Object)

38
39
40
41
# File 'middleman-core/lib/middleman-core/configuration.rb', line 38

def []=(key, val)
  setting_obj = setting(key) || define_setting(key)
  setting_obj.value = val
end

#all_settingsArray<ConfigSetting>

Get all settings, sorted by key, as ConfigSetting objects.

Returns:


18
19
20
# File 'middleman-core/lib/middleman-core/configuration.rb', line 18

def all_settings
  @settings.values.sort_by(&:key)
end

#define_setting(key, default = nil, description = nil, options_hash = ::Middleman::EMPTY_HASH) ⇒ ConfigSetting

Define a new setting, with optional default and user-friendly description. Once the configuration manager is finalized, no new settings may be defined.

Examples:

config.define_setting :compress, false, 'Whether to compress the output'

Parameters:

  • key (Symbol)

    The name of the option

  • default (Object) (defaults to: nil)

    The default value for the option

  • description (String) (defaults to: nil)

    A human-readable description of what the option does

  • options (Hash)

    Additional options.

Returns:


76
77
78
79
80
81
82
# File 'middleman-core/lib/middleman-core/configuration.rb', line 76

def define_setting(key, default = nil, description = nil, options_hash = ::Middleman::EMPTY_HASH)
  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, options_hash)
end

#defines_setting?(key) ⇒ Boolean

Does this configuration manager know about the setting identified by key?

Parameters:

  • key (Symbol)

Returns:

  • (Boolean)

62
63
64
# File 'middleman-core/lib/middleman-core/configuration.rb', line 62

def defines_setting?(key)
  @settings.key?(key)
end

#dupObject

Deep duplicate of the configuration manager


92
93
94
# File 'middleman-core/lib/middleman-core/configuration.rb', line 92

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.


86
87
88
89
# File 'middleman-core/lib/middleman-core/configuration.rb', line 86

def finalize!
  @finalized = true
  self
end

#load_settings(other_settings) ⇒ Object

Load in a list of settings


97
98
99
100
101
102
# File 'middleman-core/lib/middleman-core/configuration.rb', line 97

def load_settings(other_settings)
  other_settings.each do |setting|
    new_setting = define_setting(setting.key, setting.default, setting.description, setting.options)
    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

Returns:

  • (Boolean)

55
56
57
# File 'middleman-core/lib/middleman-core/configuration.rb', line 55

def respond_to?(method, include_private = false)
  super || defines_setting?(method) || (method =~ /^(\w+)=$/ && defines_setting?(Regexp.last_match(1)))
end

#setting(key) ⇒ ConfigSetting

Get a full ConfigSetting object for the setting with the give key.

Returns:


24
25
26
# File 'middleman-core/lib/middleman-core/configuration.rb', line 24

def setting(key)
  @settings[key]
end

#to_hObject


104
105
106
107
108
109
110
# File 'middleman-core/lib/middleman-core/configuration.rb', line 104

def to_h
  hash = {}
  @settings.each do |key, setting|
    hash[key] = setting.value
  end
  hash
end

#to_sObject


112
113
114
# File 'middleman-core/lib/middleman-core/configuration.rb', line 112

def to_s
  to_h.inspect
end