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
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'configure_me'
|
2
|
-
|
3
|
-
Given /^a (.*) class that derives from ConfigureMe::Base$/ do |name|
|
4
|
-
@klass = Class.new(ConfigureMe::Base)
|
5
|
-
end
|
6
|
-
|
7
|
-
Given /^I add a "([^"]*)" setting with a type of "([^"]*)" and a default of "([^"]*)"$/ do |name, type, default|
|
8
|
-
@klass.send :setting, name.to_sym, type.to_sym, :default => default
|
9
|
-
end
|
10
|
-
|
11
|
-
When /^I create an instance of the class$/ do
|
12
|
-
@obj = @klass.new
|
13
|
-
end
|
14
|
-
|
15
|
-
When /^I create another instance of the class$/ do
|
16
|
-
@obj2 = @klass.new
|
17
|
-
end
|
18
|
-
|
19
|
-
When /^I read the "([^"]*)" setting$/ do |name|
|
20
|
-
@result = @obj.send name.to_sym
|
21
|
-
end
|
22
|
-
|
23
|
-
When /^I read the new class's "([^"]*)" setting$/ do |name|
|
24
|
-
@result = @obj2.send name.to_sym
|
25
|
-
end
|
26
|
-
|
27
|
-
When /^I set the "([^"]*)" setting to "([^"]*)"$/ do |name, value|
|
28
|
-
@obj.send "#{name}=".to_sym, value
|
29
|
-
end
|
30
|
-
|
31
|
-
Then /^the result should be "([^"]*)"$/ do |value|
|
32
|
-
@result.should eql(value)
|
33
|
-
end
|
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
|
-
module ConfigureMe
|
4
|
-
module Settings
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
included do
|
8
|
-
attribute_method_suffix('', '=')
|
9
|
-
end
|
10
|
-
|
11
|
-
module ClassMethods
|
12
|
-
def setting(name, type, options = {})
|
13
|
-
new_setting = Setting.new(self, name, type, options)
|
14
|
-
class_settings[name] = new_setting
|
15
|
-
new_setting.define_methods!
|
16
|
-
end
|
17
|
-
|
18
|
-
def class_settings
|
19
|
-
@class_settings ||= {}.with_indifferent_access
|
20
|
-
end
|
21
|
-
|
22
|
-
def define_attribute_methods(force = false)
|
23
|
-
return unless class_settings
|
24
|
-
undefine_attribute_methods if force
|
25
|
-
super(class_settings.keys)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def settings_from_class_settings
|
30
|
-
self.class.class_settings.inject({}.with_indifferent_access) do |settings, (name, setting)|
|
31
|
-
settings[name] = setting.default
|
32
|
-
settings
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def read_setting(name)
|
37
|
-
value = nil
|
38
|
-
if self.class.caching?
|
39
|
-
value = Rails.cache.read(cache_key(name))
|
40
|
-
end
|
41
|
-
|
42
|
-
if self.class.persisting? && value.nil?
|
43
|
-
setting = ConfigureMe.persistence_klass.find_by_key(persistence_key(name))
|
44
|
-
unless setting.nil?
|
45
|
-
value = YAML::load(setting.value)
|
46
|
-
if self.class.caching?
|
47
|
-
Rails.cache.write(cache_key(name), value)
|
48
|
-
end
|
49
|
-
@settings[name.to_sym] = value
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
value = @settings[name.to_sym] if value.nil?
|
54
|
-
self.class.class_settings[name.to_sym].convert(value)
|
55
|
-
end
|
56
|
-
|
57
|
-
def write_setting(name, value)
|
58
|
-
if self.class.class_settings[name]
|
59
|
-
@settings[name.to_sym] = value
|
60
|
-
else
|
61
|
-
raise NoMethodError, "ConfigureMe: Unknown setting: #{name.inspect}"
|
62
|
-
end
|
63
|
-
|
64
|
-
if self.class.caching?
|
65
|
-
Rails.cache.write(cache_key(name), value)
|
66
|
-
end
|
67
|
-
|
68
|
-
if self.class.persisting?
|
69
|
-
setting = ConfigureMe.persistence_klass.find_or_create_by_key(persistence_key(name))
|
70
|
-
setting.value = value.to_yaml
|
71
|
-
setting.save!
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
protected
|
77
|
-
|
78
|
-
def attribute_method?(name)
|
79
|
-
self.class.class_settings.key?(name)
|
80
|
-
end
|
81
|
-
|
82
|
-
private
|
83
|
-
|
84
|
-
def settings=(new_settings)
|
85
|
-
return unless new_settings.is_a?(Hash)
|
86
|
-
settings = new_settings.stringify_keys
|
87
|
-
|
88
|
-
settings.each do |k, v|
|
89
|
-
respond_to?("#{k}=".to_sym) ? send("#{k}=".to_sym, v) : raise(UnknownMethodError, "Unknown setting: #{k}")
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def attribute(name)
|
94
|
-
read_setting(name.to_sym)
|
95
|
-
end
|
96
|
-
|
97
|
-
def attribute=(name, value)
|
98
|
-
write_setting(name.to_sym, value)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'rails/generators/active_record'
|
2
|
-
|
3
|
-
class ConfigureMeGenerator < ActiveRecord::Generators::Base #Rails::Generators::NamedBase
|
4
|
-
include Rails::Generators::Migration
|
5
|
-
|
6
|
-
source_root File.expand_path('../templates', __FILE__)
|
7
|
-
|
8
|
-
def generate_model
|
9
|
-
template 'model.rb', "app/models/#{singular_name}.rb"
|
10
|
-
end
|
11
|
-
|
12
|
-
def generate_migration
|
13
|
-
migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb"
|
14
|
-
end
|
15
|
-
|
16
|
-
def generate_initializer
|
17
|
-
template 'initializer.rb', 'config/initializers/init_configure_me.rb'
|
18
|
-
end
|
19
|
-
end
|
@@ -1 +0,0 @@
|
|
1
|
-
ConfigureMe.persist_with <%= class_name %>
|