Sha256: cd3004b023b6e68b60765975d2b05cf2cd560d8eaaa2694b610a283ad6916f70

Contents?: true

Size: 791 Bytes

Versions: 1

Compression:

Stored size: 791 Bytes

Contents

# frozen_string_literal: true

require 'yaml'

module Conifer
  class File
    NotFoundError = Class.new(StandardError)

    attr_reader :name, :prefix, :dir

    def initialize(name, dir:, prefix: nil)
      @name = name
      @prefix = prefix
      @dir = dir
    end

    def [](key)
      args = key.split('.').tap { |v| v.prepend(prefix) if prefix.present? }
      config.dig(*args)
    end

    private

    def config
      @config ||= YAML.safe_load(ERB.new(::File.read(path)).result)
    end

    def path(directory = dir)
      file = ::File.join(directory, name).to_s

      if ::File.exist?(file)
        file
      else
        raise NotFoundError, "Could not find file #{name}" if directory == '/'

        path(::File.expand_path('..', directory))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
conifer-0.1.0 lib/conifer/file.rb