Class: Kasefet::Config
- Inherits:
-
Object
- Object
- Kasefet::Config
- Defined in:
- lib/kasefet/config.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each_keys ⇒ Object
- #flatten_hash(hash) ⇒ Object
- #format(file_path = @file) ⇒ Object
-
#initialize(file) ⇒ Config
constructor
A new instance of Config.
- #load ⇒ Object
- #parse(contents, file_path = @file) ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(file) ⇒ Config
Returns a new instance of Config.
3 4 5 6 |
# File 'lib/kasefet/config.rb', line 3 def initialize(file) @file = file @settings = {} end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
8 9 10 |
# File 'lib/kasefet/config.rb', line 8 def file @file end |
Instance Method Details
#[](key) ⇒ Object
10 11 12 |
# File 'lib/kasefet/config.rb', line 10 def [](key) return key.split(".").reduce(@settings) { |hash, segment| hash ? hash[segment] : nil } end |
#[]=(key, value) ⇒ Object
14 15 16 17 |
# File 'lib/kasefet/config.rb', line 14 def []=(key, value) last_segment = key.split(".")[0..-2].reduce(@settings) { |hash, segment| hash[segment] ||= {} } last_segment[key.split(".")[-1]] = value end |
#each_keys ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/kasefet/config.rb', line 19 def each_keys() return @settings.keys.each unless block_given? @settings.keys.each do |key| yield key end end |
#flatten_hash(hash) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kasefet/config.rb', line 81 def flatten_hash(hash) hash.reduce({}) do |result, pair| key, value = pair if value.is_a? Hash flatten_hash(value).each do |suffix, subvalue| result["#{key}.#{suffix}"] = subvalue end else result[key] = value end result end end |
#format(file_path = @file) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/kasefet/config.rb', line 60 def format(file_path = @file) case File.extname(file_path) when ".json" require 'json' return @settings.to_json when ".yaml", ".yml" require 'yaml' return @settings.to_yaml else return flatten_hash(@settings).map do |key, value| if value.is_a? Array value.map do |array_value| "#{key}=#{array_value}" end.join("\n") else "#{key}=#{value}" end end.join("\n") end end |
#load ⇒ Object
27 28 29 30 |
# File 'lib/kasefet/config.rb', line 27 def load return unless File.exists?(@file) parse(File.read(@file)) end |
#parse(contents, file_path = @file) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/kasefet/config.rb', line 32 def parse(contents, file_path = @file) case File.extname(file_path) when ".yaml", ".yml" require 'yaml' @settings = YAML.load(contents) when ".json" require 'json' @settings = JSON.parse(contents) else # try the key=value format content = contents content.split("\n").each do |line| key, value = line.split("=") value = value.to_i if value =~ /\d+/ if self[key] self[key] = [self[key]] unless self[key].is_a? Array self[key] << value else self[key] = value end end end end |
#save ⇒ Object
56 57 58 |
# File 'lib/kasefet/config.rb', line 56 def save File.write(@file, format()) end |