Class: Utils::ConfigFile

Inherits:
Object show all
Includes:
DSLKit::Interpreter
Defined in:
lib/utils/config_file.rb,
lib/utils/config_file/block_config.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.



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

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



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

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



714
715
716
717
718
719
# File 'lib/utils/config_file.rb', line 714

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:



790
791
792
793
794
795
# File 'lib/utils/config_file.rb', line 790

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:



392
393
394
395
396
397
# File 'lib/utils/config_file.rb', line 392

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



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

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



341
342
343
344
345
346
# File 'lib/utils/config_file.rb', line 341

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



671
672
673
674
675
676
# File 'lib/utils/config_file.rb', line 671

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

#irb_server_urlString

The irb_server_url method configures the URL for the IRB server communication.

This method sets up a DSL accessor for the IRB server URL, providing a default value that uses a Unix domain socket located in the current working directory.

Returns:

  • (String)

    the configured IRB server URL including the default socket path



186
# File 'lib/utils/config_file.rb', line 186

dsl_accessor :irb_server_url, "unix://#{Pathname.pwd + 'irb.socket'}"

#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



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

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



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

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



170
171
172
173
174
175
# File 'lib/utils/config_file.rb', line 170

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



273
274
275
276
277
278
# File 'lib/utils/config_file.rb', line 273

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:



636
637
638
639
640
641
# File 'lib/utils/config_file.rb', line 636

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



442
443
444
445
446
447
# File 'lib/utils/config_file.rb', line 442

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



763
764
765
766
767
768
# File 'lib/utils/config_file.rb', line 763

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



804
805
806
807
808
809
810
# File 'lib/utils/config_file.rb', line 804

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