Class: KVCSV::Settings
- Inherits:
-
Object
- Object
- KVCSV::Settings
- 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”.
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
-
#[](key) ⇒ String, ...
Access a setting value by key.
-
#fetch(key, default = nil) ⇒ Object
Access a setting value with optional default.
-
#initialize(*file_paths) ⇒ Settings
constructor
Initialize a new Settings object with one or more CSV files.
-
#map {|key, value| ... } ⇒ Array
Iterate over settings with map.
-
#select {|key, value| ... } ⇒ Hash
Select settings matching criteria.
Constructor Details
#initialize(*file_paths) ⇒ Settings
Non-existent files are silently ignored
Later files override values from earlier files
Initialize a new Settings object with one or more CSV files
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
51 |
# File 'lib/kvcsv/settings.rb', line 51 def_delegators :@settings, :[], :fetch, :map, :select |
#select {|key, value| ... } ⇒ Hash
Select settings matching criteria
51 |
# File 'lib/kvcsv/settings.rb', line 51 def_delegators :@settings, :[], :fetch, :map, :select |