Module: Hocon

Defined in:
lib/hocon.rb,
lib/hocon/version.rb

Defined Under Namespace

Modules: CLI, ConfigIncludeContext, ConfigIncluderFile, ConfigList, ConfigMergeable, ConfigObject, ConfigParseable, ConfigSyntax, ConfigValue, ConfigValueType, Impl, Parser, Version Classes: ConfigError, ConfigFactory, ConfigParseOptions, ConfigRenderOptions, ConfigResolveOptions, ConfigUtil, ConfigValueFactory

Class Method Summary collapse

Class Method Details

.load(file, opts = nil) ⇒ Object

NOTE: the behavior of this load method differs a bit from the upstream public API, where a file extension may be the preferred method of determining the config syntax, even if you specify a Syntax value on ConfigParseOptions. Here we prefer the syntax (optionally) specified by the user no matter what the file extension is, and if they don’t specify one and the file extension is unrecognized, we raise an error.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hocon.rb', line 10

def self.load(file, opts = nil)
  # doing these requires lazily, because otherwise, classes that need to
  # `require_relative 'hocon'` to get the module into scope will end up recursing
  # through this require and probably ending up with circular dependencies.
  require_relative 'hocon/config_factory'
  require_relative 'hocon/impl/parseable'
  require_relative 'hocon/config_parse_options'
  require_relative 'hocon/config_resolve_options'
  require_relative 'hocon/config_error'
  syntax = opts ? opts[:syntax] : nil

  if syntax.nil?
    unless Hocon::Impl::Parseable.syntax_from_extension(file)
      raise Hocon::ConfigError::ConfigParseError.new(
          nil, "Unrecognized file extension '#{File.extname(file)}' and no value provided for :syntax option", nil)
    end
    config = Hocon::ConfigFactory.parse_file_any_syntax(
        file, Hocon::ConfigParseOptions.defaults)
  else
    config = Hocon::ConfigFactory.parse_file(
        file, Hocon::ConfigParseOptions.defaults.set_syntax(syntax))
  end

  resolved_config = Hocon::ConfigFactory.load_from_config(
      config, Hocon::ConfigResolveOptions.defaults)

  resolved_config.root.unwrapped
end

.parse(string) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hocon.rb', line 39

def self.parse(string)
  # doing these requires lazily, because otherwise, classes that need to
  # `require_relative 'hocon'` to get the module into scope will end up recursing
  # through this require and probably ending up with circular dependencies.
  require_relative 'hocon/config_factory'
  require_relative 'hocon/config_resolve_options'
  config = Hocon::ConfigFactory.parse_string(string)
  resolved_config = Hocon::ConfigFactory.load_from_config(
      config, Hocon::ConfigResolveOptions.defaults)

  resolved_config.root.unwrapped
end