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.
Instance Method Summary collapse
-
#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.
-
#needs_dereferencing? ⇒ TrueClass, FalseClass
Identifies classes suitable for dereferencing.
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.
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.
39 40 41 |
# File 'lib/occi/core/helpers/yaml_summoner.rb', line 39 def needs_dereferencing? ancestors.include? Occi::Core::Category end |