Class: Utils::ConfigFile

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

Defined Under Namespace

Classes: BlockConfig, Classify, ConfigFileError, Discover, Edit, FileFinder, Probe, Scope, 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.



21
22
# File 'lib/utils/config_file.rb', line 21

def initialize
end

Class Attribute Details

.config_file_pathsObject

Returns the value of attribute config_file_paths.



5
6
7
# File 'lib/utils/config_file.rb', line 5

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



683
684
685
686
687
688
# File 'lib/utils/config_file.rb', line 683

def classify(&block)
  if block
    @classify = Classify.new(&block)
  end
  @classify ||= Classify.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



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

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



357
358
359
360
361
362
# File 'lib/utils/config_file.rb', line 357

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



646
647
648
649
650
651
# File 'lib/utils/config_file.rb', line 646

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



74
75
76
77
# File 'lib/utils/config_file.rb', line 74

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/utils/config_file.rb', line 52

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



217
218
219
220
221
222
# File 'lib/utils/config_file.rb', line 217

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

#scope(&block) ⇒ Utils::Scope

The scope method initializes and returns a Scope object.

This method creates a new Scope instance either from the provided block or with default initialization if no block is given. The scope object is stored as an instance variable and reused on subsequent calls.

or default settings

Parameters:

  • block (Proc)

    optional block to pass to the Scope constructor

Returns:

  • (Utils::Scope)

    a Scope object configured with the provided block



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

def scope(&block)
  if block
    @scope = Scope.new(&block)
  end
  @scope ||= Scope.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



295
296
297
298
299
300
# File 'lib/utils/config_file.rb', line 295

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

#ssh_tunnel(&block) ⇒ Object



617
618
619
620
621
622
# File 'lib/utils/config_file.rb', line 617

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



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

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



726
727
728
729
730
731
# File 'lib/utils/config_file.rb', line 726

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



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

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