configure_me 0.2.1 → 0.3.1
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/lib/configure_me/base.rb +49 -4
- data/lib/configure_me/{persistence.rb → persisting.rb} +6 -6
- data/lib/configure_me/setting.rb +14 -3
- data/lib/configure_me/settings.rb +11 -9
- data/lib/configure_me/version.rb +1 -1
- data/lib/configure_me.rb +2 -1
- data/spec/configure_me/base_spec.rb +2 -7
- data/spec/configure_me/setting_spec.rb +2 -2
- metadata +5 -5
data/lib/configure_me/base.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'active_support/core_ext/hash/indifferent_access'
|
3
3
|
require 'singleton'
|
4
|
-
require 'configure_me/persistence'
|
5
|
-
require 'configure_me/caching'
|
6
4
|
require 'configure_me/settings'
|
7
5
|
require 'configure_me/nesting'
|
6
|
+
require 'configure_me/persisting'
|
7
|
+
require 'configure_me/caching'
|
8
8
|
|
9
9
|
module ConfigureMe
|
10
10
|
class Base
|
11
11
|
include ActiveModel::AttributeMethods
|
12
12
|
include Settings
|
13
|
-
include Persistence
|
14
|
-
include Caching
|
15
13
|
include Nesting
|
14
|
+
include Persisting
|
15
|
+
include Caching
|
16
16
|
include Singleton
|
17
17
|
extend ActiveModel::Naming
|
18
18
|
|
@@ -20,6 +20,51 @@ module ConfigureMe
|
|
20
20
|
def config_name
|
21
21
|
self.name.split('::').last.gsub(/^(.*)Config$/, '\1').downcase
|
22
22
|
end
|
23
|
+
|
24
|
+
def from_hash(root, config)
|
25
|
+
if root.nil?
|
26
|
+
root = const_set("RootConfig", Class.new(ConfigureMe::Base))
|
27
|
+
end
|
28
|
+
|
29
|
+
# Determine how many root keys there are.
|
30
|
+
#
|
31
|
+
# If more than one, create a new nested ConfigureMe::Base
|
32
|
+
# subclass for each key.
|
33
|
+
#
|
34
|
+
# If only one, just assign the values to the root config
|
35
|
+
if config.keys.length > 1
|
36
|
+
config.each_pair do |key, value|
|
37
|
+
if value.is_a?(Hash)
|
38
|
+
klass_name = "#{key}_config".camelize
|
39
|
+
c = root.const_set(klass_name, Class.new(ConfigureMe::Base))
|
40
|
+
from_hash(c, value)
|
41
|
+
c.send :nest_me, root
|
42
|
+
else
|
43
|
+
root.send :setting, key, :string, :default => value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
root
|
47
|
+
else
|
48
|
+
from_hash(root, config.values.first)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def load(config)
|
53
|
+
case config
|
54
|
+
when Hash
|
55
|
+
from_hash(nil, config)
|
56
|
+
else
|
57
|
+
raise ::ArgumentError, "ConfigureMe: Not sure how to load type [#{config.class}]"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def method_missing(method, *args)
|
62
|
+
if instance.respond_to?(method)
|
63
|
+
instance.send(method, *args)
|
64
|
+
else
|
65
|
+
super
|
66
|
+
end
|
67
|
+
end
|
23
68
|
end
|
24
69
|
|
25
70
|
def parent
|
@@ -1,23 +1,23 @@
|
|
1
1
|
require 'active_support/concern'
|
2
2
|
|
3
3
|
module ConfigureMe
|
4
|
-
module
|
4
|
+
module Persisting
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
included do
|
8
|
-
|
9
|
-
end
|
7
|
+
#included do
|
8
|
+
# @persisting = false
|
9
|
+
#end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
def persist_me(persistence_key = nil)
|
13
13
|
if ConfigureMe.persistence_klass.nil?
|
14
|
-
raise ::RuntimeError, "
|
14
|
+
raise ::RuntimeError, "ConfigureMe: persistence_klass is not set. Make sure you have an initializer to assign it."
|
15
15
|
end
|
16
16
|
@persisting = true
|
17
17
|
end
|
18
18
|
|
19
19
|
def persisting?
|
20
|
-
@persisting
|
20
|
+
@persisting ||= false
|
21
21
|
end
|
22
22
|
|
23
23
|
def persistence_key
|
data/lib/configure_me/setting.rb
CHANGED
@@ -1,16 +1,27 @@
|
|
1
1
|
module ConfigureMe
|
2
2
|
class Setting
|
3
|
-
attr_reader :name, :default
|
3
|
+
attr_reader :name, :type, :default
|
4
4
|
|
5
|
-
def initialize(owner, name, *args)
|
5
|
+
def initialize(owner, name, type, *args)
|
6
6
|
options = args.extract_options!
|
7
7
|
|
8
|
-
@owner, @name = owner, name.to_s
|
8
|
+
@owner, @name, @type = owner, name.to_s
|
9
9
|
@default = options.key?(:default) ? options[:default] : nil
|
10
10
|
end
|
11
11
|
|
12
12
|
def define_methods!
|
13
13
|
@owner.define_attribute_methods(true)
|
14
14
|
end
|
15
|
+
|
16
|
+
def convert(value)
|
17
|
+
case type
|
18
|
+
when :string then value
|
19
|
+
when :text then value
|
20
|
+
when :integer then value.to_i rescue value ? 1 : 0
|
21
|
+
when :float then value.to_f
|
22
|
+
when :date then ActiveRecord::ConnectionAdapters::Column.string_to_date(value)
|
23
|
+
else value
|
24
|
+
end
|
25
|
+
end
|
15
26
|
end
|
16
27
|
end
|
@@ -9,8 +9,8 @@ module ConfigureMe
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
def setting(name, options = {})
|
13
|
-
new_setting = Setting.new(self, name, options)
|
12
|
+
def setting(name, type, options = {})
|
13
|
+
new_setting = Setting.new(self, name, type, options)
|
14
14
|
class_settings[name] = new_setting
|
15
15
|
new_setting.define_methods!
|
16
16
|
end
|
@@ -34,29 +34,31 @@ module ConfigureMe
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def read_setting(name)
|
37
|
+
value = nil
|
37
38
|
if self.class.caching?
|
38
|
-
|
39
|
-
return setting unless setting.nil?
|
39
|
+
value = Rails.cache.read(cache_key(name))
|
40
40
|
end
|
41
41
|
|
42
|
-
if self.class.persisting?
|
42
|
+
if self.class.persisting? && value.nil?
|
43
43
|
setting = ConfigureMe.persistence_klass.find_by_key(persistence_key(name))
|
44
44
|
unless setting.nil?
|
45
45
|
value = YAML::load(setting.value)
|
46
|
-
|
46
|
+
if self.class.caching?
|
47
|
+
Rails.cache.write(cache_key(name), value)
|
48
|
+
end
|
47
49
|
@settings[name.to_sym] = value
|
48
|
-
return value
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
|
-
@settings[name.to_sym]
|
53
|
+
value = @settings[name.to_sym] if value.nil?
|
54
|
+
self.class.class_settings[name.to_sym].convert(value)
|
53
55
|
end
|
54
56
|
|
55
57
|
def write_setting(name, value)
|
56
58
|
if self.class.class_settings[name]
|
57
59
|
@settings[name.to_sym] = value
|
58
60
|
else
|
59
|
-
raise NoMethodError, "Unknown setting: #{name.inspect}"
|
61
|
+
raise NoMethodError, "ConfigureMe: Unknown setting: #{name.inspect}"
|
60
62
|
end
|
61
63
|
|
62
64
|
if self.class.caching?
|
data/lib/configure_me/version.rb
CHANGED
data/lib/configure_me.rb
CHANGED
@@ -12,7 +12,7 @@ describe ConfigureMe::Base do
|
|
12
12
|
describe 'with a "color" setting with a default of "red"' do
|
13
13
|
before(:each) do
|
14
14
|
@klass = Class.new(ConfigureMe::Base)
|
15
|
-
@klass.send :setting, :color, :default => 'red'
|
15
|
+
@klass.send :setting, :color, :string, :default => 'red'
|
16
16
|
end
|
17
17
|
|
18
18
|
describe 'an instance ' do
|
@@ -55,7 +55,7 @@ describe ConfigureMe::Base do
|
|
55
55
|
@klass = TestConfig
|
56
56
|
@persistence = mock('PersistenceClass')
|
57
57
|
ConfigureMe.persist_with @persistence
|
58
|
-
@klass.send :setting, :color, :default => 'red'
|
58
|
+
@klass.send :setting, :color, :string, :default => 'red'
|
59
59
|
@klass.send :persist_me
|
60
60
|
end
|
61
61
|
|
@@ -83,11 +83,6 @@ describe ConfigureMe::Base do
|
|
83
83
|
@persistence.stubs(:find_or_create_by_key).returns(@unpersisted)
|
84
84
|
@obj.color = "blue"
|
85
85
|
end
|
86
|
-
|
87
|
-
it 'should return the default value' do
|
88
|
-
@persistence.stubs(:find_by_key).returns(nil)
|
89
|
-
@obj.color.should eql('red')
|
90
|
-
end
|
91
86
|
end
|
92
87
|
|
93
88
|
describe 'with a persisted setting' do
|
@@ -5,7 +5,7 @@ describe ConfigureMe::Setting do
|
|
5
5
|
@owner = mock('owner') do
|
6
6
|
stubs(:define_attribute_methods)
|
7
7
|
end
|
8
|
-
@setting = ConfigureMe::Setting.new(@owner, :foo, :default => 'bar')
|
8
|
+
@setting = ConfigureMe::Setting.new(@owner, :foo, :string, :default => 'bar')
|
9
9
|
end
|
10
10
|
|
11
11
|
it { @setting.should respond_to(:name) }
|
@@ -17,7 +17,7 @@ describe ConfigureMe::Setting do
|
|
17
17
|
owner = mock('owner') do
|
18
18
|
expects(:define_attribute_methods).with(true)
|
19
19
|
end
|
20
|
-
setting = ConfigureMe::Setting.new(owner, :foo, :default => 'bar')
|
20
|
+
setting = ConfigureMe::Setting.new(owner, :foo, :string, :default => 'bar')
|
21
21
|
setting.define_methods!
|
22
22
|
end
|
23
23
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configure_me
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 1
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Williams
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-06 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -152,7 +152,7 @@ files:
|
|
152
152
|
- lib/configure_me/base.rb
|
153
153
|
- lib/configure_me/caching.rb
|
154
154
|
- lib/configure_me/nesting.rb
|
155
|
-
- lib/configure_me/
|
155
|
+
- lib/configure_me/persisting.rb
|
156
156
|
- lib/configure_me/setting.rb
|
157
157
|
- lib/configure_me/settings.rb
|
158
158
|
- lib/configure_me/version.rb
|