### Installation Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Instructions on how to install the ParseConfig gem using RubyGems or add it to your Gemfile. ```APIDOC ## Installation Install the gem from RubyGems or add it to your Gemfile. ```ruby # Via gem install # $ gem install parseconfig # In Gemfile gem 'parseconfig' # Then require in your code require 'parseconfig' ``` ``` -------------------------------- ### Start Development Environment with Docker Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Set up Docker containers for development and launch a bash shell within the parseconfig dev container. ```bash $ make dev |> parseconfig <| src # ``` -------------------------------- ### Install and Require ParseConfig Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Instructions for installing the gem via command line or Gemfile and requiring it in Ruby code. ```ruby # Via gem install # $ gem install parseconfig # In Gemfile gem 'parseconfig' # Then require in your code require 'parseconfig' ``` -------------------------------- ### Start Docker Compose Services Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Manually start Docker Compose services in detached mode. ```bash $ docker-compose up -d ``` -------------------------------- ### Example Configuration File Format Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Illustrates the standard Unix/Linux configuration file format supported by ParseConfig, including comments, key-value pairs, quoted values, options without values, and grouped sections. ```ini # Example configuration file format # Lines starting with # or ; are comments ; This is also a comment # Simple key-value pairs app_name = MyApplication version = 1.0.5 debug_mode = true # Values can be quoted (quotes are stripped) description = "My awesome application" path = '/usr/local/bin' # Options without values are set to boolean true enable_feature verbose # Groups are defined with brackets [server] host = 0.0.0.0 port = 8080 workers = 4 [database] host = localhost port = 5432 name = myapp_production pool_size = 10 # Groups can have inline comments [logging] # Application logging settings level = info file = /var/log/app.log ``` -------------------------------- ### Install ParseConfig Gem Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Install the ParseConfig gem using the RubyGems package manager. ```bash $ gem install parseconfig ``` -------------------------------- ### Retrieve Group Names Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Get an array of all section names defined in the configuration file. ```ruby require 'parseconfig' # Example config: # app_name = MyApp # [production] # db_host = prod.db.local # [staging] # db_host = staging.db.local # [development] # db_host = localhost config = ParseConfig.new('/etc/myapp/database.conf') groups = config.get_groups # => ["production", "staging", "development"] # Load environment-specific configuration env = ENV['RACK_ENV'] || 'development' if config.get_groups.include?(env) db_host = config[env]['db_host'] puts "Using database: #{db_host}" end ``` -------------------------------- ### Retrieve Parameter Names Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Get an array of all top-level parameter and group names, useful for iterating through configuration structures. ```ruby require 'parseconfig' # Example config: # app_name = MyApp # version = 1.0 # [server] # host = localhost # [database] # host = db.local config = ParseConfig.new('/etc/myapp/settings.conf') params = config.get_params # => ["app_name", "version", "server", "database"] # Iterate through all parameters config.get_params.each do |param| value = config[param] if value.is_a?(Hash) puts "Group [#{param}]: #{value.keys.join(', ')}" else puts "#{param} = #{value}" end end # Output: # app_name = MyApp # version = 1.0 # Group [server]: host # Group [database]: host ``` -------------------------------- ### Run Unit Tests Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Execute the project's unit tests using the provided make command. ```bash $ make test ``` -------------------------------- ### Accessing Configuration Parameters Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Demonstrates basic access to configuration parameters and nested group values. Ensure the config object is loaded with data before accessing. ```ruby puts config['app_name'] # => "MyApplication" puts config['database']['host'] # => "newhost" ``` -------------------------------- ### ParseConfig.new - Create a New Configuration Parser Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Initialize a ParseConfig object by passing the path to a configuration file. Customization options for separators and comment characters are available. ```APIDOC ## ParseConfig.new - Create a New Configuration Parser Initialize a ParseConfig object by passing the path to a configuration file. The constructor automatically reads and parses the file, making all parameters immediately accessible. You can customize the separator character and comment characters. ```ruby require 'parseconfig' # Basic usage - parse a config file config = ParseConfig.new('/etc/myapp/settings.conf') # Custom separator (default is '=') config = ParseConfig.new('/etc/myapp/settings.conf', ':') # Custom comment characters (default is ['#', ';']) config = ParseConfig.new('/etc/myapp/settings.conf', '=', ['#']) # Create empty config object (no file) config = ParseConfig.new ``` ``` -------------------------------- ### Initialize ParseConfig Object Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Create a new configuration parser instance, optionally specifying custom separators or comment characters. ```ruby require 'parseconfig' # Basic usage - parse a config file config = ParseConfig.new('/etc/myapp/settings.conf') # Custom separator (default is '=') config = ParseConfig.new('/etc/myapp/settings.conf', ':') # Custom comment characters (default is ['#', ';']) config = ParseConfig.new('/etc/myapp/settings.conf', '=', ['#']) # Create empty config object (no file) config = ParseConfig.new ``` -------------------------------- ### Accessing Configuration Values with [] Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Access configuration parameters using hash-like bracket notation. Top-level parameters return string values, group names return a hash, and non-value options return true. ```APIDOC ## Accessing Configuration Values with [] Access configuration parameters using hash-like bracket notation. Top-level parameters return string values, while group names return a hash of all parameters within that group. ```ruby require 'parseconfig' # Example config file content: # admin_email = root@localhost # listen_port = 8080 # [database] # host = localhost # port = 5432 config = ParseConfig.new('/etc/myapp/settings.conf') # Access top-level parameters email = config['admin_email'] # => "root@localhost" port = config['listen_port'] # => "8080" # Access group parameters db_config = config['database'] # => {"host"=>"localhost", "port"=>"5432"} db_host = config['database']['host'] # => "localhost" db_port = config['database']['port'] # => "5432" # Non-value options return true # Config line: "debug_mode" (no = value) debug = config['debug_mode'] # => true ``` ``` -------------------------------- ### Write Configuration to File Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Write the current configuration object to a file-like object. ```ruby file = File.open('/path/to/config/file', 'w') config.write(file) file.close ``` -------------------------------- ### Load and Access Configuration Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Load a configuration file and access its parameters and groups using the ParseConfig library. ```ruby require 'parseconfig' config = ParseConfig.new('/path/to/config/example.conf') puts config['param1'] puts config['group1']['group1_param1'] ``` -------------------------------- ### Write Configuration to File or Stream Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Writes the current configuration to an output stream, such as a file or STDOUT. Values can be written with or without quotes. The default is quoted values to STDOUT. ```ruby require 'parseconfig' config = ParseConfig.new # Build configuration config.add('app_name', 'MyApp') config.add('version', '1.0') config.add('server', { 'host' => 'localhost', 'port' => '8080' }) config.add('database', { 'host' => 'db.local', 'name' => 'myapp' }) # Write to STDOUT (default) with quoted values (default) config.write # Output: # app_name = "MyApp" # version = "1.0" # # [server] # host = "localhost" # port = "8080" # # [database] # host = "db.local" # name = "myapp" # Write to file without quotes File.open('/etc/myapp/settings.conf', 'w') do |file| config.write(file, false) end # File contents: # app_name = MyApp # version = 1.0 # # [server] # host = localhost # port = 8080 # # [database] # host = db.local # name = myapp ``` -------------------------------- ### write - Write Configuration to File or Stream Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Writes the current configuration to an output stream (file or STDOUT). Values can be written with or without quotes. The output format is `param = value` with grouped sections. ```APIDOC ## write - Write Configuration to File or Stream ### Description Write the current configuration to an output stream (file or STDOUT). Parameters can be written with or without quotes around values. The output format maintains the standard `param = value` structure with grouped sections. ### Method `write(output_stream = STDOUT, quote_values = true)` ### Parameters - **output_stream** (IO Object, optional) - The stream to write the configuration to. Defaults to STDOUT. - **quote_values** (Boolean, optional) - Whether to quote values in the output. Defaults to true. ### Request Example ```ruby require 'parseconfig' config = ParseConfig.new # Build configuration config.add('app_name', 'MyApp') config.add('version', '1.0') config.add('server', { 'host' => 'localhost', 'port' => '8080' }) config.add('database', { 'host' => 'db.local', 'name' => 'myapp' }) # Write to STDOUT (default) with quoted values (default) config.write # Write to file without quotes File.open('/etc/myapp/settings.conf', 'w') do |file| config.write(file, false) end ``` ### Response Example ``` # Output to STDOUT: # app_name = "MyApp" # version = "1.0" # # [server] # host = "localhost" # port = "8080" # # [database] # host = "db.local" # name = "myapp" # File contents: # app_name = MyApp # version = 1.0 # # [server] # host = localhost # port = 8080 # # [database] # host = db.local # name = myapp ``` ``` -------------------------------- ### get_params - Retrieve All Parameter Names Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Returns an array of all top-level parameter names and group names defined in the configuration file. Useful for iterating through all available configuration options. ```APIDOC ## get_params - Retrieve All Parameter Names Returns an array of all top-level parameter names and group names defined in the configuration file. Useful for iterating through all available configuration options. ```ruby require 'parseconfig' # Example config: # app_name = MyApp # version = 1.0 # [server] # host = localhost # [database] # host = db.local config = ParseConfig.new('/etc/myapp/settings.conf') params = config.get_params # => ["app_name", "version", "server", "database"] # Iterate through all parameters config.get_params.each do |param| value = config[param] if value.is_a?(Hash) puts "Group [#{param}]: #{value.keys.join(', ')}" else puts "#{param} = #{value}" end end # Output: # app_name = MyApp # version = 1.0 # Group [server]: host # Group [database]: host ``` ``` -------------------------------- ### eql? / == - Compare Configurations Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Compares two ParseConfig objects for equality. Configurations are considered equal if they contain the same parameters with identical values and the same groups. ```APIDOC ## eql? / == - Compare Configurations ### Description Compare two ParseConfig objects for equality. Two configurations are equal if they have the same parameters with identical values and the same groups. ### Method `eql?(other_config)` or `==(other_config)` ### Parameters - **other_config** (ParseConfig) - The other ParseConfig object to compare against. ### Request Example ```ruby require 'parseconfig' config1 = ParseConfig.new('/etc/myapp/settings.conf') config2 = ParseConfig.new('/etc/myapp/settings.conf') config3 = ParseConfig.new('/etc/myapp/other.conf') # Compare configurations if config1.eql?(config2) puts "Configurations are identical" end # Also works with == if config1 == config2 puts "Configurations match" end # Useful for detecting configuration changes original = ParseConfig.new('/etc/myapp/settings.conf') # ... application modifies config object ... if original != config1 puts "Configuration has been modified" config1.write(File.open('/etc/myapp/settings.conf', 'w')) end ``` ### Response Example ``` Configurations are identical Configurations match Configuration has been modified ``` ``` -------------------------------- ### Access Configuration Values Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Retrieve configuration parameters using bracket notation, where groups return hashes and missing values return true. ```ruby require 'parseconfig' # Example config file content: # admin_email = root@localhost # listen_port = 8080 # [database] # host = localhost # port = 5432 config = ParseConfig.new('/etc/myapp/settings.conf') # Access top-level parameters email = config['admin_email'] # => "root@localhost" port = config['listen_port'] # => "8080" # Access group parameters db_config = config['database'] # => {"host"=>"localhost", "port"=>"5432"} db_host = config['database']['host'] # => "localhost" db_port = config['database']['port'] # => "5432" # Non-value options return true # Config line: "debug_mode" (no = value) debug = config['debug_mode'] # => true ``` -------------------------------- ### Configuration File Format Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Details the standard Unix/Linux configuration file format supported by ParseConfig, including comments, key-value pairs, groups, and options without values. ```APIDOC ## Configuration File Format ### Description ParseConfig supports standard Unix/Linux configuration file format with comments, key-value pairs, groups, and options without values. ### Syntax - **Comments**: Lines starting with `#` or `;` are treated as comments. - **Key-Value Pairs**: `key = value` or `key: value`. - **Quoted Values**: Values can be enclosed in single or double quotes. Quotes are stripped upon parsing. - **Options without Values**: Parameters listed without an equals sign or value are set to a boolean `true`. - **Groups**: Sections are defined using square brackets, e.g., `[group_name]`. - **Inline Comments**: Comments can follow a group definition or a key-value pair on the same line, separated by whitespace. ### Example ```ini # Example configuration file format ; This is also a comment # Simple key-value pairs app_name = MyApplication version = 1.0.5 debug_mode = true # Values can be quoted (quotes are stripped) description = "My awesome application" path = '/usr/local/bin' # Options without values are set to boolean true enable_feature verbose # Groups are defined with brackets [server] host = 0.0.0.0 port = 8080 workers = 4 [database] host = localhost port = 5432 name = myapp_production pool_size = 10 # Groups can have inline comments [logging] # Application logging settings level = info file = /var/log/app.log ``` ``` -------------------------------- ### Add Parameters Programmatically Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Add or merge parameters and groups into the configuration object, with an option to override existing groups. ```ruby require 'parseconfig' config = ParseConfig.new # Add simple parameters config.add('app_name', 'MyApplication') config.add('version', '2.0') config.add('debug', true) # Add a new group with parameters config.add('database', { 'host' => 'localhost', 'port' => '5432', 'name' => 'myapp_db' }) # Merge additional parameters into existing group config.add('database', { 'username' => 'admin', 'pool_size' => '10' }) # database group now has: host, port, name, username, pool_size # Override existing group completely config.add('database', {'host' => 'newhost'}, true) ``` -------------------------------- ### Compare Configurations for Equality Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Compares two ParseConfig objects to check if they are identical in terms of parameters and values. This method is useful for detecting configuration changes. ```ruby require 'parseconfig' config1 = ParseConfig.new('/etc/myapp/settings.conf') config2 = ParseConfig.new('/etc/myapp/settings.conf') config3 = ParseConfig.new('/etc/myapp/other.conf') # Compare configurations if config1.eql?(config2) puts "Configurations are identical" end # Also works with == if config1 == config2 puts "Configurations match" end # Useful for detecting configuration changes original = ParseConfig.new('/etc/myapp/settings.conf') # ... application modifies config object ... if original != config1 puts "Configuration has been modified" config1.write(File.open('/etc/myapp/settings.conf', 'w')) end ``` -------------------------------- ### get_groups - Retrieve All Group Names Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Returns an array of all group names (sections enclosed in brackets) defined in the configuration file. Groups organize related parameters into logical sections. ```APIDOC ## get_groups - Retrieve All Group Names Returns an array of all group names (sections enclosed in brackets) defined in the configuration file. Groups organize related parameters into logical sections. ```ruby require 'parseconfig' # Example config: # app_name = MyApp # [production] # db_host = prod.db.local # [staging] # db_host = staging.db.local # [development] # db_host = localhost config = ParseConfig.new('/etc/myapp/database.conf') groups = config.get_groups # => ["production", "staging", "development"] # Load environment-specific configuration env = ENV['RACK_ENV'] || 'development' if config.get_groups.include?(env) db_host = config[env]['db_host'] puts "Using database: #{db_host}" end ``` ``` -------------------------------- ### Access Alternative Ruby Versions in Docker Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Execute bash in a Docker container for a specific Ruby version. ```bash $ docker-compose exec parseconfig-rb26 /bin/bash |> parseconfig-rb26 <| src # ``` -------------------------------- ### add - Add Parameters Programmatically Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Add new parameters or groups to the configuration object. Pass a hash as the value to create a new group. Existing groups can be merged or overridden. ```APIDOC ## add - Add Parameters Programmatically Add new parameters or groups to the configuration object. Pass a hash as the value to create a new group. If a group already exists, parameters are merged unless override is set to true. ```ruby require 'parseconfig' config = ParseConfig.new # Add simple parameters config.add('app_name', 'MyApplication') config.add('version', '2.0') config.add('debug', true) # Add a new group with parameters config.add('database', { 'host' => 'localhost', 'port' => '5432', 'name' => 'myapp_db' }) # Merge additional parameters into existing group config.add('database', { 'username' => 'admin', 'pool_size' => '10' }) # database group now has: host, port, name, username, pool_size # Override existing group completely config.add('database', {'host' => 'newhost'}, true) ``` ``` -------------------------------- ### Add Parameters to a Group Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Adds individual parameters to a specified group. If the group does not exist, it will be created automatically. Useful for building configurations incrementally. ```ruby require 'parseconfig' config = ParseConfig.new # Add parameters to a group (group is created if it doesn't exist) config.add_to_group('server', 'host', '0.0.0.0') config.add_to_group('server', 'port', '8080') config.add_to_group('server', 'workers', '4') config.add_to_group('logging', 'level', 'info') config.add_to_group('logging', 'file', '/var/log/app.log') puts config.get_groups # => ["server", "logging"] puts config['server']['host'] # => "0.0.0.0" puts config['server']['port'] # => "8080" puts config['logging']['level'] # => "info" ``` -------------------------------- ### add_to_group - Add Parameters to a Group Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Adds individual parameters to a specified group. If the group does not exist, it will be created automatically. This method facilitates incremental configuration building. ```APIDOC ## add_to_group - Add Parameters to a Group ### Description Add individual parameters to a specific group. If the group does not exist, it is created automatically. This method is useful for building configuration incrementally. ### Method `add_to_group(group_name, param_name, param_value)` ### Parameters - **group_name** (String) - The name of the group to add the parameter to. - **param_name** (String) - The name of the parameter to add. - **param_value** (String) - The value of the parameter. ### Request Example ```ruby require 'parseconfig' config = ParseConfig.new # Add parameters to a group (group is created if it doesn't exist) config.add_to_group('server', 'host', '0.0.0.0') config.add_to_group('server', 'port', '8080') config.add_to_group('server', 'workers', '4') config.add_to_group('logging', 'level', 'info') config.add_to_group('logging', 'file', '/var/log/app.log') ``` ### Response Example ```ruby puts config.get_groups # => ["server", "logging"] puts config['server']['host'] # => "0.0.0.0" puts config['server']['port'] # => "8080" puts config['logging']['level'] # => "info" ``` ``` -------------------------------- ### Direct Attribute Access Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Provides direct access to the internal data structures of a ParseConfig object, including the configuration file path, parameters hash, and groups array. Use for advanced manipulation. ```ruby require 'parseconfig' config = ParseConfig.new('/etc/myapp/settings.conf') # Access the file path puts config.config_file # => "/etc/myapp/settings.conf" # Access the params hash directly puts config.params.class # => Hash puts config.params.keys # => ["param1", "param2", "group1", ...] # Access the groups array directly puts config.groups.class # => Array puts config.groups # => ["group1", "group2", ...] # Modify params directly (advanced usage) config.params['new_param'] = 'new_value' config.params['server']['timeout'] = '30' ``` -------------------------------- ### Execute Bash in Docker Container Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Access a running Docker container via bash. ```bash $ docker-compose exec parseconfig /bin/bash ``` -------------------------------- ### Add ParseConfig to Gemfile Source: https://github.com/datafolklabs/ruby-parseconfig/blob/master/README.md Include the ParseConfig gem in your project's Gemfile for dependency management. ```ruby gem 'parseconfig' ``` -------------------------------- ### Direct Attribute Access Source: https://context7.com/datafolklabs/ruby-parseconfig/llms.txt Provides direct access to the internal data structures of a ParseConfig object, including `params`, `groups`, and `config_file`, for advanced manipulation. ```APIDOC ## Direct Attribute Access ### Description Access the underlying data structures directly through the `params`, `groups`, and `config_file` attributes. These provide direct access to the internal state for advanced manipulation. ### Attributes - **params** (Hash) - Direct access to the hash containing all configuration parameters and groups. - **groups** (Array) - Direct access to the array listing all defined group names. - **config_file** (String) - The path to the configuration file that was loaded. ### Request Example ```ruby require 'parseconfig' config = ParseConfig.new('/etc/myapp/settings.conf') # Access the file path puts config.config_file # => "/etc/myapp/settings.conf" # Access the params hash directly puts config.params.class # => Hash puts config.params.keys # => ["param1", "param2", "group1", ...] # Access the groups array directly puts config.groups.class # => Array puts config.groups # => ["group1", "group2", ...] # Modify params directly (advanced usage) config.params['new_param'] = 'new_value' config.params['server']['timeout'] = '30' ``` ### Response Example ``` /etc/myapp/settings.conf Hash ["param1", "param2", "group1", ...] Array ["group1", "group2", ...] ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.