Class: Wasko::Configuration
- Inherits:
-
Object
- Object
- Wasko::Configuration
- Defined in:
- lib/wasko/configuration.rb
Overview
This class will handle all things considering loading and saving configuration. It should work like this.
* User sets all things to his liking
* User writes to a `.color` file in `~/.wasko`
* User can share/edit this `.color` file
* User can load `.color` file to restore settings
Class Method Summary collapse
-
.all_themes ⇒ Object
All possible ‘.color` themes.
-
.color_theme_from_hash(name) ⇒ Object
Setup the color theme with the hash.
-
.color_theme_to_hash ⇒ Object
Transform a color theme to a hash.
-
.config_file_with_name(name) ⇒ Object
File path of the color theme file.
- .configuration_help ⇒ Object
-
.load_colors!(name) ⇒ Object
Draw the saved colors.
-
.save_colors!(name) ⇒ Object
Write out the colors.
-
.valid_name?(name) ⇒ Boolean
Blatantly stolen from [here](stackoverflow.com/questions/1032104/regex-for-finding-valid-filename) Spots all obvious bad filenames.
-
.wasko_directory ⇒ Object
All config files are stored in ‘~/.wasko/`.
Class Method Details
.all_themes ⇒ Object
All possible ‘.color` themes
26 27 28 29 30 31 32 |
# File 'lib/wasko/configuration.rb', line 26 def all_themes Dir.chdir(wasko_directory) do |path| Dir["*.color"].map do |filename| filename.gsub(/\.color$/, "") end end end |
.color_theme_from_hash(name) ⇒ Object
Setup the color theme with the hash.
46 47 48 49 50 |
# File 'lib/wasko/configuration.rb', line 46 def color_theme_from_hash(name) return {} unless all_themes.include?(name) return {} unless Hash === YAML::load(File.open(config_file_with_name(name))) YAML::load(File.open(config_file_with_name(name))) end |
.color_theme_to_hash ⇒ Object
Transform a color theme to a hash
53 54 55 56 57 58 |
# File 'lib/wasko/configuration.rb', line 53 def color_theme_to_hash %w(background_color foreground_color bold_color cursor_color font_size font_name).inject({}) do |hash, value| hash[value] = Wasko.send(value) hash end end |
.config_file_with_name(name) ⇒ Object
File path of the color theme file
41 42 43 |
# File 'lib/wasko/configuration.rb', line 41 def config_file_with_name(name) File.join(wasko_directory, "#{name}.color") end |
.configuration_help ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/wasko/configuration.rb', line 77 def configuration_help <<HELP # This is a theme used by the wasko gem, it's nothing # more than regular yaml. So (ab)use as you would normally # do. The only thing to note is, colors take only valid # css colors and this comment can be deleted. HELP end |
.load_colors!(name) ⇒ Object
Draw the saved colors
63 64 65 66 67 |
# File 'lib/wasko/configuration.rb', line 63 def load_colors!(name) color_theme_from_hash(name).each do |object, color| Wasko.send("set_#{object}", color) end end |
.save_colors!(name) ⇒ Object
Write out the colors
70 71 72 73 74 75 |
# File 'lib/wasko/configuration.rb', line 70 def save_colors!(name) File.open(config_file_with_name(name), 'w') do |out| out.write(configuration_help) out.write(color_theme_to_hash.to_yaml) end end |
.valid_name?(name) ⇒ Boolean
Blatantly stolen from [here](stackoverflow.com/questions/1032104/regex-for-finding-valid-filename) Spots all obvious bad filenames
36 37 38 |
# File 'lib/wasko/configuration.rb', line 36 def valid_name?(name) name =~ /^[^\/?*:;{}\\]+$/ end |
.wasko_directory ⇒ Object
All config files are stored in ‘~/.wasko/`
17 18 19 20 21 22 23 |
# File 'lib/wasko/configuration.rb', line 17 def wasko_directory wasko_path = File.join(ENV['HOME'], ".wasko") unless File.exists?(wasko_path) && File.directory?(wasko_path) FileUtils.mkdir_p File.join(ENV['HOME'], ".wasko") end wasko_path end |