dg-vrnetze/filegen

View on GitHub
lib/filegen/data_sources/yaml.rb

Summary

Maintainability
A
0 mins
Test Coverage
# encoding: utf-8
module Filegen
  # Data Sources
  module DataSources
    # Data source which makes yaml files available
    class Yaml
      private

      attr_reader :source

      public

      # Create data source
      def initialize(file)
        @source = HashWithIndifferentAccess.new(Psych.load_file(file))
      rescue Errno::ENOENT
        raise Exceptions::YamlFileNotFound, JSON.dump(file: file)
      end

      def fetch(key, default_value = nil)
        source[key.to_sym] || source[key.to_s] || default_value
      end
    end
  end
end