-
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.
- Loading branch information
Showing
7 changed files
with
120 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,53 @@ | ||
module RailsSettings | ||
class Configuration | ||
def initialize(*args, &block) | ||
options = args.extract_options! | ||
@default_options = args.extract_options! | ||
validate_options @default_options | ||
klass = args.shift | ||
keys = args | ||
|
||
raise ArgumentError unless klass | ||
|
||
@klass = klass | ||
@klass.class_attribute :default_settings, :setting_object_class_name | ||
@klass.default_settings = {} | ||
@klass.setting_object_class_name = options[:class_name] || 'RailsSettings::SettingObject' | ||
@klass.class_attribute :setting_keys | ||
@klass.setting_keys = {} | ||
|
||
if block_given? | ||
yield(self) | ||
else | ||
keys.each do |k| | ||
key(k) | ||
end | ||
keys.each { |k| key(k) } | ||
end | ||
|
||
raise ArgumentError.new('has_settings: No keys defined') if @klass.default_settings.blank? | ||
raise ArgumentError.new('has_settings: No keys defined') if @klass.setting_keys.empty? | ||
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) | ||
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 | ||
|
||
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 | ||
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