Class: Translator::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/translator/config_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ ConfigFile

Returns a new instance of ConfigFile.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
# File 'lib/translator/config_file.rb', line 5

def initialize(from, to)
  from = 'auto' if from.empty?
  raise ArgumentError, 'Precisa passar qual idioma deseja traduzir' if to.empty?

  @from = from.downcase
  @to = to.downcase
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



3
4
5
# File 'lib/translator/config_file.rb', line 3

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



3
4
5
# File 'lib/translator/config_file.rb', line 3

def to
  @to
end

Class Method Details

.default_fromObject



38
39
40
# File 'lib/translator/config_file.rb', line 38

def default_from
  get_default_config[:from]
end

.default_toObject



34
35
36
# File 'lib/translator/config_file.rb', line 34

def default_to
  get_default_config[:to]
end

.exist?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/translator/config_file.rb', line 42

def exist?
  File.exist?("#{Dir.home}/.translator")
end

.get_default_configObject



25
26
27
28
29
30
31
32
# File 'lib/translator/config_file.rb', line 25

def get_default_config
  config = []
  File.open("#{Dir.home}/.translator").each_line do |l|
    config << l.delete("\n")
  end

  { from: config[0], to: config[1] }
end

Instance Method Details

#config_file_pathObject



13
14
15
# File 'lib/translator/config_file.rb', line 13

def config_file_path
  "#{Dir.home}/.translator"
end

#save_configObject



17
18
19
20
21
22
# File 'lib/translator/config_file.rb', line 17

def save_config
  File.open(config_file_path, 'w') do |f|
    f.write("#{from}\n#{to}\n")
    f.close
  end
end