module DTK::DSL::YamlHelper

Public Class Methods

generate(yaml_object) click to toggle source

yaml_object can be a ::Hash or child of a ::Hash

# File lib/dsl/yaml_helper.rb, line 31
def self.generate(yaml_object)
  ::YAML.dump(convert_for_yaml_dump(yaml_object))
end
parse(file_obj) click to toggle source

Returns hash if succsefully parse; otherwise raises error

# File lib/dsl/yaml_helper.rb, line 22
def self.parse(file_obj)
  begin
    ::YAML.load(file_obj.content)
  rescue Exception => e
    raise Error::Usage, yaml_parsing_error_msg(e, file_obj)
  end
end

Private Class Methods

convert_for_yaml_dump(yaml_object) click to toggle source

this method converts embedded hash and array objects to be ::Hash and ::Array objects so YAML rendering does not have objects in it

# File lib/dsl/yaml_helper.rb, line 45
def self.convert_for_yaml_dump(yaml_object)
  if yaml_object.kind_of?(::Array)
    ret = []
    yaml_object.each { |el| ret << convert_for_yaml_dump(el) }
    ret
  elsif yaml_object.kind_of?(::Hash)
    yaml_object.inject({}) { |h, (k, v)| h.merge(k => convert_for_yaml_dump(v)) }
  else
    yaml_object
  end
end
yaml_parsing_error_msg(e, file_obj) click to toggle source
# File lib/dsl/yaml_helper.rb, line 37
def self.yaml_parsing_error_msg(e, file_obj)
  file_ref = FileParser.file_ref_in_error(file_obj)
  yaml_err_msg = e.message.gsub(/\(<unknown>\): /,'').capitalize 
  "YAML parsing error#{file_ref}:\n#{yaml_err_msg}"
end