Class: MyCnf
- Inherits:
-
Object
- Object
- MyCnf
- Defined in:
- lib/mycnf.rb
Class Method Summary collapse
- .compare(*cnf) ⇒ Object
- .compare_files(*file_pathes) ⇒ Object
- .diff(*cnf) ⇒ Object
- .diff_files(*file_pathes) ⇒ Object
- .generate(hash) ⇒ Object
- .parse(file_path) ⇒ Object
Class Method Details
.compare(*cnf) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/mycnf.rb', line 74 def self.compare(*cnf) size = cnf.size result = {} cnf.each_with_index do |c, i| result = _compare(c, size, i, result) end result end |
.compare_files(*file_pathes) ⇒ Object
69 70 71 72 |
# File 'lib/mycnf.rb', line 69 def self.compare_files(*file_pathes) cnfs = file_pathes.map { |path| parse(path) } compare(*cnfs) end |
.diff(*cnf) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mycnf.rb', line 55 def self.diff(*cnf) compare = compare(*cnf) result = {} compare.each do |section, params| params.each do |param, values| if values.uniq.size != 1 result[section] ||= {} result[section][param] = values end end end result end |
.diff_files(*file_pathes) ⇒ Object
50 51 52 53 |
# File 'lib/mycnf.rb', line 50 def self.diff_files(*file_pathes) cnfs = file_pathes.map { |path| parse(path) } diff(*cnfs) end |
.generate(hash) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mycnf.rb', line 34 def self.generate(hash) result = '' hash.each do |section, params| result << %Q[[#{section.to_s}]\n] params.each do |param, value| if value == '' result << %Q[#{param}\n] else result << %Q[#{param} = #{value}\n] end end result << %Q[\n] end result.chomp end |
.parse(file_path) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mycnf.rb', line 2 def self.parse(file_path) cnf = {} section = nil File.read(file_path).each_line do |line| line.gsub!(/#.*/, '') line.strip! next if line == '' if /\[(.+)\]/ =~ line section = $1.to_sym next end next if section.nil? next unless /^([^\=]+)\=?(.*)/ =~ line param, value = [ $1.strip, $2.strip ] param = param.gsub('-', '_').to_sym value = case value when /^\d+$/; value.to_i when /^true$/i; true when /^false$/i; false else value end cnf[section] ||= {} cnf[section][param] = value end cnf end |