Class: Utils::ConfigFile

Inherits:
Object show all
Includes:
DSLKit::Interpreter
Defined in:
lib/utils/config_file.rb

Overview

Configuration file manager for Utils library.

This class provides functionality for loading, parsing, and managing configuration settings from multiple sources. It supports DSL-style configuration blocks and integrates with various utility components to provide centralized configuration management.

Defined Under Namespace

Classes: BlockConfig, Classify, CodeComment, CodeIndexer, ConfigFileError, Discover, Edit, FileFinder, Probe, Search, SshTunnel, StripSpaces, SyncDir

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigFile

The initialize method sets up a new instance of the class.

This method is called when creating a new object and performs any necessary initialization tasks for the instance variables and internal state.



33
34
# File 'lib/utils/config_file.rb', line 33

def initialize
end

Class Attribute Details

.config_file_pathsObject

Returns the value of attribute config_file_paths.



11
12
13
# File 'lib/utils/config_file.rb', line 11

def config_file_paths
  @config_file_paths
end

Instance Method Details

#classify(&block) ⇒ Classify

The classify method initializes and returns a Classify object.

This method creates a Classify instance either from the provided block or with default settings if no block is given. It ensures that only one Classify object is created per instance by storing it in an instance variable.

Parameters:

  • block (Proc)

    optional block to configure the Classify object

Returns:

  • (Classify)

    a Classify object configured either by the block or with defaults



772
773
774
775
776
777
# File 'lib/utils/config_file.rb', line 772

def classify(&block)
  if block
    @classify = Classify.new(&block)
  end
  @classify ||= Classify.new
end

#code_comment(&block) ⇒ Utils::ConfigFile::CodeComment

The code_comment method provides access to a CodeComment configuration instance.

This method returns the existing CodeComment instance if one has already been created, or initializes and returns a new CodeComment instance if no instance exists. If a block is provided, it will be passed to the CodeComment constructor when creating a new instance.

Parameters:

  • block (Proc)

    optional block to configure the CodeComment object

Returns:



848
849
850
851
852
853
# File 'lib/utils/config_file.rb', line 848

def code_comment(&block)
  if block
    @code_comment = CodeComment.new(&block)
  end
  @code_comment ||= CodeComment.new
end

#code_indexer(&block) ⇒ Utils::ConfigFile::CodeIndexer

The code_indexer method manages and returns a CodeIndexer configuration instance.

This method provides access to a CodeIndexer object that handles configuration for generating code indexes such as ctags and cscope files. It ensures that only one CodeIndexer instance is created per object by storing it in an instance variable. When a block is provided, it initializes the CodeIndexer with custom settings; otherwise, it returns a default CodeIndexer instance.

Parameters:

  • block (Proc)

    optional block to configure the CodeIndexer object

Returns:



450
451
452
453
454
455
# File 'lib/utils/config_file.rb', line 450

def code_indexer(&block)
  if block
    @code_indexer = CodeIndexer.new(&block)
  end
  @code_indexer ||= CodeIndexer.new
end

#configure_from_paths(paths = self.class.config_file_paths) ⇒ Object

The configure_from_paths method initializes the configuration by parsing configuration files from the specified paths.

This method iterates through an array of configuration file paths and processes each one to load the configuration settings. It is typically used to set up the application’s configuration from multiple sources.

Parameters:

  • paths (Array<String>) (defaults to: self.class.config_file_paths)

    an array of file paths pointing to configuration files



44
45
46
47
48
# File 'lib/utils/config_file.rb', line 44

def configure_from_paths(paths = self.class.config_file_paths)
  for config_file_path in paths
    parse_config_file config_file_path
  end
end

#discover(&block) ⇒ Utils::Discover

The discover method initializes and returns a Discover object.

This method sets up a Discover instance, either using a provided block for configuration or creating a default instance. It ensures that only one Discover object is created per instance by storing it in an instance variable.

provided block or with default settings

Parameters:

  • block (Proc)

    optional block to configure the Discover object

Returns:

  • (Utils::Discover)

    a Discover object configured either with the



401
402
403
404
405
406
# File 'lib/utils/config_file.rb', line 401

def discover(&block)
  if block
    @discover = Discover.new(&block)
  end
  @discover ||= Discover.new
end

#edit(&block) ⇒ Edit

The edit method initializes and returns an Edit object.

This method creates an Edit instance either from a provided block or with default settings. It stores the Edit object as an instance variable and returns it on subsequent calls.

Parameters:

  • block (Proc)

    optional block to configure the Edit object

Returns:

  • (Edit)

    an Edit object configured either by the block or with default settings



729
730
731
732
733
734
# File 'lib/utils/config_file.rb', line 729

def edit(&block)
  if block
    @edit = Edit.new(&block)
  end
  @edit ||= Edit.new
end

#parse(source) ⇒ Object

The parse method processes the provided source code by interpreting it within the given binding context.

This method takes a source code string and evaluates it in the context of the specified binding, allowing for dynamic execution of code with access to the current variable scope.

