lib/conifer/file.rb in conifer-0.3.0 vs lib/conifer/file.rb in conifer-1.0.0
- old
+ new
@@ -15,35 +15,45 @@
@dir = dir
end
def [](key)
args = key.split('.').tap { |v| v.prepend(prefix) if prefix }
- config.dig(*args)
+ parsed.dig(*args)
end
- def as_hash
- config
+ def parsed
+ @parsed ||= YAML.safe_load(ERB.new(::File.read(path)).result)
end
- private
+ def path
+ return @path if defined? @path
- def config
- @config ||= YAML.safe_load(ERB.new(::File.read(path)).result)
+ @path = find_path
end
- def path(directory = dir)
+ 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
- raise NotFoundError, "Could not find file #{filename}" if directory == '/'
+ return if directory == '/'
- path(::File.expand_path('..', directory))
+ find_path(::File.expand_path('..', directory))
end
- end
-
- def filename
- "#{::File.basename(name.to_s, '.yml')}.yml"
end
end
end