lib/kramdown/options.rb in kramdown-2.3.0 vs lib/kramdown/options.rb in kramdown-2.3.1
- old
+ new
@@ -37,10 +37,11 @@
# Allowed option types.
ALLOWED_TYPES = [String, Integer, Float, Symbol, Boolean, Object]
@options = {}
+ @cached_defaults = nil
# Define a new option called +name+ (a Symbol) with the given +type+ (String, Integer, Float,
# Symbol, Boolean, Object), default value +default+ and the description +desc+. If a block is
# specified, it should validate the value and either raise an error or return a valid value.
#
@@ -52,10 +53,11 @@
raise ArgumentError, "Option name #{name} is already used" if @options.key?(name)
raise ArgumentError, "Invalid option type #{type} specified" unless ALLOWED_TYPES.include?(type)
raise ArgumentError, "Invalid type for default value" if !(type === default) && !default.nil?
raise ArgumentError, "Missing validator block" if type == Object && block.nil?
@options[name] = Definition.new(name, type, default, desc, block)
+ @cached_defaults = nil
end
# Return all option definitions.
def self.definitions
@options
@@ -66,18 +68,20 @@
@options.key?(name.to_sym)
end
# Return a Hash with the default values for all options.
def self.defaults
- temp = {}
- @options.each {|_n, o| temp[o.name] = o.default }
- temp
+ @cached_defaults ||= begin
+ temp = {}
+ @options.each {|_n, o| temp[o.name] = o.default }
+ temp.freeze
+ end
end
# Merge the #defaults Hash with the *parsed* options from the given Hash, i.e. only valid option
# names are considered and their value is run through the #parse method.
def self.merge(hash)
- temp = defaults
+ temp = defaults.dup
hash.each do |k, v|
k = k.to_sym
temp[k] = @options.key?(k) ? parse(k, v) : v
end
temp