Module: ARSettings

Defined in:
lib/arsettings/packaged.rb,
lib/arsettings/arsettings.rb,
lib/arsettings/has_settings.rb,
lib/arsettings/settings_class/class_methods.rb,
lib/arsettings/settings_class/instance_methods.rb

Defined Under Namespace

Modules: HasSettings, SettingsClass Classes: Packaged

Constant Summary collapse

AlreadyDefinedError =
Class.new(Exception)
NoSuchSettingError =
Class.new(Exception)
InvalidNameError =
Class.new(Exception)
InvalidPackageError =
Class.new(Exception)
InvalidOptionError =
Class.new(Exception)
NoDefaultPackageError =
Class.new(Exception)
UninitializedSettingError =
Class.new(Exception)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_classObject

the class that will be used when you inherit from AR::B, or if you don’t specify in ::on



36
37
38
# File 'lib/arsettings/arsettings.rb', line 36

def default_class
  @default_class
end

Class Method Details

.create(classname, options = Hash.new) ⇒ Object Also known as: create_settings_class

create the settings class



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/arsettings/arsettings.rb', line 12

def self.create( classname , options=Hash.new )
  raise AlreadyDefinedError.new("you are trying to define the settings class #{classname}, but it already exists") if Object.constants.map { |c| c.to_s }.include?(classname.to_s)
  validate_options options , :volatile , :max_chars
  Object.const_set classname , Class.new(ActiveRecord::Base)
  klass = Object.const_get(classname).class_eval do
    extend  SettingsClass::ClassMethods
    include SettingsClass::InstanceMethods
    const_set :MAX_CHARS           , options.fetch( :max_chars , 30   )
    const_set :VOLATILIE_DEFAULT   , options.fetch( :volatile  , true )
    send :load_from_db
    self
  end
  @default_class ||= klass
  klass
end

.deserialize(data) ⇒ Object

:nodoc:



52
53
54
# File 'lib/arsettings/arsettings.rb', line 52

def self.deserialize(data) # :nodoc:
  YAML::load(data)
end

.on(object, options = Hash.new) ⇒ Object

can be used to put settings on any object



40
41
42
43
44
45
46
# File 'lib/arsettings/arsettings.rb', line 40

def self.on( object , options = Hash.new )
  settings_class = options.fetch :settings_class , default_class
  raise NoDefaultPackageError.new("You did not specify a settings class, and no default is set (make sure you have already invoked create_settings_class)") unless settings_class
  validate_options options , :settings_class
  object.instance_variable_set( '@arsettings_package' , settings_class.package(object) )
  (class << object ; self ; end).send :include , HasSettings
end

.serialize(data) ⇒ Object

:nodoc:



48
49
50
# File 'lib/arsettings/arsettings.rb', line 48

def self.serialize(data) # :nodoc:
  YAML::dump(data)
end

.validate_options(options, *valid_options) ⇒ Object

:nodoc:



56
57
58
59
60
61
62
# File 'lib/arsettings/arsettings.rb', line 56

def self.validate_options(options,*valid_options) # :nodoc:
  options.each do |option,value|
    unless valid_options.include? option
      raise ARSettings::InvalidOptionError.new("#{option.inspect} is not a valid option, because it is not in #{valid_options.inspect}")
    end
  end
end