arsettings 1.0.1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +33 -8
- data/Readme.mdown +19 -6
- data/examples/generic_settings.rb +23 -0
- data/examples/helper.rb +27 -0
- data/examples/namespaced.rb +33 -0
- data/examples/on_activerecord_class.rb +20 -0
- data/examples/on_any_class.rb +18 -0
- data/lib/arsettings/arsettings.rb +14 -23
- data/lib/arsettings/has_settings.rb +19 -0
- data/lib/arsettings/packaged.rb +4 -2
- data/lib/arsettings/settings_class/class_methods.rb +57 -0
- data/lib/arsettings/settings_class/instance_methods.rb +46 -0
- data/lib/arsettings.rb +19 -3
- data/test/_helper.rb +56 -0
- data/test/_in_memory_db.rb +73 -0
- data/test/_make_gemsets.sh +11 -0
- data/test/_rcov.sh +10 -0
- data/test/_reek.sh +8 -0
- data/test/_run_all.sh +11 -0
- data/test/_run_one.rb +3 -0
- data/test/activerecord_integration_test.rb +137 -0
- data/test/arbitrary_class_test.rb +156 -0
- data/test/packaged_test.rb +306 -0
- data/test/reset_test.rb +66 -0
- data/test/setting_choose_name_test.rb +15 -0
- data/test/setting_initialize_test.rb +113 -0
- data/test/setting_test.rb +203 -0
- metadata +51 -11
- data/lib/arsettings/activerecord.rb +0 -23
- data/lib/arsettings/settings_class_methods.rb +0 -53
- data/lib/arsettings/settings_instance_methods.rb +0 -45
data/Rakefile
CHANGED
@@ -7,12 +7,36 @@ require "rake/rdoctask"
|
|
7
7
|
task :default => :test
|
8
8
|
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
namespace :test do
|
11
|
+
desc 'create the gemsets'
|
12
|
+
task :make_gemsets do
|
13
|
+
sh File.dirname(__FILE__) << "/test/_make_gemsets.sh"
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'run the tests on current ruby/gemspec'
|
17
|
+
task :crnt do
|
18
|
+
require File.dirname(__FILE__) << "/test/_run_one"
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'check that all code is covered'
|
22
|
+
task :rcov do
|
23
|
+
sh File.dirname(__FILE__) << "/test/_rcov.sh"
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'run the tests on the supported rubies and gemsets'
|
27
|
+
task :all do
|
28
|
+
sh File.dirname(__FILE__) << "/test/_run_all.sh"
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'checks for code smells -- assumes you have reek installed'
|
32
|
+
task :reek do
|
33
|
+
sh File.dirname(__FILE__) << "/test/_reek.sh"
|
34
|
+
end
|
14
35
|
end
|
15
36
|
|
37
|
+
desc 'synonym for test:crnt'
|
38
|
+
task :test => 'test:crnt'
|
39
|
+
|
16
40
|
|
17
41
|
desc 'irb session with env loaded'
|
18
42
|
task :console do
|
@@ -32,7 +56,7 @@ spec = Gem::Specification.new do |s|
|
|
32
56
|
|
33
57
|
# Change these as appropriate
|
34
58
|
s.name = "arsettings"
|
35
|
-
s.version = "1.
|
59
|
+
s.version = "1.1.1"
|
36
60
|
s.author = "Joshua Cheek"
|
37
61
|
s.email = "josh.cheek@gmail.com"
|
38
62
|
s.homepage = "https://github.com/JoshCheek/ARSettings"
|
@@ -44,15 +68,16 @@ spec = Gem::Specification.new do |s|
|
|
44
68
|
s.rdoc_options = %w(--main Readme.mdown)
|
45
69
|
|
46
70
|
# Add any extra files to include in the gem
|
47
|
-
s.files = %w(Rakefile Readme.mdown) + Dir.glob("{test
|
71
|
+
s.files = %w(Rakefile Readme.mdown) + Dir.glob("{examples/**/*,test/**/*,lib/**/*}")
|
48
72
|
s.require_paths = ["lib"]
|
49
73
|
|
50
74
|
# If you want to depend on other gems, add them here, along with any
|
51
75
|
# relevant versions
|
52
|
-
s.add_dependency("activerecord", ">= 2.3.
|
76
|
+
s.add_dependency("activerecord", ">= 2.3.3")
|
53
77
|
|
54
78
|
# If your tests use any gems, include them here
|
55
|
-
s.add_development_dependency("
|
79
|
+
s.add_development_dependency("rake", "~> 0.8.7")
|
80
|
+
s.add_development_dependency("sqlite3-ruby", "~> 1.3.1")
|
56
81
|
end
|
57
82
|
|
58
83
|
# This task actually builds the gem. We also regenerate a static
|
data/Readme.mdown
CHANGED
@@ -3,18 +3,31 @@ Description
|
|
3
3
|
|
4
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
5
|
|
6
|
+
Tested on Ruby versions 1.8.6, 1.8.7, 1.9.2
|
7
|
+
Tested against ActiveRecord versions 2.3.3, 2.3.5, 2.3.8 and 3.0.1
|
8
|
+
If you would like me to test against a specific version not on here, let me know.
|
9
|
+
|
6
10
|
Usage
|
7
11
|
=====
|
8
12
|
|
9
|
-
|
13
|
+
First you need to create a table to store your settings, this will probably go in a migration, though the only requirement is that the schema is correct ([see example](https://github.com/JoshCheek/ARSettings/blob/master/examples/helper.rb))
|
10
14
|
|
11
|
-
|
15
|
+
* Use the settings class to store settings ([see example](https://github.com/JoshCheek/ARSettings/blob/master/examples/generic_settings.rb))
|
16
|
+
* Package settings together under a single namespace ([see example](https://github.com/JoshCheek/ARSettings/blob/master/examples/namespaced.rb))
|
17
|
+
* Add settings directly to your ActiveRecord class and its instances ([see example](https://github.com/JoshCheek/ARSettings/blob/master/examples/on_activerecord_class.rb))
|
18
|
+
* Add settings to any class you like ([see example](https://github.com/JoshCheek/ARSettings/blob/master/examples/on_any_class.rb))
|
19
|
+
|
20
|
+
|
21
|
+
Installation
|
12
22
|
============
|
13
23
|
|
14
|
-
|
15
|
-
|
16
|
-
|
24
|
+
gem install arsettings
|
25
|
+
|
26
|
+
|
27
|
+
Dependencies
|
28
|
+
============
|
17
29
|
|
30
|
+
* [ActiveRecord](http://rubygems.org/gems/activerecord)
|
18
31
|
|
19
32
|
Bugs / Contribution
|
20
33
|
===================
|
@@ -23,7 +36,7 @@ If you discover any errors / bugs, please inform me, and I will fix them and pus
|
|
23
36
|
|
24
37
|
If you wish to fix it yourself, fork it, fix it, and send me a pull request.
|
25
38
|
|
26
|
-
If you think it would fit your use better if it did
|
39
|
+
If you think it would fit your use better if it did this or if the interface would be a lot nicer given that then let me know so I can consider that and make the gem better.
|
27
40
|
|
28
41
|
License
|
29
42
|
=======
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/arsettings'
|
3
|
+
|
4
|
+
# tell it to create us a settings class named Settings
|
5
|
+
# this means you must have a table in your db named 'settings'
|
6
|
+
ARSettings.create_settings_class 'Settings'
|
7
|
+
|
8
|
+
# add and change a setting
|
9
|
+
Settings.has_setting :domain , :default => 'localhost:3000'
|
10
|
+
Settings.domain # => "localhost:3000"
|
11
|
+
Settings.domain = 'localhost:9292'
|
12
|
+
Settings.domain # => "localhost:9292"
|
13
|
+
|
14
|
+
|
15
|
+
# what if the value is never initialized? (ie first use with no default)
|
16
|
+
Settings.has_setting :port
|
17
|
+
begin
|
18
|
+
Settings.port
|
19
|
+
rescue ARSettings::UninitializedSettingError
|
20
|
+
$! # => #<ARSettings::UninitializedSettingError: Settings#port has not been initialized.>
|
21
|
+
end
|
22
|
+
Settings.port = 3000
|
23
|
+
Settings.port # => 3000
|
data/examples/helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sqlite3'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
# hook up an in memory sqlite3 db
|
6
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3' , :database => ":memory:"
|
7
|
+
|
8
|
+
|
9
|
+
# silently create the db
|
10
|
+
require 'stringio'
|
11
|
+
$stdout = StringIO.new
|
12
|
+
|
13
|
+
ActiveRecord::Schema.define do
|
14
|
+
create_table :settings do |t|
|
15
|
+
t.string :name , :null => false , :size => 30
|
16
|
+
t.text :value
|
17
|
+
t.text :package
|
18
|
+
t.boolean :volatile , :default => false
|
19
|
+
t.timestamps
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table :users do |t|
|
23
|
+
t.string :name
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
$stdout = STDOUT
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/arsettings'
|
3
|
+
|
4
|
+
# tell it to create us a settings class named Settings
|
5
|
+
# this means you must have a table in your db named 'settings'
|
6
|
+
ARSettings.create_settings_class 'Settings'
|
7
|
+
|
8
|
+
|
9
|
+
# you can package settings together under a namespace
|
10
|
+
EmailSettings = Settings.package :email
|
11
|
+
|
12
|
+
# you can add settings with has_setting for consistency across uses
|
13
|
+
# or you can use add for brevity
|
14
|
+
Settings.add :enabled
|
15
|
+
EmailSettings.has_setting :enabled
|
16
|
+
|
17
|
+
# initialize, and show that boolean methods translate object/nil to true/false
|
18
|
+
EmailSettings.enabled = 'yes'
|
19
|
+
EmailSettings.enabled? # => true
|
20
|
+
|
21
|
+
EmailSettings.enabled = nil
|
22
|
+
EmailSettings.enabled? # => false
|
23
|
+
|
24
|
+
# can access the package still via the ::package method
|
25
|
+
Settings.package(:email) == EmailSettings # => true
|
26
|
+
Settings.package(:email).enabled? # => false
|
27
|
+
|
28
|
+
|
29
|
+
# Settings and EmailSettings are under different packages (namespaces)
|
30
|
+
# so they do not conflict with eachother's values
|
31
|
+
Settings.enabled = true
|
32
|
+
Settings.enabled? # => true
|
33
|
+
EmailSettings.enabled? # => false
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/arsettings'
|
3
|
+
|
4
|
+
# tell it to create us a settings class named Settings
|
5
|
+
# this means you must have a table in your db named 'settings'
|
6
|
+
ARSettings.create_settings_class 'Settings'
|
7
|
+
|
8
|
+
class User < ActiveRecord::Base
|
9
|
+
has_setting(:site_admin) { |user| user.name }
|
10
|
+
has_setting :delete_account_after , :instance => true , :default => 365 # days
|
11
|
+
end
|
12
|
+
|
13
|
+
User.create! :name => 'the admin'
|
14
|
+
User.site_admin = User.first
|
15
|
+
|
16
|
+
# block converted the user to its name
|
17
|
+
User.site_admin # => "the admin"
|
18
|
+
|
19
|
+
# can access the setting on the instance, because we passed :instance => true
|
20
|
+
User.new.delete_account_after # => 365
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/arsettings'
|
3
|
+
|
4
|
+
# tell it to create us a settings class named Settings
|
5
|
+
# this means you must have a table in your db named 'settings'
|
6
|
+
ARSettings.create_settings_class 'Settings'
|
7
|
+
|
8
|
+
|
9
|
+
class Water
|
10
|
+
ARSettings.on self
|
11
|
+
has_setting :hydrogen , :default => 1
|
12
|
+
has_setting :oxygen , :default => 2
|
13
|
+
has_setting :state , :default => :liquid , :instance => true
|
14
|
+
end
|
15
|
+
|
16
|
+
Water.hydrogen # => 1
|
17
|
+
Water.oxygen # => 2
|
18
|
+
Water.new.state # => :liquid
|
@@ -9,13 +9,13 @@ module ARSettings
|
|
9
9
|
UninitializedSettingError = Class.new(Exception)
|
10
10
|
|
11
11
|
# create the settings class
|
12
|
-
def self.
|
12
|
+
def self.create( classname , options=Hash.new )
|
13
13
|
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)
|
14
14
|
validate_options options , :volatile , :max_chars
|
15
15
|
Object.const_set classname , Class.new(ActiveRecord::Base)
|
16
16
|
klass = Object.const_get(classname).class_eval do
|
17
|
-
extend
|
18
|
-
include
|
17
|
+
extend SettingsClass::ClassMethods
|
18
|
+
include SettingsClass::InstanceMethods
|
19
19
|
const_set :MAX_CHARS , options.fetch( :max_chars , 30 )
|
20
20
|
const_set :VOLATILIE_DEFAULT , options.fetch( :volatile , false )
|
21
21
|
send :load_from_db
|
@@ -25,7 +25,14 @@ module ARSettings
|
|
25
25
|
klass
|
26
26
|
end
|
27
27
|
|
28
|
+
|
29
|
+
|
28
30
|
class << self
|
31
|
+
# initially called create_settings_class, this is for backwards compatibility
|
32
|
+
alias :create_settings_class :create
|
33
|
+
|
34
|
+
# the class that will be used when you inherit from AR::B,
|
35
|
+
# or if you don't specify in ::on
|
29
36
|
attr_accessor :default_class
|
30
37
|
end
|
31
38
|
|
@@ -34,24 +41,8 @@ module ARSettings
|
|
34
41
|
settings_class = options.fetch :settings_class , default_class
|
35
42
|
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
43
|
validate_options options , :settings_class
|
37
|
-
|
38
|
-
|
39
|
-
package = settings_class.package(object)
|
40
|
-
package.add name , inner_options , &block
|
41
|
-
getter = name
|
42
|
-
setter = "#{name}="
|
43
|
-
boolean_getter = "#{name}?"
|
44
|
-
(class << self ; self ; end).instance_eval do
|
45
|
-
define_method getter do package.send getter end
|
46
|
-
define_method setter do |arg| package.send setter , arg end
|
47
|
-
define_method boolean_getter do package.send boolean_getter end
|
48
|
-
end
|
49
|
-
if instance
|
50
|
-
define_method getter do package.send getter end
|
51
|
-
define_method boolean_getter do package.send boolean_getter end
|
52
|
-
define_method setter do |arg| package.send setter , arg end
|
53
|
-
end
|
54
|
-
end
|
44
|
+
object.instance_variable_set( '@arsettings_package' , settings_class.package(object) )
|
45
|
+
(class << object ; self ; end).send :include , HasSettings
|
55
46
|
end
|
56
47
|
|
57
48
|
def self.serialize(data) # :nodoc:
|
@@ -65,10 +56,10 @@ module ARSettings
|
|
65
56
|
def self.validate_options(options,*valid_options) # :nodoc:
|
66
57
|
options.each do |option,value|
|
67
58
|
unless valid_options.include? option
|
68
|
-
raise ARSettings::InvalidOptionError.new
|
59
|
+
raise ARSettings::InvalidOptionError.new("#{option.inspect} is not a valid option, because it is not in #{valid_options.inspect}")
|
69
60
|
end
|
70
61
|
end
|
71
62
|
end
|
72
|
-
|
63
|
+
|
73
64
|
end
|
74
65
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ARSettings
|
2
|
+
module HasSettings
|
3
|
+
|
4
|
+
def has_setting( name , inner_options={} , &block )
|
5
|
+
@arsettings_package ||= ARSettings.default_class.package(self)
|
6
|
+
instance = inner_options.delete :instance
|
7
|
+
package = @arsettings_package
|
8
|
+
package.add name , inner_options , &block
|
9
|
+
definitions = lambda do |*args|
|
10
|
+
define_method name do package.send name end # getter
|
11
|
+
define_method "#{name}=" do |arg| package.send "#{name}=" , arg end # setter
|
12
|
+
define_method "#{name}?" do package.send "#{name}?" end # boolean-getter
|
13
|
+
end
|
14
|
+
(class << self ; self ; end).instance_eval(&definitions)
|
15
|
+
definitions.call if instance
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/lib/arsettings/packaged.rb
CHANGED
@@ -17,7 +17,7 @@ module ARSettings
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.has_instance?(settings_class,package) # :nodoc:
|
20
|
-
@instances && @instances[settings_class] && @instances[settings_class][normalize
|
20
|
+
@instances && @instances[settings_class] && @instances[settings_class][normalize(package)]
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.validate(settings_class,package) # :nodoc:
|
@@ -32,7 +32,7 @@ module ARSettings
|
|
32
32
|
|
33
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
|
-
Class === settings_class && settings_class.superclass == ActiveRecord::Base &&
|
35
|
+
Class === settings_class && settings_class.superclass == ActiveRecord::Base && SettingsClass::ClassMethods === settings_class
|
36
36
|
end
|
37
37
|
|
38
38
|
|
@@ -70,6 +70,8 @@ module ARSettings
|
|
70
70
|
end
|
71
71
|
self
|
72
72
|
end
|
73
|
+
|
74
|
+
alias :has_setting :add
|
73
75
|
|
74
76
|
def setting?(name)
|
75
77
|
@settings.has_key? name
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module ARSettings
|
2
|
+
module SettingsClass
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
def reset_all # :nodoc:
|
6
|
+
Packaged.instances(self).each { |name,package| package.reset }
|
7
|
+
end
|
8
|
+
|
9
|
+
# get a Package (aka namespace or scope or group)
|
10
|
+
# to package settings together with
|
11
|
+
#
|
12
|
+
# email_settings = Settings.package :email
|
13
|
+
#
|
14
|
+
# email_settings.add :enabled , :default => false
|
15
|
+
#
|
16
|
+
# ProfileSettings = Settings.package :profile_settings
|
17
|
+
#
|
18
|
+
# ProfileSettings.add :enabled , :default => true
|
19
|
+
def package(name)
|
20
|
+
Packaged.instance self , name
|
21
|
+
end
|
22
|
+
|
23
|
+
# add a setting to the settings class, it will be packaged under the settings class itself
|
24
|
+
#
|
25
|
+
#--
|
26
|
+
# FIXME: Document the options and proc
|
27
|
+
def add( name , options={} , &proc )
|
28
|
+
options = name if name.is_a? Hash
|
29
|
+
if options[:package]
|
30
|
+
package options.delete(:package)
|
31
|
+
else
|
32
|
+
package self
|
33
|
+
end.add( name , options , &proc )
|
34
|
+
end
|
35
|
+
|
36
|
+
alias :has_setting :add
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def method_missing(name,*args) # :nodoc:
|
41
|
+
if name =~ /\A[A-Z]/
|
42
|
+
const_get name , *args
|
43
|
+
elsif name.to_s !~ /=$/ || ( name.to_s =~ /=$/ && args.size == 1 )
|
44
|
+
package(self).send name , *args
|
45
|
+
else
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def load_from_db # :nodoc:
|
51
|
+
reset_all
|
52
|
+
all.each { |instance| add :record => instance , :package => instance.package }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ARSettings
|
2
|
+
module SettingsClass
|
3
|
+
module InstanceMethods # :nodoc: all
|
4
|
+
|
5
|
+
# unfortunately, can't serialize a proc. I tried both yaml and marshal
|
6
|
+
# so will have to keep it in memory and just be sure to set it each time app loads
|
7
|
+
attr_accessor :postprocessing
|
8
|
+
|
9
|
+
|
10
|
+
def value=(new_value)
|
11
|
+
@deserialized_value = new_value
|
12
|
+
@value_is_deserialized = true
|
13
|
+
super ARSettings.serialize(new_value)
|
14
|
+
save
|
15
|
+
end
|
16
|
+
|
17
|
+
def value
|
18
|
+
@value_is_deserialized ||= false # just to clear a stupid warning
|
19
|
+
if @value_is_deserialized && !volatile?
|
20
|
+
@deserialized_value
|
21
|
+
else
|
22
|
+
raise UninitializedSettingError.new("#{package}##{name} has not been initialized.") unless super
|
23
|
+
reload
|
24
|
+
@value_is_deserialized = true
|
25
|
+
@deserialized_value = ARSettings.deserialize(super)
|
26
|
+
@deserialized_value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def volatile=(value)
|
31
|
+
super
|
32
|
+
save
|
33
|
+
end
|
34
|
+
|
35
|
+
def package
|
36
|
+
@package ||= super.to_sym
|
37
|
+
end
|
38
|
+
|
39
|
+
def package=(pkg)
|
40
|
+
super pkg.to_s
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
data/lib/arsettings.rb
CHANGED
@@ -2,12 +2,28 @@ require 'yaml'
|
|
2
2
|
|
3
3
|
manifest = %w(
|
4
4
|
arsettings
|
5
|
-
|
6
|
-
|
5
|
+
settings_class/class_methods
|
6
|
+
settings_class/instance_methods
|
7
7
|
packaged
|
8
|
-
|
8
|
+
has_settings
|
9
9
|
)
|
10
10
|
|
11
11
|
manifest.each do |filename|
|
12
12
|
require File.dirname(__FILE__) << "/arsettings/#{filename}"
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
# placing this here because otherwise it violates packaging standard
|
18
|
+
class ActiveRecord::Base
|
19
|
+
|
20
|
+
def self.inherited_with_settings(subclass)
|
21
|
+
inherited_without_settings subclass
|
22
|
+
subclass.extend ARSettings::HasSettings
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
alias_method_chain :inherited, :settings
|
27
|
+
end
|
28
|
+
|
13
29
|
end
|
data/test/_helper.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# require libs and gems
|
2
|
+
require 'test/unit'
|
3
|
+
require File.dirname(__FILE__) << "/_in_memory_db"
|
4
|
+
|
5
|
+
ARSettings.create_settings_class 'Setting'
|
6
|
+
|
7
|
+
# monkey patch Test::Unit::TestCase to make it easier to work with
|
8
|
+
class Test::Unit::TestCase
|
9
|
+
|
10
|
+
# some dummy classes for use with test data
|
11
|
+
Example1 = Struct.new :a , :b
|
12
|
+
|
13
|
+
Example2 = Class.new do
|
14
|
+
attr_accessor :a , :b
|
15
|
+
def initialize(a,b)
|
16
|
+
@a,@b = a,b
|
17
|
+
end
|
18
|
+
def ==(ex2)
|
19
|
+
@a == ex2.a &&
|
20
|
+
@b == ex2.b
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def object_menagerie
|
25
|
+
[ 'a' , :a , 1 , /a/ , [1,2] , true , false , nil , {:a => 1,:b => 2} , Example1.new(1,2) , Example2.new(1,2) ]
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.to_method_name(str)
|
29
|
+
str.gsub(/\s+/,'_').gsub(/\W+/,'')
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.context(context)
|
33
|
+
@context = to_method_name(context)
|
34
|
+
yield
|
35
|
+
@context = nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.verify( to_verify , &verification )
|
39
|
+
prefix = "test_"
|
40
|
+
prefix << @context << '_' if @context && !@context.empty?
|
41
|
+
define_method prefix << to_method_name(to_verify) , &verification if verification
|
42
|
+
end
|
43
|
+
|
44
|
+
def assert_invalid(&block)
|
45
|
+
assert_raises ActiveRecord::RecordInvalid , &block
|
46
|
+
end
|
47
|
+
|
48
|
+
def assert_invalid_name(&block)
|
49
|
+
assert_raises ARSettings::InvalidNameError , &block
|
50
|
+
end
|
51
|
+
|
52
|
+
def assert_count(count)
|
53
|
+
assert_equal count , Setting.count
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sqlite3'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
# require the setting lib that we will be testing
|
8
|
+
require File.dirname(__FILE__) + '/../lib/arsettings'
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
# hook it up to an in memory sqlite3 db
|
13
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3' , :database => ":memory:"
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
# silently create the db
|
18
|
+
require 'stringio'
|
19
|
+
old_stdout = $stdout
|
20
|
+
$stdout = StringIO.new
|
21
|
+
|
22
|
+
define_as_settings_class = lambda do |t|
|
23
|
+
t.string :name , :null => false , :size => 30
|
24
|
+
t.text :value
|
25
|
+
t.text :package
|
26
|
+
t.boolean :volatile , :default => false
|
27
|
+
t.timestamps
|
28
|
+
end
|
29
|
+
|
30
|
+
ActiveRecord::Schema.define do
|
31
|
+
|
32
|
+
[
|
33
|
+
:different_names ,
|
34
|
+
:predefined_values ,
|
35
|
+
:settings ,
|
36
|
+
:setting2s ,
|
37
|
+
:setting3s ,
|
38
|
+
:setting4s ,
|
39
|
+
:setting5s ,
|
40
|
+
:setting6s ,
|
41
|
+
:setting7s ,
|
42
|
+
:setting8s ,
|
43
|
+
:setting9s ,
|
44
|
+
:setting10s ,
|
45
|
+
:setting11s ,
|
46
|
+
:setting12s ,
|
47
|
+
:setting13s ,
|
48
|
+
:setting14s ,
|
49
|
+
:setting15s ,
|
50
|
+
:setting16s ,
|
51
|
+
:setting17s ,
|
52
|
+
:setting18s ,
|
53
|
+
:setting19s ,
|
54
|
+
:setting20s ,
|
55
|
+
:setting21s ,
|
56
|
+
:setting22s ,
|
57
|
+
:setting23s ,
|
58
|
+
:setting24s ,
|
59
|
+
:setting25s ,
|
60
|
+
:volatile_tests ,
|
61
|
+
:this_should_be_returneds ,
|
62
|
+
].flatten.each do |tablename|
|
63
|
+
create_table tablename , &define_as_settings_class
|
64
|
+
end
|
65
|
+
|
66
|
+
# capture this object so that I can write sql later (prob a better way, but IDK what it is)
|
67
|
+
$sql_executor = self
|
68
|
+
def $sql_executor.silent_execute(sql)
|
69
|
+
suppress_messages { execute(sql) }
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
$stdout = old_stdout
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env bash -l
|
2
|
+
|
3
|
+
IFS="-"
|
4
|
+
for rvm_set in 1.8.6-2.3.3 1.8.6-2.3.5 1.8.6-2.3.8 1.8.7-2.3.3 1.8.7-2.3.5 1.8.7-2.3.8 1.9.1-2.3.3 1.9.1-2.3.5 1.9.1-2.3.8 1.9.2-2.3.3 1.9.2-2.3.5 1.9.2-2.3.8 1.9.2-3.0.1 ; do
|
5
|
+
arr=( $rvm_set )
|
6
|
+
ruby_version=${arr[0]}
|
7
|
+
ar_version=${arr[1]}
|
8
|
+
rvm use "$ruby_version@ruby$ruby_version-activerecord$ar_version" --create
|
9
|
+
gem install activerecord -v "$ar_version" --no-ri --no-rdoc
|
10
|
+
gem install sqlite3-ruby -v 1.3.1 --no-ri --no-rdoc
|
11
|
+
done
|
data/test/_rcov.sh
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env bash -l
|
2
|
+
|
3
|
+
# WARNING: MAKES ASSUMPTIONS BASED ON BEING INVOKED BY THE RAKEFILE
|
4
|
+
|
5
|
+
# if this breaks, its probably because you need to install rcov on 1.8.7@ruby1.8.7-activerecord2.3.5
|
6
|
+
rvm use 1.8.7@ruby1.8.7-activerecord2.3.5
|
7
|
+
|
8
|
+
rcov -x gems test/*_test.rb
|
9
|
+
|
10
|
+
open coverage/index.html
|
data/test/_reek.sh
ADDED
data/test/_run_all.sh
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env bash -l
|
2
|
+
|
3
|
+
|
4
|
+
IFS="-"
|
5
|
+
for rvm_set in 1.8.6-2.3.3 1.8.6-2.3.5 1.8.6-2.3.8 1.8.7-2.3.3 1.8.7-2.3.5 1.8.7-2.3.8 1.9.1-2.3.3 1.9.1-2.3.5 1.9.1-2.3.8 1.9.2-2.3.3 1.9.2-2.3.5 1.9.2-2.3.8 1.9.2-3.0.1 ; do
|
6
|
+
arr=( $rvm_set )
|
7
|
+
ruby_version=${arr[0]}
|
8
|
+
ar_version=${arr[1]}
|
9
|
+
rvm use "$ruby_version@ruby$ruby_version-activerecord$ar_version"
|
10
|
+
./test/_run_one.rb
|
11
|
+
done
|