Class: CFG::Config

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/CFG/config.rb

Constant Summary collapse

MISSING =
Object.new

Constants included from Utils

Utils::COLON_OBJECT_PATTERN, Utils::ENV_VALUE_PATTERN, Utils::INTERPOLATION_PATTERN, Utils::ISO_DATETIME_PATTERN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#alnum?, #default_string_converter, #digit?, #hexdigit?, #letter?, #white_space?

Constructor Details

#initialize(path_or_stream = nil) ⇒ Config

Returns a new instance of Config.



1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
# File 'lib/CFG/config.rb', line 1937

def initialize(path_or_stream = nil)
  @no_duplicates = true
  @strict_conversions = true
  @context = {}
  @include_path = []
  @path = nil
  @root_dir = nil
  @evaluator = Evaluator.new self
  @string_converter = method :default_string_converter

  @cache = nil
  @data = nil
  @parent = nil

  return if path_or_stream.nil?

  if path_or_stream.is_a? String
    load_file path_or_stream
  else
    load path_or_stream
  end
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



1928
1929
1930
# File 'lib/CFG/config.rb', line 1928

def context
  @context
end

#dataObject

Returns the value of attribute data.



1934
1935
1936
# File 'lib/CFG/config.rb', line 1934

def data
  @data
end

#evaluatorObject

Returns the value of attribute evaluator.



1935
1936
1937
# File 'lib/CFG/config.rb', line 1935

def evaluator
  @evaluator
end

#include_pathObject

Returns the value of attribute include_path.



1929
1930
1931
# File 'lib/CFG/config.rb', line 1929

def include_path
  @include_path
end

#no_duplicatesObject

Returns the value of attribute no_duplicates.



1926
1927
1928
# File 'lib/CFG/config.rb', line 1926

def no_duplicates
  @no_duplicates
end

#parentObject

Returns the value of attribute parent.



1933
1934
1935
# File 'lib/CFG/config.rb', line 1933

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



1930
1931
1932
# File 'lib/CFG/config.rb', line 1930

def path
  @path
end

#root_dirObject

Returns the value of attribute root_dir.



1931
1932
1933
# File 'lib/CFG/config.rb', line 1931

def root_dir
  @root_dir
end

#strict_conversionsObject

Returns the value of attribute strict_conversions.



1927
1928
1929
# File 'lib/CFG/config.rb', line 1927

def strict_conversions
  @strict_conversions
end

#string_converterObject

Returns the value of attribute string_converter.



1932
1933
1934
# File 'lib/CFG/config.rb', line 1932

def string_converter
  @string_converter
end

Class Method Details

.identifier?(str) ⇒ Boolean

Returns:

  • (Boolean)


1972
1973
1974
# File 'lib/CFG/config.rb', line 1972

def self.identifier?(str)
  !/^[\p{L}_]([\p{L}\p{Nd}_]*)$/u.match(str).nil?
end

Instance Method Details

#[](key) ⇒ Object



2082
2083
2084
# File 'lib/CFG/config.rb', line 2082

def [](key)
  get key
end

#as_dictObject



2086
2087
2088
# File 'lib/CFG/config.rb', line 2086

def as_dict
  @data.nil? ? {} : @data.as_dict
end

#base_get(key, default = MISSING) ⇒ Object



2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
# File 'lib/CFG/config.rb', line 2045

def base_get(key, default = MISSING)
  if @cache&.include?(key)
    result = @cache[key]
  elsif @data.nil?
    raise ConfigError, 'No data in configuration'
  else
    if @data.include? key
      result = evaluated @data[key]
    elsif Config.identifier? key
      raise ConfigError, "Not found in configuration: #{key}" if default.equal?(MISSING)

      result = default
    else
      # not an identifier. Treat as a path
      begin
        result = get_from_path key
      rescue InvalidPathError, BadIndexError, CircularReferenceError
        raise
      rescue ConfigError
        if default.equal? MISSING
          #e = ConfigError.new "Not found in configuration: #{key}"
          #raise e
          raise
        end
        result = default
      end
    end
    # if user specified a cache, populate it
    @cache[key] = result unless @cache.nil?
  end
  result
end

#cachedObject



1960
1961
1962
# File 'lib/CFG/config.rb', line 1960

def cached
  !@cache.nil?
end

#cached=(cache) ⇒ Object



1964
1965
1966
1967
1968
1969
1970
# File 'lib/CFG/config.rb', line 1964

def cached=(cache)
  if !cache
    @cache = nil
  elsif @cache.nil?
    @cache = {}
  end
end

#convert_string(str) ⇒ Object

Raises:



2027
2028
2029
2030
2031
2032
# File 'lib/CFG/config.rb', line 2027

def convert_string(str)
  result = @string_converter.call str, self
  raise ConfigError, "Unable to convert string #{str}" if strict_conversions && result.equal?(str)

  result
end

#evaluated(value, evaluator = nil) ⇒ Object



2034
2035
2036
2037
2038
2039
2040
2041
# File 'lib/CFG/config.rb', line 2034

def evaluated(value, evaluator = nil)
  result = value
  if value.is_a? ASTNode
    e = evaluator.nil? ? @evaluator : evaluator
    result = e.evaluate value
  end
  result
end

#get(key, default = MISSING) ⇒ Object



2078
2079
2080
# File 'lib/CFG/config.rb', line 2078

def get(key, default = MISSING)
  unwrap base_get(key, default)
end

#get_from_path(path) ⇒ Object



2022
2023
2024
2025
# File 'lib/CFG/config.rb', line 2022

def get_from_path(path)
  @evaluator.refs_seen.clear
  @evaluator.get_from_path parse_path(path)
end

#load(stream) ⇒ Object



2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
# File 'lib/CFG/config.rb', line 2008

def load(stream)
  parser = Parser.new stream
  node = parser.container
  unless node.is_a? MappingNode
    e = ConfigError.new 'Root configuration must be a mapping'

    e.location = node.start
    raise e
  end
  set_path stream.path if stream.is_a? File
  @data = wrap_mapping node
  @cache&.clear
end

#load_file(path) ⇒ Object



1976
1977
1978
1979
1980
# File 'lib/CFG/config.rb', line 1976

def load_file(path)
  f = File.open path, 'r:utf-8'
  load f
  f.close
end

#set_path(path) ⇒ Object



1982
1983
1984
1985
# File 'lib/CFG/config.rb', line 1982

def set_path(path)
  @path = path
  @root_dir = Pathname.new(path).parent
end

#wrap_list(node) ⇒ Object



2002
2003
2004
2005
2006
# File 'lib/CFG/config.rb', line 2002

def wrap_list(node)
  result = ListWrapper.new self
  result.concat node.elements
  result
end

#wrap_mapping(node) ⇒ Object



1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
# File 'lib/CFG/config.rb', line 1987

def wrap_mapping(node)
  result = DictWrapper.new self
  seen = @no_duplicates ? {} : nil
  node.elements.each do |t, v|
    k = t.value
    unless seen.nil?
      raise ConfigError, "Duplicate key #{k} seen at #{t.start} (previously at #{seen[k]})" if seen.include? k

      seen[k] = t.start
    end
    result[k] = v
  end
  result
end