Class: Budik::Config
Overview
‘Config’ class loads and manages app configuration.
Instance Attribute Summary collapse
-
#lang ⇒ Object
Language strings, options and sources.
-
#options ⇒ Object
Language strings, options and sources.
-
#sources ⇒ Object
Language strings, options and sources.
Instance Method Summary collapse
-
#edit ⇒ Object
Opens options file for editing.
-
#init_lang ⇒ Object
Sets application’s language.
-
#initialize ⇒ Config
constructor
Installs the application if not installed.
-
#install ⇒ Object
Installs the application.
-
#install_lang(dir) ⇒ Object
Creates default language file from template.
-
#install_options(dir) ⇒ Object
Creates options file from template.
-
#install_sources(dir) ⇒ Object
Creates sources file from template.
-
#installed? ⇒ Boolean
Checks if the application is already installed.
-
#open_file(file) ⇒ Object
Opens file in default editor depending on platform.
-
#platform? ⇒ Boolean
Returns current platform application’s running on.
-
#reset ⇒ Object
Resets app’s configuration.
-
#rpi?(os) ⇒ Boolean
Checks if application is running on Raspberry Pi.
-
#translate(lang) ⇒ Object
Creates and/or opens language file for translation.
Constructor Details
#initialize ⇒ Config
Installs the application if not installed. Loads options, sources and language.
17 18 19 20 21 22 23 24 |
# File 'lib/budik/config.rb', line 17 def initialize @templates_dir = File.dirname(__FILE__) + '/../../config/templates/' install unless installed? @options = YAML.load_file(Dir.home + '/.budik/options.yml') @sources = YAML.load_file(File.(@options['sources']['path'])) @lang = init_lang end |
Instance Attribute Details
#lang ⇒ Object
Language strings, options and sources.
38 39 40 |
# File 'lib/budik/config.rb', line 38 def lang @lang end |
#options ⇒ Object
Language strings, options and sources.
38 39 40 |
# File 'lib/budik/config.rb', line 38 def @options end |
#sources ⇒ Object
Language strings, options and sources.
38 39 40 |
# File 'lib/budik/config.rb', line 38 def sources @sources end |
Instance Method Details
#edit ⇒ Object
Opens options file for editing.
41 42 43 44 |
# File 'lib/budik/config.rb', line 41 def edit = File.('~/.budik/options.yml') open_file() end |
#init_lang ⇒ Object
Sets application’s language.
-
Returns:
-
R18n::Translation object.
-
31 32 33 34 35 |
# File 'lib/budik/config.rb', line 31 def init_lang R18n.default_places = Dir.home + '/.budik/lang/' R18n.set(@options['lang']) R18n.t end |
#install ⇒ Object
Installs the application.
47 48 49 50 51 52 53 54 |
# File 'lib/budik/config.rb', line 47 def install dir = Dir.home + '/.budik/' FileUtils.mkdir_p([dir, dir + 'lang/', dir + 'downloads/']) (dir) install_sources(dir) install_lang(dir) end |
#install_lang(dir) ⇒ Object
Creates default language file from template.
-
Args:
-
dir
-> Directory containing app’s configuration (String).
-
81 82 83 84 |
# File 'lib/budik/config.rb', line 81 def install_lang(dir) lang = @templates_dir + 'lang/en.yml' FileUtils.cp lang, dir + 'lang/' end |
#install_options(dir) ⇒ Object
Creates options file from template.
-
Args:
-
dir
-> Directory containing app’s configuration (String).
-
61 62 63 64 |
# File 'lib/budik/config.rb', line 61 def (dir) = @templates_dir + 'options/' + platform?.to_s + '.yml' FileUtils.cp , dir + 'options.yml' end |
#install_sources(dir) ⇒ Object
Creates sources file from template.
-
Args:
-
dir
-> Directory containing app’s configuration (String).
-
71 72 73 74 |
# File 'lib/budik/config.rb', line 71 def install_sources(dir) sources = @templates_dir + 'sources/sources.yml' FileUtils.cp sources, dir unless File.file? dir + sources end |
#installed? ⇒ Boolean
Checks if the application is already installed.
87 88 89 |
# File 'lib/budik/config.rb', line 87 def installed? File.file?(Dir.home + '/.budik/options.yml') end |
#open_file(file) ⇒ Object
Opens file in default editor depending on platform.
-
Args:
-
file
-> File to open (String).
-
96 97 98 99 100 101 102 103 |
# File 'lib/budik/config.rb', line 96 def open_file(file) if @options['os'] == 'windows' system('@powershell -Command "' + file + '"') else editor = ENV['EDITOR'] ? ENV['EDITOR'] : 'vi' system(editor + ' "' + file + '"') end end |
#platform? ⇒ Boolean
Returns current platform application’s running on.
-
Returns:
-
:windows, :linux or :rpi
-
110 111 112 113 |
# File 'lib/budik/config.rb', line 110 def platform? os = Sys::Platform.linux? ? :linux : :windows rpi?(os) ? :rpi : os end |
#reset ⇒ Object
Resets app’s configuration.
116 117 118 119 |
# File 'lib/budik/config.rb', line 116 def reset = @templates_dir + 'options/' + platform?.to_s + '.yml' FileUtils.cp(, Dir.home + '/.budik/options.yml') end |
#rpi?(os) ⇒ Boolean
Checks if application is running on Raspberry Pi.
-
Args:
-
os
-> Operating system (:windows or :linux)
-
-
Returns:
-
true or false
-
128 129 130 131 132 133 134 135 |
# File 'lib/budik/config.rb', line 128 def rpi?(os) return false unless os == :linux cpuinfo = File.read('/proc/cpuinfo') hardware = cpuinfo.scan(/[hH]ardware\s*:\s*(\w+)/).first.first hardware =~ /BCM270[89]/ rescue false end |
#translate(lang) ⇒ Object
Creates and/or opens language file for translation.
-
Args:
-
lang
-> Language code (String)
-
142 143 144 145 146 147 |
# File 'lib/budik/config.rb', line 142 def translate(lang) template = @templates_dir + 'lang/en.yml' new_lang = Dir.home + '/.budik/lang/' + lang + '.yml' FileUtils.cp template, new_lang open_file(new_lang) end |