Class: Hocon::Impl::SimpleConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/hocon/impl/simple_config.rb

Constant Summary collapse

ConfigMissingError =
Hocon::ConfigError::ConfigMissingError
ConfigNotResolvedError =
Hocon::ConfigError::ConfigNotResolvedError
ConfigNullError =
Hocon::ConfigError::ConfigNullError
ConfigWrongTypeError =
Hocon::ConfigError::ConfigWrongTypeError
ConfigValueType =
Hocon::ConfigValueType
Path =
Hocon::Impl::Path
DefaultTransformer =
Hocon::Impl::DefaultTransformer

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ SimpleConfig

Returns a new instance of SimpleConfig.



17
18
19
# File 'lib/hocon/impl/simple_config.rb', line 17

def initialize(object)
  @object = object
end

Instance Method Details

#find(me, path, expected, original_path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hocon/impl/simple_config.rb', line 50

def find(me, path, expected, original_path)
  key = path.first
  rest = path.remainder
  if rest.nil?
    find_key(me, key, expected, original_path)
  else
    o = find_key(me, key, ConfigValueType::OBJECT,
                 original_path.sub_path(0, original_path.length - rest.length))
    raise "Error: object o is nil" unless not o.nil?
    find(o, rest, expected, original_path)
  end
end

#find_key(me, key, expected, original_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hocon/impl/simple_config.rb', line 25

def find_key(me, key, expected, original_path)
  v = me.peek_assuming_resolved(key, original_path)
  if v.nil?
    raise ConfigMissingError.new(nil, "No configuration setting found for key '#{original_path.render}'", nil)
  end

  if not expected.nil?
    v = DefaultTransformer.transform(v, expected)
  end

  if v.value_type == ConfigValueType::NULL
    raise ConfigNullError.new(v.origin,
                              (ConfigNullError.make_message(original_path.render,
                                                            (not expected.nil?) ? expected.name : nil)),
                              nil)
  elsif (not expected.nil?) && v.value_type != expected
    raise ConfigWrongTypeError.new(v.origin,
                                   "#{original_path.render} has type #{v.value_type.name} " +
                                       "rather than #{expected.name}",
                                   nil)
  else
    return v
  end
end

#get_value(path) ⇒ Object



63
64
65
66
# File 'lib/hocon/impl/simple_config.rb', line 63

def get_value(path)
  parsed_path = Path.new_path(path)
  find(@object, parsed_path, nil, parsed_path)
end

#has_path(path_expression) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/hocon/impl/simple_config.rb', line 68

def has_path(path_expression)
  path = Path.new_path(path_expression)
  begin
    peeked = @object.peek_path(path)
  rescue ConfigNotResolvedError => e
    raise Hocon::Impl::ConfigImpl.improve_not_resolved(path, e)
  end
  (not peeked.nil?) && peeked.value_type != ConfigValueType::NULL
end

#rootObject



21
22
23
# File 'lib/hocon/impl/simple_config.rb', line 21

def root
  @object
end

#with_value(path_expression, v) ⇒ Object



83
84
85
86
# File 'lib/hocon/impl/simple_config.rb', line 83

def with_value(path_expression, v)
  path = Path.new_path(path_expression)
  self.class.new(root.with_value(path, v))
end

#without_path(path_expression) ⇒ Object



78
79
80
81
# File 'lib/hocon/impl/simple_config.rb', line 78

def without_path(path_expression)
  path = Path.new_path(path_expression)
  self.class.new(root.without_path(path))
end