Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

OptionHash: support deprecated keys

  • Loading branch information...
commit 7f84efd53a95c84521e1119e5f82a4be46b4051e 1 parent a4064c4
@minad minad authored
View
17 lib/temple/hash.rb
@@ -67,8 +67,9 @@ def update(hash)
class OptionHash < MutableHash
def initialize(*hash, &block)
super(*hash)
- @invalid = block
+ @handler = block
@valid = {}
+ @deprecated = {}
end
def []=(key, value)
@@ -85,16 +86,26 @@ def valid_keys
keys.concat(@valid.keys).uniq
end
- def add(*keys)
+ def add_valid_keys(*keys)
keys.flatten.each { |key| @valid[key] = true }
end
+ def add_deprecated_keys(*keys)
+ keys.flatten.each { |key| @valid[key] = @deprecated[key] = true }
+ end
+
def validate_hash!(hash)
hash.keys.each {|key| validate_key!(key) }
end
def validate_key!(key)
- @invalid.call(self, key) unless valid_key?(key)
+ @handler.call(self, key, true) if deprecated_key?(key)
+ @handler.call(self, key, false) unless valid_key?(key)
+ end
+
+ def deprecated_key?(key)
+ @deprecated.include?(key) ||
+ @hash.any? {|h| h.deprecated_key?(key) if h.respond_to?(:deprecated_key?) }
end
def valid_key?(key)
View
2  lib/temple/mixins/engine_dsl.rb
@@ -98,7 +98,7 @@ def chain_name(name)
def chain_class_constructor(filter, option_filter)
local_options = Hash === option_filter.last ? option_filter.pop : nil
raise(ArgumentError, 'Only symbols allowed in option filter') unless option_filter.all? {|o| Symbol === o }
- default_options.add(option_filter) if respond_to?(:default_options)
+ default_options.add_valid_keys(option_filter) if respond_to?(:default_options)
proc do |engine|
filter.new(ImmutableHash.new(local_options, engine.options.only(option_filter)))
end
View
23 lib/temple/mixins/options.rb
@@ -8,18 +8,33 @@ def set_default_options(opts)
def default_options
@default_options ||= OptionHash.new(superclass.respond_to?(:default_options) ?
- superclass.default_options : nil) do |hash, key|
- raise ArgumentError, "Option #{key.inspect} is not supported by #{self}" unless @option_validator_disabled
+ superclass.default_options : nil) do |hash, key, deprecated|
+ unless @option_validator_disabled
+ if deprecated
+ puts "Option #{key.inspect} is deprecated by #{self}"
+ else
+ raise ArgumentError, "Option #{key.inspect} is not supported by #{self}"
+ end
+ end
end
end
def define_options(*opts)
if opts.last.respond_to?(:keys)
hash = opts.pop
- default_options.add(hash.keys)
+ default_options.add_valid_keys(hash.keys)
default_options.update(hash)
end
- default_options.add(opts)
+ default_options.add_valid_keys(opts)
+ end
+
+ def deprecated_options(*opts)
+ if opts.last.respond_to?(:keys)
+ hash = opts.pop
+ default_options.add_deprecated_keys(hash.keys)
+ default_options.update(hash)
+ end
+ default_options.add_deprecated_keys(opts)
end
def disable_option_validator!
Please sign in to comment.
Something went wrong with that request. Please try again.