Sha256: ca069285dc5bf379ea9f65b92c72f0ad5150ad84e3fd8d51c16392518dfd4034

Contents?: true

Size: 1004 Bytes

Versions: 1

Compression:

Stored size: 1004 Bytes

Contents

require 'psych'

module Hako
  module YamlLoader
    class << self
      def load(path)
        class_loader = Psych::ClassLoader.new
        scanner = Psych::ScalarScanner.new(class_loader)
        prev_path = @current_path
        @current_path = path
        visitor = Visitor.new(scanner, class_loader) do |_, val|
          load(@current_path.parent.join(val))
        end
        path.open do |f|
          visitor.accept(Psych.parse(f))
        end
      ensure
        @current_path = prev_path
      end
    end

    class Visitor < Psych::Visitors::ToRuby
      INCLUDE_TAG = 'tag:include'.freeze
      SHOVEL = '<<'.freeze

      def initialize(scanner, class_loader, &block)
        super(scanner, class_loader)
        @domain_types[INCLUDE_TAG] = [INCLUDE_TAG, block]
      end

      def revive_hash(hash, o)
        super(hash, o).tap do |r|
          if r[SHOVEL].is_a?(Hash)
            h = r.delete(SHOVEL)
            r.merge!(h)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hako-0.7.0 lib/hako/yaml_loader.rb