Module: Kerbi::Utils::Values

Defined in:
lib/utils/values.rb

Overview

Utilities module for all value loading functionality.

Defined Under Namespace

Classes: ErbWrapper

Class Method Summary collapse

Class Method Details

.from_files(fname_exprs, **opts) ⇒ Object



7
8
9
10
# File 'lib/utils/values.rb', line 7

def self.from_files(fname_exprs, **opts)
  final_paths = resolve_fname_exprs(fname_exprs, **opts)
  load_yaml_files(final_paths)
end

.from_inlines(inline_exprs) ⇒ Object

Parses and merges cli-level key-value assignments of the form foo.bar=baz.

Parameters:

  • inline_exprs (Array<String>)

    e.g %w[foo=bar, foo.bar=baz]



56
57
58
59
60
61
# File 'lib/utils/values.rb', line 56

def self.from_inlines(inline_exprs)
  inline_exprs.inject({}) do |whole, str_assignment|
    assignment = self.parse_inline_assignment(str_assignment)
    whole.deep_merge(assignment)
  end
end

.load_yaml_file(good_fname) ⇒ Hash

Loads and performs all interpolation operations on file, returns corresponding symbol hash of values. File is expected to contain one root element. noinspection RubyResolve

Parameters:

  • good_fname (String)

    path of values file

Returns:

  • (Hash)

    corresponding value hash



83
84
85
86
87
# File 'lib/utils/values.rb', line 83

def self.load_yaml_file(good_fname)
  file_contents = File.read(good_fname)
  interpolated = ErbWrapper.new.interpolate(file_contents)
  YAML.load(interpolated).deep_symbolize_keys
end

.load_yaml_files(final_file_paths) ⇒ Object

Loads the dicts from files pointed to by final_file_paths into memory, and returns the deep-merged hash in the order of the files. of value assignment files

Parameters:

  • final_file_paths (Array<String>)

    absolute filenames



45
46
47
48
49
50
# File 'lib/utils/values.rb', line 45

def self.load_yaml_files(final_file_paths)
  final_file_paths.inject({}) do |whole, fname|
    file_values = self.load_yaml_file(fname)
    whole.deep_merge(file_values)
  end
end

.parse_inline_assignment(str_assign) ⇒ Hash

Parses a single cli-level key-value assignment with the form foo.bar=baz. Raises an exception if the expression is malformed.

Parameters:

  • str_assign (String)

    e.g foo=bar

Returns:

  • (Hash)

    corresponding symbol hash e.g bar



68
69
70
71
72
73
74
# File 'lib/utils/values.rb', line 68

def self.parse_inline_assignment(str_assign)
  deep_key, value = str_assign.split("=")
  raise "malformed assignment #{str_assign}" unless deep_key && value
  assign_parts = deep_key.split(".") << value
  assignment = assign_parts.reverse.inject{ |a, n| { n => a } }
  assignment.deep_symbolize_keys
end

.raise_if_file_not_found(fname_expr, path, **opts) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/utils/values.rb', line 31

def self.raise_if_file_not_found(fname_expr, path, **opts)
  if fname_expr != 'values' && !path
    raise Kerbi::ValuesFileNotFoundError.new(
      fname_expr: fname_expr,
      root: opts[:root]
    )
  end
end

.resolve_fname_exprs(fname_exprs, **opts) ⇒ Array<String>

Resolves each filename expression given, returning an array of absolute paths. Automatically prepends the default values filename

  • values.yaml - to the list and does not complain if it does not exist.

If two or more filename expressions resolve to the same absolute path, only one copy will be in the list.

Parameters:

  • fname_exprs (Array<String>)

    cli-level values file path names

  • opts (Hash)

    downstream options for file-loading methods

Returns:

  • (Array<String>)

    list of unique absolute filenames



21
22
23
24
25
26
27
28
29
# File 'lib/utils/values.rb', line 21

def self.resolve_fname_exprs(fname_exprs, **opts)
  final_exprs = fname_exprs.uniq
  final_exprs.map do |fname_expr|
    candidate_paths = values_paths(fname_expr, **opts)
    path = Kerbi::Utils::Misc.real_files_for(*candidate_paths)[0]
    raise_if_file_not_found(fname_expr, path, **opts)
    path.presence
  end.compact.uniq
end

.values_paths(fname, root: nil) ⇒ Array<String>

noinspection RubyLiteralArrayInspection

Returns all possible paths that a values filename might resolve to according to kerbi conventions. Does not check for file existence. The paths are ordered by similarity to the input expression, starting obviously with the input itself.

Parameters:

  • fname (Object)

    cli-level filename expression for a values file

  • root (String) (defaults to: nil)

    optionally pass in project/mixer root dir

Returns:

  • (Array<String>)

    all possible paths



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/utils/values.rb', line 98

def self.values_paths(fname, root: nil)
  if root.nil?
    root = ''
  else
    root = "#{root}/" unless root.end_with?("/")
  end
  [
    "#{root}#{fname}",
    "#{root}#{fname}.yaml",
    "#{root}#{fname}.json",
    "#{root}#{fname}.yaml.erb",
    "#{root}values/#{fname}",
    "#{root}values/#{fname}.yaml.erb",
    "#{root}values/#{fname}.yaml",
    "#{root}values/#{fname}.json"
  ]
end