Sha256: 54515babfd690b6bf6ec377ef24c83c29e8815c688b648f62067d349795fe097

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'erb'

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

    attr_reader :name, :dir, :prefix, :format, :options

    def initialize(name, dir:, prefix: nil, format: :yml, **options)
      @name = name
      @dir = dir
      @prefix = prefix
      @format = format
      @options = options
    end

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

    def parsed
      @parsed ||= case format
                  when :yml, :yaml
                    YAML.safe_load(ERB.new(::File.read(path)).result, **options)
                  when :json
                    JSON.parse(ERB.new(::File.read(path)).result, **options)
                  else
                    raise UnsupportedFormatError, "Format '#{format}' is not supported"
                  end
    end

    def path
      return @path if defined? @path

      @path = find_path
    end

    def exists?
      !path.nil?
    end

    def filename
      "#{::File.basename(name.to_s, ".#{format}")}.#{format}"
    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-2.0.0 lib/conifer/file.rb