Module: DbAgile::Core::Repository::YAMLClassMethods

Included in:
DbAgile::Core::Repository
Defined in:
lib/dbagile/core/repository/yaml_methods.rb

Overview

module YAMLInstanceMethods

Instance Method Summary collapse

Instance Method Details

#from_yaml(str, root_path) ⇒ Object

Loads a repository from a YAML file and returns a Core::Repository instance.

This method is not robust at all and re-raises any error that occurs. It should be protected upstream.

Parameters:

  • str (String)

    a YAML source

  • root_path (String)

    root path to set on the repository



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dbagile/core/repository/yaml_methods.rb', line 47

def from_yaml(str, root_path)
  # Load the hash
  hash = YAML::load(str)

  # Load the repository version
  version = hash['version'].to_s.strip
  if version.nil? or version.empty?
    raise "missing version number"
  end

  # create the repository instance
  repo = Repository.new(root_path, version)

  # load databases
  hash['databases'].each_pair{|dbname, dbconfig|
    db = Core::Database.new(dbname.to_s.to_sym)
    dbconfig.each_pair{|key, value|
      db.send(:"#{key}=", value)
    }
    repo << db
  }

  # Set current database
  current = hash['current'].to_s.strip
  unless current.empty?
    repo.current_db_name = current.to_sym
  end

  repo
end

#from_yaml_file(file, root_path) ⇒ Object

Loads a repository from a YAML file and returns a Core::Repository instance.

This method is not robust at all and re-raises any error that occurs. It should be protected upstream.

Parameters:

  • file (String)

    path to a repository index (exist, readable)

  • root_path (String)

    path of the repository itself (exist, readable)

Raises:

  • DbAgile::CorruptedRepositoryError if anything goes wrong.



34
35
36
# File 'lib/dbagile/core/repository/yaml_methods.rb', line 34

def from_yaml_file(file, root_path)
  from_yaml(File.read(file), root_path)
end