Class: Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/helpful_utils/common/configuration.rb

Constant Summary collapse

@@path_to_config_dir =
"#{::RAILS_ROOT}/config/project"

Class Method Summary collapse

Class Method Details

.has?(config_name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/helpful_utils/common/configuration.rb', line 19

def self.has?(config_name)
  self.method_defined? config_name
end

.load_configuration_from_file(config_file) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/helpful_utils/common/configuration.rb', line 24

def self.load_configuration_from_file ( config_file )
  config = YAML.load_file( config_file )    
  if config.instance_of? Hash
    attrs = []
    config.each_key { |key| attrs << key.to_sym } # извлекаем имена полей
    config_struct = Struct.new *attrs             # создаем структуру с этими полями
    new_config = config_struct.new                # создаем объект этой структуры
    config_struct.members.each do |attr_name|     # инициализируем значения полей объекта
      new_config.send( "#{attr_name}=", config[attr_name] )
    end
    config = new_config                           # отображаем трансформацию в конфиг
  end
  attr_name = File.basename( config_file, ".yaml" )

  self.class_eval do
    cattr_accessor attr_name.to_sym               # создаем в модуле аттрибут
  end

  self.send("#{attr_name}=", config)
end

.load_configurationsObject



10
11
12
13
14
15
16
17
# File 'lib/helpful_utils/common/configuration.rb', line 10

def self.load_configurations
  if defined? ::RAILS_ROOT
    Dir["#{path_to_config_dir}/*.yaml"].each do |config_file|
      load_configuration_from_file(config_file)
    end
  end

end