Class: ARSettings::Packaged

Inherits:
Object
  • Object
show all
Defined in:
lib/arsettings/packaged.rb

Constant Summary collapse

InvalidSettingsClassError =
Class.new Exception
PASSTHROUGH =

:nodoc:

lambda { |val| val }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings_class, package) ⇒ Packaged

instance methods



41
42
43
# File 'lib/arsettings/packaged.rb', line 41

def initialize(settings_class,package) # :nodoc:
  @package , @settings_class , @settings = package.to_s.to_sym , settings_class , Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

:nodoc:



146
147
148
149
150
151
152
153
154
155
# File 'lib/arsettings/packaged.rb', line 146

def method_missing(name,*args) # :nodoc:
  name_to_s = name.to_s
  if name_to_s =~ /\A[A-Z]/
    settings_class.const_get name , *args
  elsif name_to_s !~ /=$/ || ( name_to_s =~ /=$/ && args.size == 1 )
    raise ARSettings::NoSuchSettingError.new("There is no setting named #{name_to_s.chomp '='}")
  else
    super
  end
end

Instance Attribute Details

#packageObject (readonly)

Returns the value of attribute package.



7
8
9
# File 'lib/arsettings/packaged.rb', line 7

def package
  @package
end

#settings_classObject (readonly)

Returns the value of attribute settings_class.



7
8
9
# File 'lib/arsettings/packaged.rb', line 7

def settings_class
  @settings_class
end

Class Method Details

.has_instance?(settings_class, package) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


19
20
21
# File 'lib/arsettings/packaged.rb', line 19

def self.has_instance?(settings_class,package) # :nodoc:
  @instances && @instances[settings_class] && @instances[settings_class][normalize(package)]
end

.instance(settings_class, package) ⇒ Object

:nodoc:



11
12
13
14
15
16
17
# File 'lib/arsettings/packaged.rb', line 11

def self.instance(settings_class,package) # :nodoc:
  validate(settings_class,package)
  package = normalize package
  @instances ||= Hash.new
  @instances[settings_class] ||= Hash.new
  @instances[settings_class][package] ||= new(settings_class,package)
end

.validate(settings_class, package) ⇒ Object

:nodoc:



23
24
25
26
# File 'lib/arsettings/packaged.rb', line 23

def self.validate(settings_class,package) # :nodoc:
  validate_package(package)
  validate_settings_class(settings_class)
end

.validate_package(package) ⇒ Object

:nodoc:



28
29
30
31
# File 'lib/arsettings/packaged.rb', line 28

def self.validate_package(package) # :nodoc:
  raise ARSettings::InvalidPackageError.new("#{package.inspect} should be a String/Symbol/Class") unless 
      String === package || Symbol === package || Class === package
end

.validate_settings_class(settings_class) ⇒ Object

:nodoc:



33
34
35
36
# File 'lib/arsettings/packaged.rb', line 33

def self.validate_settings_class(settings_class) # :nodoc:
  raise InvalidSettingsClassError.new("#{settings_class.inspect} should be a class created by the create_settings_class method") unless 
      Class === settings_class && settings_class.superclass == ActiveRecord::Base && SettingsClass::ClassMethods === settings_class
end

Instance Method Details

#add(name, options = {}, &proc) ⇒ Object Also known as: has_setting



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/arsettings/packaged.rb', line 56

def add( name , options={} , &proc )
  return(add_from_instance name[:record]) if name.is_a? Hash # internal use only
  ARSettings.validate_options options , :volatile , :default
  name = name.to_sym
  validate_name(name)
  if setting? name
    @settings[name].volatile        =  options[:volatile]  if options.has_key? :volatile
    @settings[name].postprocessing  =  proc                if proc
  else
    add_setter(name)
    add_getter(name)
    add_boolean_getter(name)
    @settings[name] = settings_class.new :name => name.to_s , :postprocessing => proc || PASSTHROUGH , :volatile => !!options.fetch(:volatile,settings_class::VOLATILIE_DEFAULT) , :package => package
    send "#{name}=" , options[:default] if options.has_key?(:default)
  end
  self
end

#resetObject

Destroy all of this package’s settings WARNING This will wipe them out of the database



46
47
48
49
50
51
52
53
54
# File 'lib/arsettings/packaged.rb', line 46

def reset
  (@settings||{}).each do |name,instance| 
    remove_setter(name)
    remove_getter(name)
    remove_boolean_getter(name)
    instance.destroy
  end
  @settings = Hash.new
end

#setting?(name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/arsettings/packaged.rb', line 76

def setting?(name)
  @settings.has_key? name
end

#settingsObject



80
81
82
# File 'lib/arsettings/packaged.rb', line 80

def settings
  @settings.keys
end

#settings_with_valuesObject



84
85
86
# File 'lib/arsettings/packaged.rb', line 84

def settings_with_values
  @settings.map { |name,instance| [name,instance.value] }
end