Sha256: 82af56f38d4b83dcb4cae6565c6c57acba2be914934643d36d56a34bcf5589c7
Contents?: true
Size: 1.14 KB
Versions: 1
Compression:
Stored size: 1.14 KB
Contents
# frozen_string_literal: true require 'yaml' require 'erb' module Conifer class File NotFoundError = Class.new(StandardError) attr_reader :name, :prefix, :dir, :allowed_classes def initialize(name, dir:, prefix: nil, allowed_classes: []) @name = name @prefix = prefix @dir = dir @allowed_classes = allowed_classes end def [](key) args = key.split('.').tap { |v| v.prepend(prefix) if prefix } parsed.dig(*args) end def parsed @parsed ||= YAML.safe_load(ERB.new(::File.read(path)).result, allowed_classes) end def path return @path if defined? @path @path = find_path end def exists? !path.nil? end def filename "#{::File.basename(name.to_s, '.yml')}.yml" end def validate! raise NotFoundError, "Could not find file #{filename}" if path.nil? end private def find_path(directory = dir) file = ::File.join(directory, filename).to_s if ::File.exist?(file) file else return if directory == '/' find_path(::File.expand_path('..', directory)) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
conifer-1.1.0 | lib/conifer/file.rb |