Sha256: 34c59b24a011708fb69c549dbba26c3885921235e58cd5af3f7e268ad9c4bd5c

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'erb'

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 }
      parsed.dig(*args)
    end

    def parsed
      @parsed ||= YAML.safe_load(ERB.new(::File.read(path)).result)
    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.0.0 lib/conifer/file.rb