Class: Hatemile::Util::Configure

Inherits:
Object
  • Object
show all
Defined in:
lib/hatemile/util/configure.rb

Overview

The Configure class contains the configuration of HaTeMiLe.

Instance Method Summary collapse

Constructor Details

#initialize(files_name = nil, locales = [:'en-US']) ⇒ Configure

Initializes a new object that contains the configuration of HaTeMiLe.

Parameters:

  • files_name (Array<String>) (defaults to: nil)

    The path of files.

  • locales (Array<Symbol>) (defaults to: [:'en-US'])

    The locales.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hatemile/util/configure.rb', line 30

def initialize(files_name = nil, locales = [:'en-US'])
  Hatemile::Helper.require_valid_type(files_name, Array)
  Hatemile::Helper.require_valid_type(files_name, Array)

  if files_name.nil?
    pattern = File.join(
      File.dirname(File.dirname(File.dirname(__FILE__))),
      'locale',
      '*.yml'
    )
    files_name = Dir.glob(pattern)
  end
  @locales = locales
  @parameters = {}
  files_name.each do |file_name|
    @parameters = @parameters.merge(YAML.load_file(file_name))
  end
end

Instance Method Details

#get_parameter(parameter) ⇒ String

Returns the value of a parameter of configuration.

Parameters:

  • parameter (String)

    The parameter.

Returns:

  • (String)

    The value of the parameter.



81
82
83
84
85
86
87
88
89
# File 'lib/hatemile/util/configure.rb', line 81

def get_parameter(parameter)
  @locales.each do |locale|
    next if @locales.last == locale

    value = @parameters[locale.to_s]['hatemile'].fetch(parameter, nil)
    return value unless value.nil?
  end
  @parameters[@locales.last.to_s]['hatemile'].fetch(parameter)
end

#get_parametersHash

Returns the parameters of configuration.

Returns:

  • (Hash)

    The parameters of configuration.



53
54
55
56
57
58
59
60
61
# File 'lib/hatemile/util/configure.rb', line 53

def get_parameters
  clone_parameters = {}
  @locales.each do |locale|
    clone_parameters = @parameters[locale.to_s]['hatemile'].merge(
      clone_parameters
    )
  end
  clone_parameters
end

#has_parameter?(parameter) ⇒ Boolean

Check that the configuration has an parameter.

Parameters:

  • parameter (String)

    The parameter.

Returns:

  • (Boolean)

    True if the configuration has the parameter or false if the configuration not has the parameter.



69
70
71
72
73
74
# File 'lib/hatemile/util/configure.rb', line 69

def has_parameter?(parameter)
  @locales.each do |locale|
    return true if @parameters[locale.to_s]['hatemile'].key?(parameter)
  end
  false
end