Class: Middleman::Configuration::ConfigurationManager

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

Constructor Details

#initializeConfigurationManager

Returns a new instance of ConfigurationManager.



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

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



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

def method_missing(method, *args)
  if defines_setting?(method) && args.empty?
    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.

Returns:

  • (Object)


28
29
30
31
# File 'lib/middleman-core/configuration.rb', line 28

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)


36
37
38
39
# File 'lib/middleman-core/configuration.rb', line 36

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:



16
17
18
# File 'lib/middleman-core/configuration.rb', line 16

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

#define_setting(key, default = nil, description = nil, options = {}) ⇒ 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) (defaults to: {})

    Additional options.

Returns:



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

def define_setting(key, default=nil, description=nil, options={})
  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)
end

#defines_setting?(key) ⇒ Boolean

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

Parameters:

  • key (Symbol)

Returns:

  • (Boolean)


60
61
62
# File 'lib/middleman-core/configuration.rb', line 60

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

#dupObject

Deep duplicate of the configuration manager



90
91
92
# File 'lib/middleman-core/configuration.rb', line 90

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.



84
85
86
87
# File 'lib/middleman-core/configuration.rb', line 84

def finalize!
  @finalized = true
  self
end

#load_settings(other_settings) ⇒ Object

Load in a list of settings



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

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)


53
54
55
# File 'lib/middleman-core/configuration.rb', line 53

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.

Returns:



22
23
24
# File 'lib/middleman-core/configuration.rb', line 22

def setting(key)
  @settings[key]
end

#to_hObject



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

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

#to_sObject



110
111
112
# File 'lib/middleman-core/configuration.rb', line 110

def to_s
  to_h.inspect
end