Sha256: 790e4a219789578ee43767dc2366a078d76db4638faa4a66b393ae8cbfe91881

Contents?: true

Size: 1.19 KB

Versions: 30

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true
require 'psych'

module Hako
  class YamlLoader
    def initialize
      @current_path = nil
    end

    # @param [String] path
    # @return [Hash]
    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

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

      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)
            h2 = {}
            r.each do |k, v|
              if k == SHOVEL
                h2.merge!(v)
              else
                h2[k] = v
              end
            end
            r.replace(h2)
          end
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
hako-1.1.0 lib/hako/yaml_loader.rb
hako-1.0.1 lib/hako/yaml_loader.rb
hako-1.0.0 lib/hako/yaml_loader.rb
hako-0.27.0 lib/hako/yaml_loader.rb
hako-0.26.0 lib/hako/yaml_loader.rb
hako-0.25.2 lib/hako/yaml_loader.rb
hako-0.25.1 lib/hako/yaml_loader.rb
hako-0.25.0 lib/hako/yaml_loader.rb
hako-0.24.0 lib/hako/yaml_loader.rb
hako-0.23.0 lib/hako/yaml_loader.rb
hako-0.22.0 lib/hako/yaml_loader.rb
hako-0.21.1 lib/hako/yaml_loader.rb
hako-0.21.0 lib/hako/yaml_loader.rb
hako-0.20.3 lib/hako/yaml_loader.rb
hako-0.20.2 lib/hako/yaml_loader.rb
hako-0.20.1 lib/hako/yaml_loader.rb
hako-0.20.0 lib/hako/yaml_loader.rb
hako-0.19.0 lib/hako/yaml_loader.rb
hako-0.18.1 lib/hako/yaml_loader.rb
hako-0.18.0 lib/hako/yaml_loader.rb