Parameters:

  • source (String)

    the source code to be interpreted and executed

Returns:

  • (Object)

    returns self after processing the source code



86
87
88
89
# File 'lib/utils/config_file.rb', line 86

def parse(source)
  interpret_with_binding source, binding
  self
end

#parse_config_file(config_file_path) ⇒ Utils::ConfigFile

Note:

The method will output a warning message to standard error if it fails to read the configuration file and return nil.

The parse_config_file method reads and processes a configuration file.

This method opens the specified configuration file, reads its contents, and parses the configuration data. It handles file path expansion and includes error handling for system call errors during file operations.

Parameters:

  • config_file_path (String)

    the path to the configuration file to be parsed

Returns:

Raises:

  • (SystemCallError)

    if there is an issue reading the configuration file



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/utils/config_file.rb', line 64

def parse_config_file(config_file_path)
  config_file_path = File.expand_path(config_file_path)
  File.open(config_file_path) do |cf|
    parse cf.read
  end
  self
rescue SystemCallError => e
  $DEBUG and warn "Couldn't read config file "\
    "#{config_file_path.inspect}: #{e.class} #{e}"
  return nil
end

#probe(&block) ⇒ Utils::Probe

The probe method initializes and returns a Probe object.

This method creates a new Probe instance either from the provided block or with default settings, storing it for later use. It ensures that only one Probe instance is created per object, returning the existing instance on subsequent calls.

Parameters:

  • block (Proc)

    optional block to configure the Probe instance

Returns:

  • (Utils::Probe)

    a Probe instance configured either by the block or with default settings



241
242
243
244
245
246
# File 'lib/utils/config_file.rb', line 241

def probe(&block)
  if block
    @probe = Probe.new(&block)
  end
  @probe ||= Probe.new
end

#search(&block) ⇒ Utils::Search

The search method initializes and returns a Search object.

This method creates a Search instance either from a provided block or with default settings. It maintains a cached instance of the Search object, returning the same instance on subsequent calls.

block or with default settings

Parameters:

  • block (Proc)

    optional block to configure the Search object

Returns:

  • (Utils::Search)

    a Search object configured either by the provided



333
334
335
336
337
338
# File 'lib/utils/config_file.rb', line 333

def search(&block)
  if block
    @search = Search.new(&block)
  end
  @search ||= Search.new
end

#ssh_tunnel(&block) ⇒ Utils::ConfigFile::SshTunnel

The ssh_tunnel method provides access to an SSH tunnel configuration instance.

This method returns the existing SSH tunnel configuration object if one has already been created, or initializes and returns a new SSH tunnel configuration instance if no instance exists. If a block is provided, it will be passed to the SSH tunnel configuration constructor when creating a new instance.

Parameters:

  • block (Proc)

    optional block to configure the SSH tunnel object

Returns:



694
695
696
697
698
699
# File 'lib/utils/config_file.rb', line 694

def ssh_tunnel(&block)
  if block
    @ssh_tunnel = SshTunnel.new(&block)
  end
  @ssh_tunnel ||= SshTunnel.new
end

#strip_spaces(&block) ⇒ Utils::StripSpaces

The strip_spaces method configures and returns a StripSpaces object for processing whitespace.

This method initializes a StripSpaces processor that can be used to remove or modify whitespace in strings. When a block is provided, it sets up the processor with custom behavior defined by the block. Otherwise, it returns a default StripSpaces instance.

Parameters:

  • block (Proc)

    optional block to customize the strip spaces behavior

Returns:

  • (Utils::StripSpaces)

    a configured StripSpaces processor instance



500
501
502
503
504
505
# File 'lib/utils/config_file.rb', line 500

def strip_spaces(&block)
  if block
    @strip_spaces = StripSpaces.new(&block)
  end
  @strip_spaces ||= StripSpaces.new
end

#sync_dir(&block) ⇒ SyncDir

The sync_dir method provides access to a SyncDir instance.

This method returns the existing SyncDir instance if one has already been created, or initializes and returns a new SyncDir instance if no instance exists. If a block is provided, it will be passed to the SyncDir constructor when creating a new instance.

Returns:

  • (SyncDir)

    the SyncDir instance associated with this object



821
822
823
824
825
826
# File 'lib/utils/config_file.rb', line 821

def sync_dir(&block)
  if block
    @sync_dir = SyncDir.new(&block)
  end
  @sync_dir ||= SyncDir.new
end

#to_rubyString

The to_ruby method generates a Ruby configuration string by collecting configuration data from various components and combining them into a single formatted output.

Returns:

  • (String)

    a Ruby formatted string containing configuration settings from search, discover, strip_spaces, probe, ssh_tunnel, edit, and classify components



862
863
864
865
866
867
868
# File 'lib/utils/config_file.rb', line 862

def to_ruby
  result = "# vim: set ft=ruby:\n"
  for bc in %w[search discover strip_spaces probe ssh_tunnel edit classify]
    result << "\n" << __send__(bc).to_ruby
  end
  result
end