Module: Rbcli::Config

Defined in:
lib/rbcli/util/config.rb

Class Method Summary collapse

Class Method Details

.add_categorized_defaults(name, description, config) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbcli/util/config.rb', line 55

def self.add_categorized_defaults name, description, config
	@categorized_defaults ||= {}
	@categorized_defaults[name.to_sym] = {
			description: description,
			config: config
	}

	@config_defaults ||= {}
	@config_defaults[name.to_sym] = {}
	config.each do |k, v|
		@config_defaults[name.to_sym][k.to_sym] = v[:value]
	end
	@loaded = false
end

.add_default(name, description: nil, value: nil) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rbcli/util/config.rb', line 70

def self.add_default name, description: nil, value: nil
	@config_individual_lines ||= []
	text = "#{name.to_s}: #{value}".ljust(30) + " # #{description}"
	@config_individual_lines.push text unless @config_individual_lines.include? text
	@config_defaults[name.to_sym] = value
	@loaded = false
end

.add_defaults(filename = nil, text: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rbcli/util/config.rb', line 78

def self.add_defaults filename=nil, text: nil
	filename = File.expand_path filename
	return unless filename and File.exists? filename
	@config_text ||= ''
	@config_text += "\n" unless @config_text.empty?
	File.readlines(filename).each do |line|
		if (line.start_with? '---' or line.start_with? '...')
			@config_text << "\n\n"
		else
			@config_text << line unless @config_text.include? line
		end
	end if filename and File.exists? filename
	@config_text << "\n\n" << text if text

	@config_defaults ||= {}
	@config_defaults.deep_merge! YAML::load(@config_text).deep_symbolize!
	@loaded = false
end

.configObject



111
112
113
114
# File 'lib/rbcli/util/config.rb', line 111

def self.config
	self.load unless @loaded
	(@config.nil?) ? @config_defaults : @config
end

.generate_userconf(filename) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rbcli/util/config.rb', line 116

def self.generate_userconf filename
	filepath = File.expand_path "#{(filename) ? filename : "#{Dir.pwd}/config.yml"}"
	FileUtils.touch filepath
	File.write filepath, @config_text
	File.open(filepath, 'a') do |f|
		f.puts "# Individual Settings"
		@config_individual_lines.each { |l| f.puts l }
	end if @config_individual_lines


	if @categorized_defaults
		text = ''
		@categorized_defaults.each do |name, opts|
			text += "\n# #{opts[:description]}\n"
			text += "#{name.to_s}:\n"
			opts[:config].each do |opt, v|
				text += "  #{opt.to_s}: #{(v[:value].nil?) ? '~' : v[:value]}".ljust(30) + " # #{v[:description]}\n"
			end
		end
		File.open(filepath, 'a') do |f|
			f.puts text
		end
	end
end

.loadObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rbcli/util/config.rb', line 97

def self.load
	if (! @config_file.nil?) and File.exists? @config_file
		@config = YAML::load(File.read(@config_file)).deep_symbolize!
		@config.deep_merge! @config_defaults if @merge_defaults
	elsif @userfile_required
		puts "User's config file not found at #{@config_file}. Please run this tool with the -g option to generate it."
		exit 1
	else
		@config = @config_defaults
	end
	@loaded = true
	@config
end

.set_userfile(filename, merge_defaults: false, required: false) ⇒ Object



48
49
50
51
52
53
# File 'lib/rbcli/util/config.rb', line 48

def self.set_userfile filename, merge_defaults: false, required: false
	@config_file = File.expand_path filename
	@merge_defaults = merge_defaults
	@userfile_required = required
	@loaded = false
end