lib/safe_yaml/resolver.rb in safe_yaml-0.8.6 vs lib/safe_yaml/resolver.rb in safe_yaml-0.9.0

- old
+ new

@@ -1,18 +1,18 @@ module SafeYAML class Resolver - def initialize - @whitelist = SafeYAML::OPTIONS[:whitelisted_tags] || [] - @initializers = SafeYAML::OPTIONS[:custom_initializers] || {} - @raise_on_unknown_tag = SafeYAML::OPTIONS[:raise_on_unknown_tag] + def initialize(options) + @options = SafeYAML::OPTIONS.merge(options || {}) + @whitelist = @options[:whitelisted_tags] || [] + @initializers = @options[:custom_initializers] || {} + @raise_on_unknown_tag = @options[:raise_on_unknown_tag] end def resolve_node(node) - if not node - return node - end - + return node if !node + return self.native_resolve(node) if tag_is_whitelisted?(self.get_node_tag(node)) + case self.get_node_type(node) when :root resolve_root(node) when :map resolve_map(node) @@ -26,17 +26,14 @@ raise "Don't know how to resolve this node: #{node.inspect}" end end def resolve_map(node) - tag = get_and_check_node_tag(node) - return self.native_resolve(node) if tag_is_whitelisted?(tag) - + tag = get_and_check_node_tag(node) hash = @initializers.include?(tag) ? @initializers[tag].call : {} + map = normalize_map(self.get_node_value(node)) - map = normalize_map(self.get_node_value(node)) - # Take the "<<" key nodes first, as these are meant to approximate a form of inheritance. inheritors = map.select { |key_node, value_node| resolve_node(key_node) == "<<" } inheritors.each do |key_node, value_node| merge_into_hash(hash, resolve_node(value_node)) end @@ -57,20 +54,24 @@ seq.inject(arr) { |array, node| array << resolve_node(node) } end def resolve_scalar(node) - Transform.to_proper_type(self.get_node_value(node), self.value_is_quoted?(node), get_and_check_node_tag(node)) + Transform.to_proper_type(self.get_node_value(node), self.value_is_quoted?(node), get_and_check_node_tag(node), @options) end def get_and_check_node_tag(node) tag = self.get_node_tag(node) - SafeYAML.tag_safety_check!(tag) + SafeYAML.tag_safety_check!(tag, @options) tag end def tag_is_whitelisted?(tag) @whitelist.include?(tag) + end + + def options + @options end private def normalize_map(map) # Syck creates Hashes from maps.