Class: Budik::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/budik/config.rb

Overview

‘Config’ class loads and manages app configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

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.expand_path(@options['sources']['path']))
  @lang = init_lang
end

Instance Attribute Details

#langObject

Language strings, options and sources.



38
39
40
# File 'lib/budik/config.rb', line 38

def lang
  @lang
end

#optionsObject

Language strings, options and sources.



38
39
40
# File 'lib/budik/config.rb', line 38

def options
  @options
end

#sourcesObject

Language strings, options and sources.



38
39
40
# File 'lib/budik/config.rb', line 38

def sources
  @sources
end

Instance Method Details

#editObject

Opens options file for editing.



41
42
43
44
# File 'lib/budik/config.rb', line 41

def edit
  options_path = File.expand_path('~/.budik/options.yml')
  open_file(options_path)
end

#init_langObject

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

#installObject

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/'])

  install_options(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 install_options(dir)
  options = @templates_dir + 'options/' + platform?.to_s + '.yml'
  FileUtils.cp options, 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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/budik/config.rb', line 110

def platform?
  os = Sys::Platform.linux? ? :linux : :windows
  rpi?(os) ? :rpi : os
end

#resetObject

Resets app’s configuration.



116
117
118
119
# File 'lib/budik/config.rb', line 116

def reset
  options = @templates_dir + 'options/' + platform?.to_s + '.yml'
  FileUtils.cp(options, 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

Returns:

  • (Boolean)


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