configure_me 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rspec +1 -1
- data/{README.md → README.rdoc} +7 -7
- data/Rakefile +1 -7
- data/configure_me.gemspec +3 -7
- data/lib/configure_me/attribute_methods.rb +88 -0
- data/lib/configure_me/base.rb +58 -62
- data/lib/configure_me/caching.rb +19 -14
- data/lib/configure_me/identity.rb +15 -0
- data/lib/configure_me/loading.rb +57 -0
- data/lib/configure_me/naming.rb +18 -0
- data/lib/configure_me/nesting.rb +30 -24
- data/lib/configure_me/persistence.rb +29 -0
- data/lib/configure_me/persisting.rb +33 -20
- data/lib/configure_me/setting.rb +61 -12
- data/lib/configure_me/validations.rb +33 -0
- data/lib/configure_me/version.rb +1 -1
- data/lib/configure_me.rb +25 -3
- data/lib/generators/configure_me/setup_generator.rb +21 -0
- data/lib/generators/configure_me/templates/initializer.rb +1 -0
- data/lib/generators/{templates → configure_me/templates}/migration.rb +0 -0
- data/lib/generators/{templates/model.rb → configure_me/templates/model.erb} +0 -0
- data/lib/generators/configure_me/templates/model.rb +2 -0
- data/spec/configure_me/attribute_methods_spec.rb +113 -0
- data/spec/configure_me/base_spec.rb +43 -88
- data/spec/configure_me/caching_spec.rb +75 -0
- data/spec/configure_me/identity_spec.rb +30 -0
- data/spec/configure_me/loading_spec.rb +84 -0
- data/spec/configure_me/naming_spec.rb +23 -0
- data/spec/configure_me/nesting_spec.rb +36 -0
- data/spec/configure_me/persistence_spec.rb +62 -0
- data/spec/configure_me/persisting_spec.rb +123 -0
- data/spec/configure_me/setting_spec.rb +180 -13
- data/spec/spec_helper.rb +6 -0
- data/spec/support/active_model_lint.rb +16 -0
- metadata +52 -81
- data/features/memory.feature +0 -16
- data/features/step_definitions/base_steps.rb +0 -33
- data/lib/configure_me/settings.rb +0 -101
- data/lib/generators/configure_me_generator.rb +0 -19
- data/lib/generators/templates/initializer.rb +0 -1
data/lib/configure_me/setting.rb
CHANGED
@@ -1,27 +1,76 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
|
1
3
|
module ConfigureMe
|
4
|
+
class InvalidDefault < StandardError; end
|
5
|
+
class UnsupportedType < StandardError; end
|
6
|
+
class InvalidConversion < StandardError; end
|
7
|
+
# == Setting
|
8
|
+
#
|
9
|
+
# There are two methods used to create a setting:
|
10
|
+
# 1. calling the class method <tt>setting</tt> from within an instance of ConfigureMe::Base
|
11
|
+
# 2. implicitly when a hash is fed to ConfigureMe::Base.load
|
2
12
|
class Setting
|
3
13
|
attr_reader :name, :type, :default
|
4
14
|
|
5
|
-
|
15
|
+
VALID_TYPES = [:string, :integer, :float, :boolean, :unknown]
|
16
|
+
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
|
17
|
+
|
18
|
+
def initialize(name, *args)
|
6
19
|
options = args.extract_options!
|
7
20
|
|
8
|
-
@
|
21
|
+
@name = name.to_s
|
9
22
|
@default = options.key?(:default) ? options[:default] : nil
|
10
|
-
|
11
|
-
|
12
|
-
def define_methods!
|
13
|
-
@owner.define_attribute_methods(true)
|
23
|
+
@type = options.key?(:type) ? options[:type] : infer_type(@default)
|
24
|
+
raise UnsupportedType.new("Invalid type: #{@type}") unless VALID_TYPES.include?(@type)
|
14
25
|
end
|
15
26
|
|
16
27
|
def convert(value)
|
17
28
|
case type
|
18
|
-
when :string then value
|
19
|
-
when :text then value
|
29
|
+
when :string then convert_to_string(value)
|
20
30
|
when :integer then value.to_i rescue value ? 1 : 0
|
21
|
-
when :float then value.to_f
|
22
|
-
when :
|
23
|
-
when :
|
24
|
-
|
31
|
+
when :float then value.to_f rescue value ? 1.0 : 0.0
|
32
|
+
when :boolean then convert_to_boolean(value)
|
33
|
+
when :unknown
|
34
|
+
@type = infer_type(value)
|
35
|
+
convert(value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def infer_type(value)
|
42
|
+
case value
|
43
|
+
when String
|
44
|
+
:string
|
45
|
+
when Fixnum
|
46
|
+
:integer
|
47
|
+
when Float
|
48
|
+
:float
|
49
|
+
when TrueClass, FalseClass
|
50
|
+
:boolean
|
51
|
+
when NilClass
|
52
|
+
:unknown
|
53
|
+
else
|
54
|
+
raise InvalidDefault.new("Unable to infer type from #{value.inspect}")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def convert_to_string(value)
|
59
|
+
case value
|
60
|
+
when String
|
61
|
+
value
|
62
|
+
when Fixnum, Float, TrueClass, FalseClass, NilClass
|
63
|
+
value.to_s
|
64
|
+
else
|
65
|
+
raise InvalidConversion.new("Unable to convert #{value.inspect} to string")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def convert_to_boolean(value)
|
70
|
+
if value.is_a?(String) && value.blank?
|
71
|
+
nil
|
72
|
+
else
|
73
|
+
TRUE_VALUES.include?(value)
|
25
74
|
end
|
26
75
|
end
|
27
76
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module ConfigureMe
|
4
|
+
class Base
|
5
|
+
include ActiveModel::Validations
|
6
|
+
define_model_callbacks :validation
|
7
|
+
|
8
|
+
def save(options={})
|
9
|
+
run_callbacks :validation do
|
10
|
+
perform_validations(options) ? super : false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?(context = nil)
|
15
|
+
context ||= (persisted? ? :update : :create)
|
16
|
+
output = super(context)
|
17
|
+
errors.empty? && output
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def perform_validations(options={})
|
23
|
+
if options[:validate] != false
|
24
|
+
valid?(options[:context])
|
25
|
+
else
|
26
|
+
true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module Validations
|
32
|
+
end
|
33
|
+
end
|
data/lib/configure_me/version.rb
CHANGED
data/lib/configure_me.rb
CHANGED
@@ -1,14 +1,36 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'singleton'
|
3
|
+
|
1
4
|
module ConfigureMe
|
2
5
|
class << self
|
3
|
-
def
|
4
|
-
|
6
|
+
def init(options = {})
|
7
|
+
options = {:persist_with => nil, :cache_with => nil}.merge(options)
|
8
|
+
@persistence_klass = options[:persist_with]
|
9
|
+
@cache_object = options[:cache_with]
|
5
10
|
end
|
6
11
|
|
7
12
|
def persistence_klass
|
8
|
-
@persistence_klass ||=
|
13
|
+
@persistence_klass ||= nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def cache_object
|
17
|
+
@cache_object
|
9
18
|
end
|
10
19
|
end
|
20
|
+
|
21
|
+
class Base
|
22
|
+
include Singleton
|
23
|
+
end
|
11
24
|
end
|
12
25
|
|
26
|
+
require 'configure_me/attribute_methods'
|
27
|
+
require 'configure_me/caching'
|
28
|
+
require 'configure_me/identity'
|
29
|
+
require 'configure_me/loading'
|
30
|
+
require 'configure_me/naming'
|
31
|
+
require 'configure_me/nesting'
|
32
|
+
require 'configure_me/persistence'
|
33
|
+
require 'configure_me/persisting'
|
13
34
|
require 'configure_me/setting'
|
35
|
+
require 'configure_me/validations'
|
14
36
|
require 'configure_me/base'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
module ConfigureMe
|
4
|
+
class SetupGenerator < ActiveRecord::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
source_root File.expand_path('../templates', __FILE__)
|
8
|
+
|
9
|
+
def generate_model
|
10
|
+
template 'model.rb', "app/models/#{singular_name}.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
def generate_migration
|
14
|
+
migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb"
|
15
|
+
end
|
16
|
+
|
17
|
+
def generate_initializer
|
18
|
+
template 'initializer.rb', 'config/initializers/init_configure_me.rb'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
ConfigureMe.init(:persist_with => <%= class_name %>, :cache_with => Rails.cache)
|
File without changes
|
File without changes
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConfigureMe::AttributeMethods do
|
4
|
+
before {
|
5
|
+
@config_class = define_test_class('MyTestConfig', ConfigureMe::Base)
|
6
|
+
}
|
7
|
+
subject { @config_class.instance }
|
8
|
+
it 'make clean should clear the temp_attributes' do
|
9
|
+
subject.send(:temp_attributes).expects(:clear)
|
10
|
+
subject.send(:make_clean)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ConfigureMe::AttributeMethods, 'reading an attribute' do
|
15
|
+
before {
|
16
|
+
@config_class = define_test_class('MyTestConfig', ConfigureMe::Base)
|
17
|
+
@config_class.send(:setting, :testsetting, :default => 'foo')
|
18
|
+
@config = @config_class.instance
|
19
|
+
@config.stubs(:read_cache)
|
20
|
+
@config.stubs(:read_persist)
|
21
|
+
}
|
22
|
+
subject { @config }
|
23
|
+
it 'should call "read_attribute"' do
|
24
|
+
@config.expects(:read_attribute).with('testsetting')
|
25
|
+
subject.testsetting
|
26
|
+
end
|
27
|
+
context 'with a pristine instance' do
|
28
|
+
it 'should attempt to read from the cache' do
|
29
|
+
subject.expects(:read_cache)
|
30
|
+
subject.testsetting
|
31
|
+
end
|
32
|
+
it 'should attempt to read from the persistence store' do
|
33
|
+
subject.expects(:read_persist)
|
34
|
+
subject.testsetting
|
35
|
+
end
|
36
|
+
it 'should return the default value' do
|
37
|
+
subject.testsetting.should eql('foo')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
context 'with a persisted value' do
|
41
|
+
context 'with a non-cached value' do
|
42
|
+
before { @config.stubs(:read_cache).returns(nil) }
|
43
|
+
it 'should attempt to read from the cache' do
|
44
|
+
subject.expects(:read_cache).returns(nil)
|
45
|
+
subject.testsetting
|
46
|
+
end
|
47
|
+
it 'should attempt to read from the persistence store' do
|
48
|
+
subject.expects(:read_persist)
|
49
|
+
subject.testsetting
|
50
|
+
end
|
51
|
+
it 'should write the value to the cache' do
|
52
|
+
subject.stubs(:read_persist).returns('persisted')
|
53
|
+
subject.expects(:write_cache).with(:testsetting, 'persisted')
|
54
|
+
subject.testsetting
|
55
|
+
end
|
56
|
+
it 'should return the persisted value' do
|
57
|
+
subject.stubs(:read_cache).returns('persisted')
|
58
|
+
subject.testsetting.should eql('persisted')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'with a cached value' do
|
63
|
+
before { @config.stubs(:read_cache).returns('cached') }
|
64
|
+
it 'should attempt to read from the cache' do
|
65
|
+
subject.expects(:read_cache).returns('cached')
|
66
|
+
subject.testsetting
|
67
|
+
end
|
68
|
+
it 'should not attempt to read from the persistence store' do
|
69
|
+
subject.expects(:read_persist).never
|
70
|
+
subject.testsetting
|
71
|
+
end
|
72
|
+
it 'should return the cached value' do
|
73
|
+
subject.testsetting.should eql('cached')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'with a dirty value' do
|
79
|
+
before { @config.stubs(:testsetting_changed?).returns(true) }
|
80
|
+
it 'should not attempt to read from the cache' do
|
81
|
+
subject.expects(:read_cache).never
|
82
|
+
subject.testsetting
|
83
|
+
end
|
84
|
+
it 'should not attempt to read from the persistence store' do
|
85
|
+
subject.expects(:read_persist).never
|
86
|
+
subject.testsetting
|
87
|
+
end
|
88
|
+
it 'should return the value from the temp_attributes hash' do
|
89
|
+
subject.expects(:temp_attributes).returns(:testsetting => 'iamdirty')
|
90
|
+
subject.testsetting.should eql('iamdirty')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe ConfigureMe::AttributeMethods, 'writing an attribute' do
|
96
|
+
before {
|
97
|
+
@config_class = define_test_class('MyTestConfig', ConfigureMe::Base)
|
98
|
+
@config_class.send(:setting, :testsetting, :default => 'foo')
|
99
|
+
@config = @config_class.instance
|
100
|
+
@config.stubs(:read_cache)
|
101
|
+
@config.stubs(:read_persist)
|
102
|
+
}
|
103
|
+
subject { @config }
|
104
|
+
|
105
|
+
it 'should set the attribute to dirty' do
|
106
|
+
subject.expects(:make_dirty).with(:testsetting)
|
107
|
+
subject.testsetting = 'newvalue'
|
108
|
+
end
|
109
|
+
it 'should write the value to the temp_attributes hash' do
|
110
|
+
subject.testsetting = 'newvalue'
|
111
|
+
subject.send(:temp_attributes)[:testsetting].should eql('newvalue')
|
112
|
+
end
|
113
|
+
end
|
@@ -1,103 +1,58 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ConfigureMe
|
4
|
-
|
3
|
+
describe ConfigureMe do
|
4
|
+
it { should respond_to(:init) }
|
5
|
+
it { should respond_to(:persistence_klass) }
|
6
|
+
it { should respond_to(:cache_object) }
|
7
|
+
it 'should provide nil defaults for :init' do
|
8
|
+
ConfigureMe.init
|
9
|
+
ConfigureMe.persistence_klass.should be_nil
|
10
|
+
ConfigureMe.cache_object.should be_nil
|
5
11
|
end
|
6
12
|
|
7
|
-
it
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
describe 'with a "color" setting with a default of "red"' do
|
13
|
-
before(:each) do
|
14
|
-
@klass = Class.new(ConfigureMe::Base)
|
15
|
-
@klass.send :setting, :color, :string, :default => 'red'
|
16
|
-
end
|
17
|
-
|
18
|
-
describe 'an instance ' do
|
19
|
-
it 'should respond to attribute_method?(:color) with true' do
|
20
|
-
@obj = @klass.instance
|
21
|
-
@obj.send(:attribute_method?, :color).should be_true
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should respond to attribute_method?(:size) with false' do
|
25
|
-
@obj = @klass.instance
|
26
|
-
@obj.send(:attribute_method?, :size).should be_false
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'should respond to :color with "red"' do
|
30
|
-
@obj = @klass.instance
|
31
|
-
@obj.color.should eql('red')
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should store a new :color of "blue"' do
|
35
|
-
@obj = @klass.instance
|
36
|
-
@obj.color = 'blue'
|
37
|
-
@obj.color.should eql('blue')
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'should raise error when setting an unknown attribute' do
|
41
|
-
@obj = @klass.instance
|
42
|
-
lambda { @obj.write_setting('size', 'big') }.should raise_error(NoMethodError)
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'should assign from a hash' do
|
46
|
-
@obj = @klass.instance
|
47
|
-
@obj.send :settings=, {:color => 'blue'}
|
48
|
-
@obj.color.should eql('blue')
|
49
|
-
end
|
50
|
-
end
|
13
|
+
it 'should accept a hash of initialization arguments' do
|
14
|
+
ConfigureMe.init(:persist_with => 'foo', :cache_with => 'bar')
|
15
|
+
ConfigureMe.persistence_klass.should eql('foo')
|
16
|
+
ConfigureMe.cache_object.should eql('bar')
|
51
17
|
end
|
18
|
+
end
|
52
19
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
ConfigureMe.persist_with @persistence
|
58
|
-
@klass.send :setting, :color, :string, :default => 'red'
|
59
|
-
@klass.send :persist_me
|
60
|
-
end
|
20
|
+
describe ConfigureMe::Base do
|
21
|
+
it 'should enforce the singleton pattern' do
|
22
|
+
lambda { ConfigureMe::Base.new }.should raise_error(NoMethodError)
|
23
|
+
end
|
61
24
|
|
62
|
-
|
63
|
-
|
64
|
-
|
25
|
+
class BaseConfig < ConfigureMe::Base
|
26
|
+
setting :setting1, :type => :integer, :default => 12
|
27
|
+
end
|
65
28
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
@unpersisted = mock('UnpersistedSetting') do
|
70
|
-
stubs(:value=)
|
71
|
-
end
|
72
|
-
@persistence = mock('PersistenceClass')
|
73
|
-
ConfigureMe.persist_with @persistence
|
74
|
-
end
|
29
|
+
subject { BaseConfig.instance }
|
30
|
+
its(:persisted?) { should be_true }
|
31
|
+
its(:class) { should respond_to(:setting1) }
|
75
32
|
|
76
|
-
|
33
|
+
describe 'ActiveModel compliance' do
|
34
|
+
before { @config = define_test_class('TestConfig', ConfigureMe::Base).instance }
|
35
|
+
subject { @config }
|
36
|
+
it_should_behave_like "ActiveModel"
|
37
|
+
end
|
77
38
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
39
|
+
describe 'find_by_id' do
|
40
|
+
subject { ConfigureMe::Base }
|
41
|
+
before {
|
42
|
+
@mock_config = mock('Config') do
|
43
|
+
stubs(:config_key).returns('the-right-one')
|
44
|
+
stubs(:instance).returns('instance')
|
45
|
+
end
|
46
|
+
@configs = [@mock_config]
|
47
|
+
}
|
48
|
+
it 'should return nil for an invalid id' do
|
49
|
+
subject.stubs(:configs).returns([])
|
50
|
+
subject.find_by_id('something').should be_nil
|
86
51
|
end
|
87
52
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
@persisted = mock('PersistedSetting') do
|
92
|
-
stubs(:value).returns("blue".to_yaml)
|
93
|
-
end
|
94
|
-
@persistence.stubs(:find_by_key).returns(@persisted)
|
95
|
-
ConfigureMe.persist_with @persistence
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'should return a valid value' do
|
99
|
-
@obj.color.should eql("blue")
|
100
|
-
end
|
53
|
+
it 'should return a matching config' do
|
54
|
+
subject.stubs(:configs).returns(@configs)
|
55
|
+
subject.find_by_id('the-right-one').should eql('instance')
|
101
56
|
end
|
102
57
|
end
|
103
58
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConfigureMe::Caching, 'the class' do
|
4
|
+
subject { ConfigureMe::Base }
|
5
|
+
it { should respond_to(:cache_me) }
|
6
|
+
it { should respond_to(:caching?) }
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ConfigureMe::Caching, 'caching?' do
|
10
|
+
before {
|
11
|
+
@caching_class = define_test_class('CachingConfig', ConfigureMe::Base)
|
12
|
+
@caching_class.send(:cache_me)
|
13
|
+
}
|
14
|
+
subject { @caching_class }
|
15
|
+
|
16
|
+
context 'when cache_object is available' do
|
17
|
+
before { ConfigureMe.stubs(:cache_object).returns({}) }
|
18
|
+
specify { subject.caching?.should be_true }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when cache_object is unavailable' do
|
22
|
+
before { ConfigureMe.stubs(:cache_object).returns(nil) }
|
23
|
+
specify { subject.caching?.should be_false }
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ConfigureMe::Caching, 'when caching is enabled' do
|
29
|
+
before {
|
30
|
+
@caching_class = define_test_class('CachingConfig', ConfigureMe::Base)
|
31
|
+
@caching_class.stubs(:caching?).returns(true)
|
32
|
+
@cache_object = mock('CacheObject') do
|
33
|
+
stubs(:read).returns('cached_value')
|
34
|
+
stubs(:write)
|
35
|
+
end
|
36
|
+
ConfigureMe.stubs(:cache_object).returns(@cache_object)
|
37
|
+
}
|
38
|
+
subject { @caching_class.instance }
|
39
|
+
|
40
|
+
it 'should attempt to read from the cache' do
|
41
|
+
ConfigureMe.expects(:cache_object).once.returns(@cache_object)
|
42
|
+
subject.read_cache('cachedsetting')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should return the value from the cache' do
|
46
|
+
subject.read_cache('cachedsetting').should eql('cached_value')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should attempt to write to the cache' do
|
50
|
+
@cache_object.expects(:write).once
|
51
|
+
subject.write_cache('cachedsetting', 'newvalue')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe ConfigureMe::Caching, 'when caching is disabled' do
|
56
|
+
before {
|
57
|
+
@caching_class = define_test_class('CachingConfig', ConfigureMe::Base)
|
58
|
+
@caching_class.stubs(:caching?).returns(false)
|
59
|
+
}
|
60
|
+
subject { @caching_class.instance }
|
61
|
+
|
62
|
+
it 'should not attempt to read from the cache' do
|
63
|
+
ConfigureMe.expects(:cache_object).never
|
64
|
+
subject.read_cache('cachedsetting')
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should return nil when read_cache is called' do
|
68
|
+
subject.read_cache('cachedsetting').should be_nil
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should not attempt to write to the cache' do
|
72
|
+
ConfigureMe.expects(:cache_object).never
|
73
|
+
subject.write_cache('cachedsetting', 'newvalue')
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConfigureMe::Identity do
|
4
|
+
before {
|
5
|
+
@parent_class = define_test_class('ParentConfig', ConfigureMe::Base)
|
6
|
+
@nested_class = define_test_class('NestedConfig', ConfigureMe::Base)
|
7
|
+
}
|
8
|
+
|
9
|
+
context 'a root class' do
|
10
|
+
subject { @parent_class.instance }
|
11
|
+
it { should respond_to(:config_key) }
|
12
|
+
it { should respond_to(:config_name) }
|
13
|
+
it { should respond_to(:storage_key) }
|
14
|
+
its(:config_name) { should eql('parent') }
|
15
|
+
its(:config_key) { should eql('parent') }
|
16
|
+
it 'should generate a valid storage key' do
|
17
|
+
subject.storage_key('foo').should eql('parent-foo')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'a nested class' do
|
22
|
+
before { @nested_class.send(:nest_me, @parent_class) }
|
23
|
+
subject { @nested_class.instance }
|
24
|
+
its(:config_name) { should eql('nested') }
|
25
|
+
its(:config_key) { should eql('parent-nested') }
|
26
|
+
it 'should generate a valid storage key' do
|
27
|
+
subject.storage_key('foo').should eql('parent-nested-foo')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConfigureMe::Loading, 'a filename' do
|
4
|
+
subject { ConfigureMe::Base }
|
5
|
+
it 'should raise an error for a non-existent file' do
|
6
|
+
lambda { subject.load('foo') }.should raise_error(ArgumentError)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should load an existing file' do
|
10
|
+
File.stubs(:exists?).returns(true)
|
11
|
+
File.stubs(:open).returns({:foo => 'bar'}.to_yaml)
|
12
|
+
subject.expects(:from_hash)
|
13
|
+
subject.load('avalidfile')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe ConfigureMe::Loading, 'a hash' do
|
18
|
+
subject { ConfigureMe::Base }
|
19
|
+
|
20
|
+
context 'with :app => {:setting1 => \'foo\', :setting2 => 845}' do
|
21
|
+
before { @config = ConfigureMe::Base.load(:app => {:setting1 => 'foo', :setting2 => 845}) }
|
22
|
+
subject { @config.instance }
|
23
|
+
its(:config_name) { should eql('app') }
|
24
|
+
|
25
|
+
it { should respond_to(:setting1) }
|
26
|
+
it { should respond_to(:setting2) }
|
27
|
+
it { should_not respond_to(:setting3) }
|
28
|
+
its(:setting1) { should eql('foo') }
|
29
|
+
its(:setting2) { should eql(845) }
|
30
|
+
its(:parent_config) { should be_nil }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with :group1 => {:setting1 => 6, :setting2 => \'foo\'}, :group2 => {:setting3 => 8, :setting4 => \'bar\'}' do
|
34
|
+
describe 'the instance' do
|
35
|
+
before {
|
36
|
+
@config = ConfigureMe::Base.load(
|
37
|
+
:group1 => {
|
38
|
+
:setting1 => 6,
|
39
|
+
:setting2 => 'foo'
|
40
|
+
},
|
41
|
+
:group2 => {
|
42
|
+
:setting3 => 8,
|
43
|
+
:setting4 => 'bar'
|
44
|
+
}
|
45
|
+
)
|
46
|
+
}
|
47
|
+
|
48
|
+
subject { @config.instance }
|
49
|
+
its(:config_name) { should eql('root') }
|
50
|
+
it { should respond_to(:group1) }
|
51
|
+
it { should respond_to(:group2) }
|
52
|
+
it { should_not respond_to(:group3) }
|
53
|
+
|
54
|
+
describe 'group1' do
|
55
|
+
subject { @config.group1 }
|
56
|
+
it { should_not be_nil }
|
57
|
+
it { should respond_to(:setting1) }
|
58
|
+
it { should respond_to(:setting2) }
|
59
|
+
it { should_not respond_to(:setting3) }
|
60
|
+
its(:setting1) { should eql(6) }
|
61
|
+
its(:setting2) { should eql('foo') }
|
62
|
+
its(:parent_config) { should eql(@config.instance) }
|
63
|
+
end
|
64
|
+
|
65
|
+
describe 'group2' do
|
66
|
+
subject { @config.instance.group2 }
|
67
|
+
it { should_not be_nil }
|
68
|
+
it { should respond_to(:setting3) }
|
69
|
+
it { should respond_to(:setting4) }
|
70
|
+
it { should_not respond_to(:setting1) }
|
71
|
+
its(:setting3) { should eql(8) }
|
72
|
+
its(:setting4) { should eql('bar') }
|
73
|
+
its(:parent_config) { should eql(@config.instance) }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe ConfigureMe::Loading, 'something unsupported' do
|
80
|
+
subject { ConfigureMe::Base }
|
81
|
+
it 'should raise an exception' do
|
82
|
+
lambda { subject.load(123) }.should raise_error(ArgumentError)
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConfigureMe::Naming do
|
4
|
+
class NamingConfig < ConfigureMe::Base
|
5
|
+
end
|
6
|
+
|
7
|
+
before {
|
8
|
+
@persistence_klass = mock('PersistenceKlass') do
|
9
|
+
stubs(:model_name).returns('persistence')
|
10
|
+
end
|
11
|
+
ConfigureMe.stubs(:persistence_klass).returns(@persistence_klass)
|
12
|
+
}
|
13
|
+
subject { NamingConfig }
|
14
|
+
context 'when persisting' do
|
15
|
+
before { NamingConfig.stubs(:persisting?).returns(true) }
|
16
|
+
its(:model_name) { should eql('persistence') }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when not persisting' do
|
20
|
+
before { NamingConfig.stubs(:persisting?).returns(false) }
|
21
|
+
its(:model_name) { should_not eql('persistence') }
|
22
|
+
end
|
23
|
+
end
|