Sha256: 71280224b8c65269d323a324725d5a91a59ca662524462ea2a71db2905d55b89

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module SafeYAML
  class SyckResolver
    QUOTE_STYLES = [:quote1, :quote2]

    def resolve_node(node)
      case node.value
      when Hash
        return resolve_map(node)
      when Array
        return resolve_seq(node)
      when String
        return resolve_scalar(node)
      else
        raise "Don't know how to resolve this node: #{node.inspect}"
      end
    end

    def resolve_map(node)
      map = node.value

      hash = {}

      # Take the "<<" key nodes first, as these are meant to approximate a form of inheritance.
      inheritors = map.keys.select { |node| resolve_node(node) == "<<" }
      inheritors.each do |key|
        value_node = map.delete(key)
        hash.merge!(resolve_node(value_node))
      end

      # All that's left should be normal (non-"<<") nodes.
      map.each do |key_node, value_node|
        hash[resolve_node(key_node)] = resolve_node(value_node)
      end

      return hash
    end

    def resolve_seq(node)
      seq = node.value
      seq.map { |node| resolve_node(node) }
    end

    def resolve_scalar(node)
      Transform.to_proper_type(node.value, QUOTE_STYLES.include?(node.instance_variable_get(:@style)), node.type_id)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
safe_yaml-0.7.0 lib/safe_yaml/syck_resolver.rb