Module: ConfigSL::FileSupport::ClassMethods
- Defined in:
- lib/configsl/file_support.rb
Overview
Required class methods to support file operations.
Instance Method Summary collapse
-
#config_file_name(filename = nil) ⇒ String
The default name to use for the configuration file.
-
#config_file_path(path = nil) ⇒ String
The default path to use for the configuration file.
-
#configsl_find_file(path: nil, format: nil) ⇒ Array<String>
Find the configuration file based on the default path, name, and defined formats.
-
#configsl_find_file_format(extension) ⇒ Symbol
Find the format for a file given its extension.
-
#file_extensions(format: nil) ⇒ Array<String>
The file extensions supported by this configuration.
-
#register_file_format(format, opts = {}) ⇒ Object
Register support for a file format.
Instance Method Details
#config_file_name(filename = nil) ⇒ String
The default name to use for the configuration file.
29 30 31 32 |
# File 'lib/configsl/file_support.rb', line 29 def config_file_name(filename = nil) @config_file_name = filename unless filename.nil? @config_file_name ||= 'config' end |
#config_file_path(path = nil) ⇒ String
The default path to use for the configuration file.
38 39 40 41 |
# File 'lib/configsl/file_support.rb', line 38 def config_file_path(path = nil) @config_file_path = path unless path.nil? @config_file_path ||= '.' end |
#configsl_find_file(path: nil, format: nil) ⇒ Array<String>
Find the configuration file based on the default path, name, and defined formats.
The returned files are ordered based on how their formats are defined. For example:
register_file_format :yaml
register_file_format :json
would result in the YAML file being returned first if both are found.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/configsl/file_support.rb', line 92 def configsl_find_file(path: nil, format: nil) paths = Dir.glob( path.nil? ? "#{config_file_name}.{#{file_extensions(format:).join(',')}}" : path, base: path.nil? ? config_file_path : nil ) raise FileNotFoundError, 'No configuration file found!' if paths.empty? path.nil? ? paths.map { |p| File.join(config_file_path, p) } : paths end |
#configsl_find_file_format(extension) ⇒ Symbol
Find the format for a file given its extension.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/configsl/file_support.rb', line 110 def configsl_find_file_format(extension) raise FileFormatError, 'No file formats have been defined' if @config_file_formats.nil? extension = extension.sub(/^\./, '').to_sym (@config_file_formats || {}).each do |format, opts| return format if opts[:extensions].include?(extension) end raise FileFormatError, "No file format found for extension: #{extension}" end |
#file_extensions(format: nil) ⇒ Array<String>
The file extensions supported by this configuration.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/configsl/file_support.rb', line 48 def file_extensions(format: nil) @config_file_formats ||= {} if format return @config_file_formats[format][:extensions] if @config_file_formats.key?(format) raise FileFormatError, "File format not found: #{format}" end @config_file_formats.values.map { |o| o[:extensions] }.flatten end |
#register_file_format(format, opts = {}) ⇒ Object
Register support for a file format.
67 68 69 70 71 72 73 |
# File 'lib/configsl/file_support.rb', line 67 def register_file_format(format, opts = {}) @config_file_formats ||= {} opts[:class] ||= ConfigSL::FileFormat.const_get(format.capitalize) opts[:extensions] ||= opts[:class].extensions @config_file_formats[format] = opts end |