Class: RVGP::Utilities::Yaml

Inherits:
Object
  • Object
show all
Defined in:
lib/rvgp/utilities/yaml.rb

Overview

This class wraps the Psych library, and adds functionality we need, to parse yaml files. We mostly added this class because the Psych.add_builtin_type wasn’t able to contain state outside it’s return values. (which we need, in order to track include dependencies)

Defined Under Namespace

Classes: PsychInclude, PsychProc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, include_paths = nil) ⇒ Yaml

Returns a new instance of Yaml.

Parameters:

  • path (String)

    The full path to the yaml file you wish to parse

  • include_paths (Array<String>) (defaults to: nil)

    An array of directories, to search, when locating the target of a “!!include” line



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rvgp/utilities/yaml.rb', line 87

def initialize(path, include_paths = nil)
  @path = path
  @dependencies = []
  @include_paths = Array(include_paths || File.expand_path(File.dirname(path)))

  vanilla_yaml = Psych.safe_load_file(path, symbolize_names: true, permitted_classes: [Date, Symbol])
  @yaml = replace_each_in_yaml(vanilla_yaml, PsychInclude) do |psych_inc|
    @dependencies << psych_inc.path
    psych_inc.contents @include_paths
  end
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



82
83
84
# File 'lib/rvgp/utilities/yaml.rb', line 82

def dependencies
  @dependencies
end

#include_pathsObject (readonly)

Returns the value of attribute include_paths.



82
83
84
# File 'lib/rvgp/utilities/yaml.rb', line 82

def include_paths
  @include_paths
end

#pathObject (readonly)

Returns the value of attribute path.



82
83
84
# File 'lib/rvgp/utilities/yaml.rb', line 82

def path
  @path
end

Instance Method Details

#[](attr) ⇒ Object

Return the specified attribute, in this yaml file

Parameters:

  • attr (String)

    The attribute you’re looking for

Returns:

  • (Object)

    The value of the the provided attribute



102
103
104
# File 'lib/rvgp/utilities/yaml.rb', line 102

def [](attr)
  @yaml&.[](attr)
end

#key?(attr) ⇒ TrueClass, FalseClass Also known as: has_key?

Returns true or false, depending on whether the attribute you’re looking for, exists in this yaml file.

Parameters:

  • attr (String)

    The attribute you’re looking for

Returns:

  • (TrueClass, FalseClass)

    Whether the key is defined in this file



110
111
112
# File 'lib/rvgp/utilities/yaml.rb', line 110

def key?(attr)
  @yaml&.key? attr
end