class Conifer::File
Constants
- NotFoundError
- UnsupportedFormatError
Attributes
allowed_classes[R]
dir[R]
format[R]
name[R]
prefix[R]
Public Class Methods
new(name, dir:, prefix: nil, format: :yml, allowed_classes: [])
click to toggle source
# File lib/conifer/file.rb, line 13 def initialize(name, dir:, prefix: nil, format: :yml, allowed_classes: []) @name = name @dir = dir @prefix = prefix @format = format @allowed_classes = allowed_classes end
Public Instance Methods
[](key)
click to toggle source
# File lib/conifer/file.rb, line 21 def [](key) args = key.split('.').tap { |v| v.prepend(prefix) if prefix } parsed.dig(*args) end
exists?()
click to toggle source
# File lib/conifer/file.rb, line 43 def exists? !path.nil? end
filename()
click to toggle source
# File lib/conifer/file.rb, line 47 def filename "#{::File.basename(name.to_s, ".#{format}")}.#{format}" end
parsed()
click to toggle source
# File lib/conifer/file.rb, line 26 def parsed @parsed ||= case format when :yml, :yaml YAML.safe_load(ERB.new(::File.read(path)).result, allowed_classes) when :json JSON.parse(ERB.new(::File.read(path)).result) else raise UnsupportedFormatError, "Format '#{format}' is not supported" end end
path()
click to toggle source
# File lib/conifer/file.rb, line 37 def path return @path if defined? @path @path = find_path end
validate!()
click to toggle source
# File lib/conifer/file.rb, line 51 def validate! raise NotFoundError, "Could not find file #{filename}" if path.nil? end
Private Instance Methods
find_path(directory = dir)
click to toggle source
# File lib/conifer/file.rb, line 57 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