Sha256: 9107649e9398756cf16da5a90b92b6a1dc94ea9bc848241c572449a8971d56c4

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Specimen
  module Runtime
    class YmlParser
      class YmlTypeError < StandardError; end
      class YmlERBError < StandardError; end
      class YmlDataError < StandardError; end

      def self.parse!(file)
        new(file).parse
      end

      def initialize(file)
        @file = file
      end

      def parse
        specimen_yml_data
      end

      private

      def specimen_yml
        @specimen_yml ||= File.read(@file)
      end

      def specimen_yml_erb
        @specimen_yml_erb ||= ERB.new(specimen_yml, trim_mode: '%').result(binding)
      rescue StandardError
        raise YmlERBError, "#{@file} could not be parsed with ERB!"
      end

      def specimen_yml_data
        return @specimen_yml_data if @specimen_yml_data

        data = Psych.load(specimen_yml_erb, aliases: true)

        raise YmlTypeError, 'specimen.yml data could not be parsed' unless data.is_a?(Hash)

        @specimen_yml_data = data
      rescue StandardError
        raise YmlDataError, "Could not read data from: '#{@file}'!"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
specimen-0.0.3.alpha lib/specimen/runtime/yml_parser.rb