Module: Occi::Core::Helpers::YamlSummoner

Included in:
AttributeDefinition, Category
Defined in:
lib/occi/core/helpers/yaml_summoner.rb

Overview

Introduces YAML parsing capabilities to every receiver class.

Author:

Instance Method Summary collapse

Instance Method Details

#from_yaml(path, model = nil, attribute_definitions = nil) ⇒ Object

Reads the provided YAML file and passes its content as `args` to the constructor of the class. YAML file is expected to provide all the necessary information (depending on the class in question) in the required form (appropriate types and structures). Dereferencing will will be automatically attempted on suitable classes, this requires `model` and `attribute_definitions` to be provided.

Examples:

Occi::Core::AttributeDefinition.from_yaml 'my_def.yml'
  # => #<Occi::Core::AttributeDefinition>

Parameters:

  • path (String)

    path to a YAML File

  • model (Occi::Core::Model) (defaults to: nil)

    model instance for dereferencing (category look-up)

  • attribute_definitions (Hash) (defaults to: nil)

    hash with known attribute definitions for dereferencing

Returns:

  • (Object)

    instance of receiver class


24
25
26
27
28
29
30
31
32
33
# File 'lib/occi/core/helpers/yaml_summoner.rb', line 24

def from_yaml(path, model = nil, attribute_definitions = nil)
  raise 'This method cannot be invoked on instances' unless is_a? Class
  allowed_classes = respond_to?(:allowed_yaml_classes, true) ? allowed_yaml_classes : []

  logger.debug "Loading YAML definition for #{self} from #{path.inspect}"
  object_args = YAML.safe_load(File.read(path), allowed_classes)
  object_args.symbolize_keys!
  object_args.dereference_with!(self, model, attribute_definitions) if needs_dereferencing?
  new object_args
end

#needs_dereferencing?TrueClass, FalseClass

Identifies classes suitable for dereferencing.

Returns:


39
40
41
# File 'lib/occi/core/helpers/yaml_summoner.rb', line 39

def needs_dereferencing?
  ancestors.include? Occi::Core::Category
end