arsettings 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +14 -13
- data/Readme.mdown +13 -18
- data/lib/arsettings/arsettings.rb +4 -4
- data/lib/arsettings/packaged.rb +56 -55
- data/lib/arsettings/settings_class_methods.rb +21 -7
- data/lib/arsettings/settings_instance_methods.rb +1 -1
- metadata +2 -7
data/Rakefile
CHANGED
@@ -1,11 +1,19 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake/gempackagetask"
|
3
|
+
require "rake/rdoctask"
|
4
|
+
# require "rake/testtask"
|
5
|
+
|
6
|
+
|
1
7
|
task :default => :test
|
2
8
|
|
9
|
+
|
3
10
|
desc 'run the unit tests'
|
4
11
|
task :test do
|
5
12
|
query = File.dirname(__FILE__) << '/test/*_test.rb'
|
6
13
|
Dir[query].each { |filename| require filename }
|
7
14
|
end
|
8
15
|
|
16
|
+
|
9
17
|
desc 'irb session with env loaded'
|
10
18
|
task :console do
|
11
19
|
dir = File.dirname(__FILE__)
|
@@ -14,18 +22,6 @@ task :console do
|
|
14
22
|
system "irb -f #{requirements}"
|
15
23
|
end
|
16
24
|
|
17
|
-
require "rubygems"
|
18
|
-
require "rake/gempackagetask"
|
19
|
-
require "rake/rdoctask"
|
20
|
-
|
21
|
-
require "rake/testtask"
|
22
|
-
Rake::TestTask.new do |t|
|
23
|
-
t.libs << "test"
|
24
|
-
t.test_files = FileList["test/**/*_test.rb"]
|
25
|
-
t.verbose = true
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
25
|
|
30
26
|
# This builds the actual gem. For details of what all these options
|
31
27
|
# mean, and other ones you can add, check the documentation here:
|
@@ -36,7 +32,7 @@ spec = Gem::Specification.new do |s|
|
|
36
32
|
|
37
33
|
# Change these as appropriate
|
38
34
|
s.name = "arsettings"
|
39
|
-
s.version = "1.0.
|
35
|
+
s.version = "1.0.1"
|
40
36
|
s.author = "Joshua Cheek"
|
41
37
|
s.email = "josh.cheek@gmail.com"
|
42
38
|
s.homepage = "https://github.com/JoshCheek/ARSettings"
|
@@ -89,3 +85,8 @@ desc 'Clear out RDoc and generated packages'
|
|
89
85
|
task :clean => [:clobber_rdoc, :clobber_package] do
|
90
86
|
rm "#{spec.name}.gemspec"
|
91
87
|
end
|
88
|
+
|
89
|
+
# desc 'generate docs'
|
90
|
+
# task :doc do
|
91
|
+
# system "rdoc --force-update --format=darkfish --main=lib/arsettings.rb --title=ARSettings --main=arsettings.rb #{File.dirname(__FILE__)}/lib"
|
92
|
+
# end
|
data/Readme.mdown
CHANGED
@@ -1,37 +1,32 @@
|
|
1
1
|
Description
|
2
2
|
===========
|
3
3
|
|
4
|
+
ActiveRecord has a lot of support for tables of similar values. But what about those one time only values, like site settings? This is what ARSettings is intended for. One line to add settings to your ActiveRecord classes. Two to non-ActiveRecord classes. And you can have settings that are not defined on any class as well.
|
5
|
+
|
4
6
|
Usage
|
5
7
|
=====
|
6
8
|
|
7
|
-
|
8
|
-
Test
|
9
|
-
====
|
10
|
-
|
11
|
-
$ rake
|
12
|
-
|
9
|
+
Create a table to store your settings ([see example](https://github.com/JoshCheek/ARSettings/blob/master/example/helper.rb)), and then declare your settings as you like ([see examples](https://github.com/JoshCheek/ARSettings/blob/master/example/example.rb)).
|
13
10
|
|
14
11
|
Dependencies
|
15
12
|
============
|
16
13
|
|
17
|
-
*
|
18
|
-
* [
|
19
|
-
|
14
|
+
* Ruby 1.9
|
15
|
+
* [ActiveRecord](http://rubygems.org/gems/activerecord) Tested on versions 2.3.8 and 3.0.1
|
16
|
+
* [sqlite-ruby](http://rubygems.org/gems/sqlite-ruby) Tested on version 1.3.1
|
20
17
|
|
21
|
-
TODO
|
22
|
-
====
|
23
18
|
|
24
|
-
|
25
|
-
|
26
|
-
* Turn into gem
|
19
|
+
Bugs / Contribution
|
20
|
+
===================
|
27
21
|
|
28
|
-
|
22
|
+
If you discover any errors / bugs, please inform me, and I will fix them and push out a new version.
|
29
23
|
|
30
|
-
|
24
|
+
If you wish to fix it yourself, fork it, fix it, and send me a pull request.
|
31
25
|
|
32
|
-
|
26
|
+
If you think it would fit your use better if it did ______ or if the interface would be a lot nicer given _______ then let me know so I can consider that and make the gem better.
|
33
27
|
|
34
|
-
|
28
|
+
License
|
29
|
+
=======
|
35
30
|
|
36
31
|
Copyright (c) 2010 Joshua Cheek
|
37
32
|
|
@@ -34,7 +34,7 @@ module ARSettings
|
|
34
34
|
settings_class = options.fetch :settings_class , default_class
|
35
35
|
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
|
36
36
|
validate_options options , :settings_class
|
37
|
-
(class << object ; self ; end).send :define_method , :has_setting do |name,inner_options={},&block|
|
37
|
+
(class << object ; self ; end).send :define_method , :has_setting do |name,inner_options={},&block| # :nodoc:
|
38
38
|
instance = inner_options.delete :instance
|
39
39
|
package = settings_class.package(object)
|
40
40
|
package.add name , inner_options , &block
|
@@ -54,15 +54,15 @@ module ARSettings
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
def self.serialize(data)
|
57
|
+
def self.serialize(data) # :nodoc:
|
58
58
|
YAML::dump(data)
|
59
59
|
end
|
60
60
|
|
61
|
-
def self.deserialize(data)
|
61
|
+
def self.deserialize(data) # :nodoc:
|
62
62
|
YAML::load(data)
|
63
63
|
end
|
64
64
|
|
65
|
-
def self.validate_options(options,*valid_options)
|
65
|
+
def self.validate_options(options,*valid_options) # :nodoc:
|
66
66
|
options.each do |option,value|
|
67
67
|
unless valid_options.include? option
|
68
68
|
raise ARSettings::InvalidOptionError.new "#{option.inspect} is not a valid option, because it is not in #{valid_options.inspect}"
|
data/lib/arsettings/packaged.rb
CHANGED
@@ -2,13 +2,13 @@ module ARSettings
|
|
2
2
|
class Packaged
|
3
3
|
|
4
4
|
InvalidSettingsClassError = Class.new Exception
|
5
|
-
PASSTHROUGH = lambda { |val| val }
|
5
|
+
PASSTHROUGH = lambda { |val| val } # :nodoc:
|
6
6
|
|
7
7
|
attr_reader :package , :settings_class
|
8
8
|
|
9
9
|
private_class_method :new
|
10
10
|
|
11
|
-
def self.instance(settings_class,package)
|
11
|
+
def self.instance(settings_class,package) # :nodoc:
|
12
12
|
validate(settings_class,package)
|
13
13
|
package = normalize package
|
14
14
|
@instances ||= Hash.new
|
@@ -16,42 +16,33 @@ module ARSettings
|
|
16
16
|
@instances[settings_class][package] ||= new(settings_class,package)
|
17
17
|
end
|
18
18
|
|
19
|
-
def self.has_instance?(settings_class,package)
|
19
|
+
def self.has_instance?(settings_class,package) # :nodoc:
|
20
20
|
@instances && @instances[settings_class] && @instances[settings_class][normalize package]
|
21
21
|
end
|
22
22
|
|
23
|
-
def self.validate(settings_class,package)
|
23
|
+
def self.validate(settings_class,package) # :nodoc:
|
24
24
|
validate_package(package)
|
25
25
|
validate_settings_class(settings_class)
|
26
26
|
end
|
27
27
|
|
28
|
-
def self.validate_package(package)
|
28
|
+
def self.validate_package(package) # :nodoc:
|
29
29
|
raise ARSettings::InvalidPackageError.new("#{package.inspect} should be a String/Symbol/Class") unless
|
30
30
|
String === package || Symbol === package || Class === package
|
31
31
|
end
|
32
32
|
|
33
|
-
def self.validate_settings_class(settings_class)
|
33
|
+
def self.validate_settings_class(settings_class) # :nodoc:
|
34
34
|
raise InvalidSettingsClassError.new("#{settings_class.inspect} should be a class created by the create_settings_class method") unless
|
35
35
|
Class === settings_class && settings_class.superclass == ActiveRecord::Base && SettingsClass_ClassMethods === settings_class
|
36
36
|
end
|
37
|
-
|
38
|
-
def self.instances(settings_class)
|
39
|
-
@instances ||= Hash.new
|
40
|
-
@instances[settings_class] || Hash.new
|
41
|
-
end
|
42
|
-
|
43
|
-
def self.normalize(package)
|
44
|
-
return package if Symbol === package
|
45
|
-
package.to_s.to_sym
|
46
|
-
end
|
47
37
|
|
48
38
|
|
49
39
|
# instance methods
|
50
|
-
|
51
|
-
def initialize(settings_class,package)
|
40
|
+
|
41
|
+
def initialize(settings_class,package) # :nodoc:
|
52
42
|
@package , @settings_class , @settings = package.to_s.to_sym , settings_class , Hash.new
|
53
43
|
end
|
54
44
|
|
45
|
+
# Destroy all of this package's settings *WARNING* This will wipe them out of the database
|
55
46
|
def reset
|
56
47
|
(@settings||{}).each do |name,instance|
|
57
48
|
remove_setter(name)
|
@@ -62,12 +53,6 @@ module ARSettings
|
|
62
53
|
@settings = Hash.new
|
63
54
|
end
|
64
55
|
|
65
|
-
def validate_name(name)
|
66
|
-
raise ARSettings::InvalidNameError.new("#{name} is #{name.to_s.size}, but MAX_CHARS is set to #{settings_class::MAX_CHARS}") if name.to_s.length > settings_class::MAX_CHARS
|
67
|
-
regex = /\A[a-z_][a-zA-Z_]*\Z/m
|
68
|
-
raise ARSettings::InvalidNameError.new("#{name.inspect} is not a valid settings name, because it is not a valid method name since it does not match #{regex.inspect}") if name.to_s !~ regex
|
69
|
-
end
|
70
|
-
|
71
56
|
def add( name , options={} , &proc )
|
72
57
|
return(add_from_instance name[:record]) if name.is_a? Hash # internal use only
|
73
58
|
ARSettings.validate_options options , :volatile , :default
|
@@ -89,48 +74,74 @@ module ARSettings
|
|
89
74
|
def setting?(name)
|
90
75
|
@settings.has_key? name
|
91
76
|
end
|
77
|
+
|
78
|
+
def settings
|
79
|
+
@settings.keys
|
80
|
+
end
|
81
|
+
|
82
|
+
def settings_with_values
|
83
|
+
@settings.map { |name,instance| [name,instance.value] }
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def self.instances(settings_class) # :nodoc:
|
89
|
+
@instances ||= Hash.new
|
90
|
+
@instances[settings_class] || Hash.new
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.normalize(package) # :nodoc:
|
94
|
+
return package if Symbol === package
|
95
|
+
package.to_s.to_sym
|
96
|
+
end
|
92
97
|
|
93
|
-
def
|
98
|
+
def validate_name(name) # :nodoc:
|
99
|
+
raise ARSettings::InvalidNameError.new("#{name} is #{name.to_s.size}, but MAX_CHARS is set to #{settings_class::MAX_CHARS}") if name.to_s.length > settings_class::MAX_CHARS
|
100
|
+
regex = /\A[a-z_][a-zA-Z_]*\Z/m
|
101
|
+
raise ARSettings::InvalidNameError.new("#{name.inspect} is not a valid settings name, because it is not a valid method name since it does not match #{regex.inspect}") if name.to_s !~ regex
|
102
|
+
end
|
103
|
+
|
104
|
+
def metaclass # :nodoc:
|
94
105
|
class << self
|
95
106
|
self
|
96
107
|
end
|
97
108
|
end
|
98
|
-
|
99
|
-
def define_method( name , &body )
|
109
|
+
|
110
|
+
def define_method( name , &body ) # :nodoc:
|
100
111
|
metaclass.send :define_method , name , &body
|
101
112
|
end
|
102
|
-
|
103
|
-
def remove_method(name)
|
113
|
+
|
114
|
+
def remove_method(name) # :nodoc:
|
104
115
|
metaclass.send :remove_method , "#{setting}="
|
105
116
|
end
|
106
|
-
|
107
|
-
def add_setter(name)
|
117
|
+
|
118
|
+
def add_setter(name) # :nodoc:
|
108
119
|
define_method "#{name}=" do |value|
|
109
120
|
@settings[name].value = @settings[name].postprocessing.call(value)
|
110
121
|
end
|
111
122
|
end
|
112
|
-
|
113
|
-
def add_getter(name)
|
123
|
+
|
124
|
+
def add_getter(name) # :nodoc:
|
114
125
|
define_method(name) { @settings[name].value }
|
115
126
|
end
|
116
|
-
|
117
|
-
def add_boolean_getter(name)
|
127
|
+
|
128
|
+
def add_boolean_getter(name) # :nodoc:
|
118
129
|
define_method("#{name}?") { !!@settings[name].value }
|
119
130
|
end
|
120
|
-
|
121
|
-
def remove_setter(name)
|
131
|
+
|
132
|
+
def remove_setter(name) # :nodoc:
|
122
133
|
metaclass.send :remove_method , "#{name}="
|
123
134
|
end
|
124
|
-
|
125
|
-
def remove_getter(name)
|
135
|
+
|
136
|
+
def remove_getter(name) # :nodoc:
|
126
137
|
metaclass.send :remove_method , name
|
127
138
|
end
|
128
|
-
|
129
|
-
def remove_boolean_getter(name)
|
139
|
+
|
140
|
+
def remove_boolean_getter(name) # :nodoc:
|
130
141
|
metaclass.send :remove_method , "#{name}?"
|
131
142
|
end
|
132
|
-
|
133
|
-
def method_missing(name,*args)
|
143
|
+
|
144
|
+
def method_missing(name,*args) # :nodoc:
|
134
145
|
if name.to_s =~ /\A[A-Z]/
|
135
146
|
settings_class.const_get name , *args
|
136
147
|
elsif name.to_s !~ /=$/ || ( name.to_s =~ /=$/ && args.size == 1 )
|
@@ -139,18 +150,8 @@ module ARSettings
|
|
139
150
|
super
|
140
151
|
end
|
141
152
|
end
|
142
|
-
|
143
|
-
def
|
144
|
-
@settings.keys
|
145
|
-
end
|
146
|
-
|
147
|
-
def settings_with_values
|
148
|
-
@settings.map { |name,instance| [name,instance.value] }
|
149
|
-
end
|
150
|
-
|
151
|
-
private
|
152
|
-
|
153
|
-
def add_from_instance(record)
|
153
|
+
|
154
|
+
def add_from_instance(record) # :nodoc:
|
154
155
|
record.postprocessing = PASSTHROUGH
|
155
156
|
name = record.name.to_sym
|
156
157
|
validate_name(name)
|
@@ -1,14 +1,28 @@
|
|
1
1
|
module ARSettings
|
2
2
|
module SettingsClass_ClassMethods
|
3
3
|
|
4
|
-
def reset_all
|
4
|
+
def reset_all # :nodoc:
|
5
5
|
Packaged.instances(self).each { |name,package| package.reset }
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
# get a Package (aka namespace or scope or group)
|
9
|
+
# to package settings together with
|
10
|
+
#
|
11
|
+
# email_settings = Settings.package :email
|
12
|
+
#
|
13
|
+
# email_settings.add :enabled , :default => false
|
14
|
+
#
|
15
|
+
# ProfileSettings = Settings.package :profile_settings
|
16
|
+
#
|
17
|
+
# ProfileSettings.add :enabled , :default => true
|
18
|
+
def package(name)
|
19
|
+
Packaged.instance self , name
|
10
20
|
end
|
11
21
|
|
22
|
+
# add a setting to the settings class, it will be packaged under the settings class itself
|
23
|
+
#
|
24
|
+
#--
|
25
|
+
# FIXME: Document the options and proc
|
12
26
|
def add( name , options={} , &proc )
|
13
27
|
options = name if name.is_a? Hash
|
14
28
|
if options[:package]
|
@@ -18,7 +32,9 @@ module ARSettings
|
|
18
32
|
end.add( name , options , &proc )
|
19
33
|
end
|
20
34
|
|
21
|
-
|
35
|
+
private
|
36
|
+
|
37
|
+
def method_missing(name,*args) # :nodoc:
|
22
38
|
if name =~ /\A[A-Z]/
|
23
39
|
const_get name , *args
|
24
40
|
elsif name.to_s !~ /=$/ || ( name.to_s =~ /=$/ && args.size == 1 )
|
@@ -27,10 +43,8 @@ module ARSettings
|
|
27
43
|
super
|
28
44
|
end
|
29
45
|
end
|
30
|
-
|
31
|
-
private
|
32
46
|
|
33
|
-
def load_from_db
|
47
|
+
def load_from_db # :nodoc:
|
34
48
|
reset_all
|
35
49
|
all.each { |instance| add :record => instance , :package => instance.package }
|
36
50
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module ARSettings
|
2
2
|
|
3
|
-
module SettingsClass_InstanceMethods
|
3
|
+
module SettingsClass_InstanceMethods # :nodoc: all
|
4
4
|
|
5
5
|
# unfortunately, can't serialize a proc. I tried both yaml and marshal
|
6
6
|
# so will have to keep it in memory and just be sure to set it each time app loads
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arsettings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 23
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 1
|
8
7
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
8
|
+
- 1
|
9
|
+
version: 1.0.1
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Joshua Cheek
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 19
|
30
28
|
segments:
|
31
29
|
- 2
|
32
30
|
- 3
|
@@ -42,7 +40,6 @@ dependencies:
|
|
42
40
|
requirements:
|
43
41
|
- - ">="
|
44
42
|
- !ruby/object:Gem::Version
|
45
|
-
hash: 25
|
46
43
|
segments:
|
47
44
|
- 1
|
48
45
|
- 3
|
@@ -82,7 +79,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
79
|
requirements:
|
83
80
|
- - ">="
|
84
81
|
- !ruby/object:Gem::Version
|
85
|
-
hash: 3
|
86
82
|
segments:
|
87
83
|
- 0
|
88
84
|
version: "0"
|
@@ -91,7 +87,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
87
|
requirements:
|
92
88
|
- - ">="
|
93
89
|
- !ruby/object:Gem::Version
|
94
|
-
hash: 3
|
95
90
|
segments:
|
96
91
|
- 0
|
97
92
|
version: "0"
|