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

#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`.



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