-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Allow using different classes for each setting" (#88)
- Loading branch information
Showing
8 changed files
with
52 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,32 @@ | ||
module RailsSettings | ||
class Configuration | ||
def initialize(*args, &block) | ||
@default_options = args.extract_options! | ||
validate_options @default_options | ||
options = args.extract_options! | ||
klass = args.shift | ||
keys = args | ||
|
||
raise ArgumentError unless klass | ||
|
||
@klass = klass | ||
@klass.class_attribute :setting_keys | ||
@klass.setting_keys = {} | ||
@klass.class_attribute :default_settings, :setting_object_class_name | ||
@klass.default_settings = {} | ||
@klass.setting_object_class_name = options[:class_name] || 'RailsSettings::SettingObject' | ||
|
||
if block_given? | ||
yield(self) | ||
else | ||
keys.each { |k| key(k) } | ||
keys.each do |k| | ||
key(k) | ||
end | ||
end | ||
|
||
raise ArgumentError.new('has_settings: No keys defined') if @klass.setting_keys.empty? | ||
raise ArgumentError.new('has_settings: No keys defined') if @klass.default_settings.blank? | ||
end | ||
|
||
def key(name, options={}) | ||
validate_name name | ||
validate_options options | ||
options = @default_options.merge(options) | ||
|
||
@klass.setting_keys[name] = { | ||
:default_value => (options[:defaults] || {}).stringify_keys.freeze, | ||
:class_name => (options[:class_name] || 'RailsSettings::SettingObject') | ||
} | ||
end | ||
|
||
private | ||
|
||
def validate_name(name) | ||
raise ArgumentError.new("has_settings: Symbol expected, but got a #{name.class}") unless name.is_a?(Symbol) | ||
end | ||
|
||
def validate_options(options) | ||
valid_options = [:defaults, :class_name] | ||
options.each do |key, value| | ||
unless valid_options.include?(key) | ||
raise ArgumentError.new("has_settings: Invalid option #{key}") | ||
end | ||
end | ||
if options[:class_name] && !options[:class_name].constantize.ancestors.include?(RailsSettings::SettingObject) | ||
raise ArgumentError.new("has_settings: #{options[:class_name]} must be a subclass of RailsSettings::SettingObject") | ||
end | ||
raise ArgumentError.new("has_settings: Option :defaults expected, but got #{options.keys.join(', ')}") unless options.blank? || (options.keys == [:defaults]) | ||
@klass.default_settings[name] = (options[:defaults] || {}).stringify_keys.freeze | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters