Class: Longleaf::ApplicationConfigDeserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/services/application_config_deserializer.rb

Overview

Deserializer for application configuration files

Class Method Summary collapse

Class Method Details

.absolution(base_pathname, file_path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/longleaf/services/application_config_deserializer.rb', line 66

def self.absolution(base_pathname, file_path)
  if file_path.nil?
    nil
  else
    path = Pathname.new(file_path)
    if path.absolute?
      path = path.expand_path.to_s
    else
      path = (base_pathname + path).to_s
    end
  end
end

.deserialize(config_path, format: 'yaml') ⇒ Object

Deserializes a valid application configuration file as a ApplicationConfigManager option return [ApplicationConfigManager] manager for the loaded configuration

Parameters:

  • config_path (String)

    file path to the service and storage mapping configuration file

  • format (String)

    encoding format of the config file



15
16
17
18
19
20
21
22
23
24
# File 'lib/longleaf/services/application_config_deserializer.rb', line 15

def self.deserialize(config_path, format: 'yaml')
  content = load_config_file(config_path)
  config = load(content, format)

  config_md5 = Digest::MD5.hexdigest(content)

  make_paths_absolute(config_path, config)
  Longleaf::ApplicationConfigValidator.validate(config)
  Longleaf::ApplicationConfigManager.new(config, config_md5)
end

.from_yaml(content) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/longleaf/services/application_config_deserializer.rb', line 48

def self.from_yaml(content)
  begin
    YAML.safe_load(content, [], [], true)
  rescue => err
    raise Longleaf::ConfigurationError.new(err)
  end
end

.load(content, format) ⇒ Object

Deserialize a configuration file into a hash return [Hash] hash containing the configuration

Parameters:

  • content (String)

    the contents of the application configuration file

  • format (String)

    encoding format of the config file



39
40
41
42
43
44
45
46
# File 'lib/longleaf/services/application_config_deserializer.rb', line 39

def self.load(content, format)
  case format
  when 'yaml'
    from_yaml(content)
  else
    raise ArgumentError.new("Invalid deserialization format #{format} specified")
  end
end

.load_config_file(config_path) ⇒ Object (private)



26
27
28
29
30
31
32
33
# File 'lib/longleaf/services/application_config_deserializer.rb', line 26

def self.load_config_file(config_path)
  begin
    File.read(config_path)
  rescue Errno::ENOENT
    raise Longleaf::ConfigurationError.new(
        "Configuration file #{config_path} does not exist.")
  end
end

.make_paths_absolute(config_path, config) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/longleaf/services/application_config_deserializer.rb', line 56

def self.make_paths_absolute(config_path, config)
  base_pathname = Pathname.new(config_path).expand_path.parent

  config[AF::LOCATIONS].each do |name, properties|
    properties[AF::LOCATION_PATH] = absolution(base_pathname, properties[AF::LOCATION_PATH])

    properties[AF::METADATA_PATH] = absolution(base_pathname, properties[AF::METADATA_PATH])
  end
end