Module: ConfigSL::FileSupport::ClassMethods

Defined in:
lib/configsl/file_support.rb

Overview

Required class methods to support file operations.

Instance Method Summary collapse

Instance Method Details

#config_file_name(filename = nil) ⇒ String

The default name to use for the configuration file.

Parameters:

  • filename (String) (defaults to: nil)

    The name of the configuration file.

Returns:

  • (String)

    The name of 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.

Parameters:

  • path (String) (defaults to: nil)

    The path to the configuration file.

Returns:

  • (String)

    The path to 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.

Parameters:

  • path (String) (defaults to: nil)

    Optional path to look for the file. If provided, this method will raise an error the exact file is not found.

  • format (Symbol) (defaults to: nil)

    Optional format to look for the file in. If provided, will only match files of the given format. Ignored if path is provided.

Returns:

  • (Array<String>)

    Array of matching files.

Raises:



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.

Parameters:

  • extension (String)

    The file extension to get the format for.

Returns:

  • (Symbol)

    The format for the file.

Raises:



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.

Parameters:

  • format (Symbol) (defaults to: nil)

    Optional format to get the extensions for. If specified, only returns the extensions for that format.

Returns:

  • (Array<String>)


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.

Parameters:

  • format (Symbol)

    The format to register.

  • opts (Hash) (defaults to: {})

    The options for the format.

Options Hash (opts):

  • :class (Class)

    The class to use for the format. Defaults to ConfigSL::FileFormat::format, where format is capitalized.

  • :extensions (Array<String>)

    File extensions for the format. defaults to those defined in opts[:class].



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