Class: ActiveConfiguration::SettingManager

Inherits:
Object
  • Object
show all
Defined in:
lib/active_configuration/setting_manager.rb

Overview

Returns a SettingProxy object for any option that has been configured.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configurable) ⇒ SettingManager

Initializes this SettingManager and keeps track of what model this SettingManager is proxying settings for.

Parameters:

  • configurable (ActiveRecord::Base)

    the model that hsa been configured for use with ActiveConfiguration.



15
16
17
18
# File 'lib/active_configuration/setting_manager.rb', line 15

def initialize(configurable)
  @configurable = configurable
  @settings     = Hash.new
end

Instance Attribute Details

#configurableObject

Returns the value of attribute configurable.



7
8
9
# File 'lib/active_configuration/setting_manager.rb', line 7

def configurable
  @configurable
end

#settingsObject

Returns the value of attribute settings.



8
9
10
# File 'lib/active_configuration/setting_manager.rb', line 8

def settings
  @settings
end

Instance Method Details

#[](key) ⇒ Hash, ...

Provides access to setting details for a setting at the given key.

Parameters:

  • key (Symbol)

    the key of the requested setting.

Returns:

  • (Hash, Array, NilClass)

    the Hash or Array of Hashes for the setting with the given key or nil if there isn’t a match.



26
27
28
29
30
31
32
33
34
# File 'lib/active_configuration/setting_manager.rb', line 26

def [](key)
  if @configurable.class.configuration.options.has_key?(key)
    @settings[key] ||= SettingProxy.new(self, key)

    return @settings[key].value
  end

  return nil
end

#[]=(key, value) ⇒ Hash, ...

Replaces the Hash or Array of Hashes for the setting with the given key with the given value.

Parameters:

  • key (Symbol)

    the key of the requested setting.

Returns:

  • (Hash, Array, NilClass)

    the Hash or Array of Hashes for the setting with the given key or nil if there isn’t a match.



43
44
45
46
47
48
49
50
51
52
# File 'lib/active_configuration/setting_manager.rb', line 43

def []=(key, value)
  if @configurable.class.configuration.options.has_key?(key)
    @settings[key] ||= SettingProxy.new(self, key)
    @settings[key].replace(value)

    return @settings[key].value
  end

  return nil
end

#reloadObject

Resets any pending setting modifications.



90
91
92
# File 'lib/active_configuration/setting_manager.rb', line 90

def reload
  @settings = Hash.new
end

#saveBoolean

Saves all settings with pending modificaitons.

Returns:

  • (Boolean)

    whether or not the save was successful.



72
73
74
# File 'lib/active_configuration/setting_manager.rb', line 72

def save
  return !settings.values.collect{|setting| setting.save}.include?(false)
end

#update_settings(replacement_settings = {}) ⇒ Boolean

Writes over multiple settings and saves all setting updates at once.

Parameters:

  • replacement_settings (Hash) (defaults to: {})

    the has of settings to be set.

Returns:

  • (Boolean)

    whether or not the save was successful.



81
82
83
84
85
86
87
# File 'lib/active_configuration/setting_manager.rb', line 81

def update_settings(replacement_settings = {})
  write_settings(replacement_settings)

  validate

  return (@configurable.errors[:settings].empty? ? save : false)
end

#validateObject

Runs validations against all settings with pending modificaitons. Any errors are added to @configurable.errors.



65
66
67
# File 'lib/active_configuration/setting_manager.rb', line 65

def validate
  settings.values.collect{|setting| setting.validate}
end

#write_settings(replacement_settings = {}) ⇒ Object

Writes over multiple settings at once.

Parameters:

  • replacement_settings (Hash) (defaults to: {})

    the has of settings to be set.



57
58
59
60
61
# File 'lib/active_configuration/setting_manager.rb', line 57

def write_settings(replacement_settings = {})
  replacement_settings.each_pair do |key, value|
    self[key] = value
  end
end