Class: AppLocale::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Config

Returns a new instance of Config.



12
13
14
15
16
17
18
19
# File 'lib/applocale/config.rb', line 12

def initialize(config_file)
  @config_file = config_file
  parse_config_file!
  @api_key = env_or_config_value("api_key")
  @project_id = env_or_config_value("project_id")
  @host = env_or_config_value("host")
  @locale_directory = env_or_config_value("locale_directory", default: "config/locales")
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



6
7
8
# File 'lib/applocale/config.rb', line 6

def api_key
  @api_key
end

#config_fileObject

Returns the value of attribute config_file.



6
7
8
# File 'lib/applocale/config.rb', line 6

def config_file
  @config_file
end

#configuration_dataObject

Returns the value of attribute configuration_data.



6
7
8
# File 'lib/applocale/config.rb', line 6

def configuration_data
  @configuration_data
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/applocale/config.rb', line 6

def host
  @host
end

#locale_directoryObject

Returns the value of attribute locale_directory.



6
7
8
# File 'lib/applocale/config.rb', line 6

def locale_directory
  @locale_directory
end

#project_idObject

Returns the value of attribute project_id.



6
7
8
# File 'lib/applocale/config.rb', line 6

def project_id
  @project_id
end

Class Method Details

.load(config_file) ⇒ Object



8
9
10
# File 'lib/applocale/config.rb', line 8

def self.load(config_file)
  new(config_file)
end

Instance Method Details

#parse_config_file!Object



21
22
23
24
25
26
27
# File 'lib/applocale/config.rb', line 21

def parse_config_file!
  validate_config_file!

  file_content = File.read(config_file_path)
  parsed_config_file = ERB.new(file_content).result
  @configuration_data = YAML.safe_load(parsed_config_file).dig("applocale") || {}
end

#validate_config_file!Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/applocale/config.rb', line 29

def validate_config_file!
  unless File.exist?(config_file_path)
    puts "[AppLocale] Error: #{config_file_path} does not exist."
    exit 1
  end

  unless File.readable?(config_file_path)
    puts "[AppLocale] Error: #{config_file_path} is not readable by AppLocale."
    exit 1
  end
end