Class: KVCSV::Settings

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/kvcsv/settings.rb

Overview

Settings class for managing application settings from CSV files

This class loads settings from one or more CSV files and provides read-only access to the merged configuration. CSV files should have two columns: “key” and “value”.

Examples:

Basic usage

settings = KVCSV::Settings.new("config/app.csv", "config/local.csv")
database_host = settings[:database_host]
debug_mode = settings[:debug]

Using fetch with default value

port = settings.fetch(:port, 3000)

CSV file format

key,value
database_host,localhost
database_port,5432
debug,true
cache_enabled,false

Constant Summary collapse

TRUE_VALUES =

Values that are converted to true

%w[t 1 true yes y].freeze
FALSE_VALUES =

Values that are converted to false

%w[f 0 false no n].freeze
NIL_VALUES =

Values that are converted to nil

%w[nil null na n/a].freeze

Instance Method Summary collapse

Constructor Details

#initialize(*file_paths) ⇒ Settings

Note:

Non-existent files are silently ignored

Note:

Later files override values from earlier files

Initialize a new Settings object with one or more CSV files

Examples:

Load from multiple files

settings = KVCSV::Settings.new(
  "config/defaults.csv",
  "config/environment.csv",
  "config/local.csv"
)


74
75
76
77
78
79
80
81
82
# File 'lib/kvcsv/settings.rb', line 74

def initialize(*file_paths)
  @settings = {}
  file_paths.compact.each do |file_path|
    next unless File.exist?(file_path)

    load_file(file_path)
  end
  symbolize_keys!
end

Instance Method Details

#[](key) ⇒ String, ...

Access a setting value by key



51
# File 'lib/kvcsv/settings.rb', line 51

def_delegators :@settings, :[], :fetch, :map, :select

#fetch(key, default = nil) ⇒ Object

Access a setting value with optional default



51
# File 'lib/kvcsv/settings.rb', line 51

def_delegators :@settings, :[], :fetch, :map, :select

#map {|key, value| ... } ⇒ Array

Iterate over settings with map

Yields:

  • (key, value)

    Each key-value pair



51
# File 'lib/kvcsv/settings.rb', line 51

def_delegators :@settings, :[], :fetch, :map, :select

#select {|key, value| ... } ⇒ Hash

Select settings matching criteria

Yields:

  • (key, value)

    Each key-value pair



51
# File 'lib/kvcsv/settings.rb', line 51

def_delegators :@settings, :[], :fetch, :map, :select