Class: ARSettings::Packaged
- Inherits:
-
Object
- Object
- ARSettings::Packaged
- Defined in:
- lib/arsettings/packaged.rb
Constant Summary collapse
- InvalidSettingsClassError =
Class.new Exception
- PASSTHROUGH =
:nodoc:
lambda { |val| val }
Instance Attribute Summary collapse
-
#package ⇒ Object
readonly
Returns the value of attribute package.
-
#settings_class ⇒ Object
readonly
Returns the value of attribute settings_class.
Class Method Summary collapse
-
.has_instance?(settings_class, package) ⇒ Boolean
:nodoc:.
-
.instance(settings_class, package) ⇒ Object
:nodoc:.
-
.validate(settings_class, package) ⇒ Object
:nodoc:.
-
.validate_package(package) ⇒ Object
:nodoc:.
-
.validate_settings_class(settings_class) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #add(name, options = {}, &proc) ⇒ Object (also: #has_setting)
-
#initialize(settings_class, package) ⇒ Packaged
constructor
instance methods.
-
#reset ⇒ Object
Destroy all of this package’s settings WARNING This will wipe them out of the database.
- #setting?(name) ⇒ Boolean
- #settings ⇒ Object
- #settings_with_values ⇒ Object
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
#package ⇒ Object (readonly)
Returns the value of attribute package.
7 8 9 |
# File 'lib/arsettings/packaged.rb', line 7 def package @package end |
#settings_class ⇒ Object (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:
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 , ={} , &proc ) return(add_from_instance name[:record]) if name.is_a? Hash # internal use only ARSettings. , :volatile , :default name = name.to_sym validate_name(name) if setting? name @settings[name].volatile = [:volatile] if .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 => !!.fetch(:volatile,settings_class::VOLATILIE_DEFAULT) , :package => package send "#{name}=" , [:default] if .has_key?(:default) end self end |
#reset ⇒ Object
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
76 77 78 |
# File 'lib/arsettings/packaged.rb', line 76 def setting?(name) @settings.has_key? name end |
#settings ⇒ Object
80 81 82 |
# File 'lib/arsettings/packaged.rb', line 80 def settings @settings.keys end |
#settings_with_values ⇒ Object
84 85 86 |
# File 'lib/arsettings/packaged.rb', line 84 def settings_with_values @settings.map { |name,instance| [name,instance.value] } end |