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.



38
39
# File 'lib/utils/config_file.rb', line 38

def initialize
end

Class Attribute Details

.config_file_pathsArray<String>

The config_file_paths accessor method provides read and write access to the config_file_paths instance variable.

Returns:

  • (Array<String>)

    the array of configuration file paths



16
17
18
# File 'lib/utils/config_file.rb', line 16

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



817
818
819
820
821
822
# File 'lib/utils/config_file.rb', line 817

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:



893
894
895
896
897
898
# File 'lib/utils/config_file.rb', line 893

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:



495
496
497
498
499
500
# File 'lib/utils/config_file.rb', line 495

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



49
50
51
52
53
# File 'lib/utils/config_file.rb', line 49

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



444
445
446
447
448
449
# File 'lib/utils/config_file.rb', line 444

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



774
775
776
777
778
779
# File 'lib/utils/config_file.rb', line 774

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



91
92
93
94
# File 'lib/utils/config_file.rb', line 91

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



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/utils/config_file.rb', line 69

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



284
285
286
287
288
289
# File 'lib/utils/config_file.rb', line 284

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



376
377
378
379
380
381
# File 'lib/utils/config_file.rb', line 376

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:



739
740
741
742
743
744
# File 'lib/utils/config_file.rb', line 739

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



545
546
547
548
549
550
# File 'lib/utils/config_file.rb', line 545

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



866
867
868
869
870
871
# File 'lib/utils/config_file.rb', line 866

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



907
908
909
910
911
912
913
# File 'lib/utils/config_file.rb', line 907

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