Class: DbAgile::Core::Repository

Inherits:
Object
  • Object
show all
Extended by:
YAMLClassMethods
Includes:
YAMLInstanceMethods, Enumerable
Defined in:
lib/dbagile/core/repository.rb,
lib/dbagile/core/repository/builder.rb,
lib/dbagile/core/repository/yaml_methods.rb

Defined Under Namespace

Modules: YAMLClassMethods, YAMLInstanceMethods Classes: Builder

Constant Summary collapse

REPOSITORY_INDEX_FILE_NAME =

Index file name

"dbagile.idx"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from YAMLClassMethods

from_yaml, from_yaml_file

Methods included from YAMLInstanceMethods

#to_yaml

Constructor Details

#initialize(root_path, version = DbAgile::VERSION) ⇒ Repository

Creates a repository instance



31
32
33
34
35
36
37
# File 'lib/dbagile/core/repository.rb', line 31

def initialize(root_path, version = DbAgile::VERSION)
  DbAgile::Robustness::valid_rw_directory!(root_path)
  DbAgile::Robustness::valid_rw_file!(File.join(root_path, REPOSITORY_INDEX_FILE_NAME))
  @root_path = root_path
  @version = version
  @databases = []
end

Instance Attribute Details

#current_db_nameObject

Current database (its name, i.e. a Symbol)



24
25
26
# File 'lib/dbagile/core/repository.rb', line 24

def current_db_name
  @current_db_name
end

#databasesObject (readonly)

Databases as an array of Database instances



21
22
23
# File 'lib/dbagile/core/repository.rb', line 21

def databases
  @databases
end

#root_pathObject (readonly)

Path to the root path of the repository



14
15
16
# File 'lib/dbagile/core/repository.rb', line 14

def root_path
  @root_path
end

#versionObject

Repository version



17
18
19
# File 'lib/dbagile/core/repository.rb', line 17

def version
  @version
end

Class Method Details

.create!(root_path) ⇒ Repository

Creates a fresh new repository somewhere

Parameters:

  • root_path (String)

    path to an unexisting repository folder

Returns:

  • (Repository)

    the created repository instance

Raises:

  • IOError is the repository already exists or cannot be created.



171
172
173
174
175
176
177
178
# File 'lib/dbagile/core/repository.rb', line 171

def self.create!(root_path)
  DbAgile::Robustness::unexisting_directory!(root_path)
  index_file = File.join(root_path, REPOSITORY_INDEX_FILE_NAME)
  FileUtils.mkdir_p(root_path)
  FileUtils.touch(index_file)
  repo = Repository.new(root_path, DbAgile::VERSION)
  repo.save!
end

.load(root_path) ⇒ Object

Loads a repository from a root path

Parameters:

  • root_path (String)

    path to a repository folder

Raises:

  • IOError if required repository files do not exists or access is denied

  • DbAgile::CorruptedRepositoryError if anything goes wrong



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/dbagile/core/repository.rb', line 148

def self.load(root_path)
  # some checks first
  DbAgile::Robustness::valid_rw_directory!(root_path)
  index_file = File.join(root_path, REPOSITORY_INDEX_FILE_NAME)
  msg = "Not a dbagile repository, missing or access denied on #{index_file}"
  DbAgile::Robustness::valid_rw_file!(index_file, msg)
  
  # loading
  begin
    from_yaml_file(index_file, root_path) 
  rescue StandardError => ex
    msg = "Repository corruped: #{ex.message}"
    raise DbAgile::CorruptedRepositoryError, msg, ex.backtrace
  end
end

Instance Method Details

#<<(db) ⇒ Object

Adds a database instance



126
127
128
129
# File 'lib/dbagile/core/repository.rb', line 126

def <<(db)
  db.file_resolver = file_resolver
  self.databases << db
end

#current?(name_or_db) ⇒ Boolean

Checks if a name/database is the current one

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dbagile/core/repository.rb', line 84

def current?(name_or_db)
  case name_or_db
    when Symbol
      return nil unless has_database?(name_or_db)
      self.current_db_name == name_or_db
    when Core::Database
      self.current_db_name == name_or_db.name
    else
      raise ArgumentError, "Symbol or Database expected, #{name_or_db.inspect} found."
  end
end

#current_databaseObject

Returns the current database



117
118
119
# File 'lib/dbagile/core/repository.rb', line 117

def current_database
  database(current_db_name)
end

#database(match) ⇒ Object

Returns a database by match. Returns nil if no such database



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dbagile/core/repository.rb', line 102

def database(match)
  return match if match.kind_of?(::DbAgile::Core::Database)
  databases.find{|c| 
    case match
      when Symbol
        c.name == match
      when String
        c.uri == match
      when Regexp
        match =~ c.uri
    end
  }
end

#each(*args, &block) ⇒ Object

Yields the block with each database in turn



97
98
99
# File 'lib/dbagile/core/repository.rb', line 97

def each(*args, &block)
  databases.each(*args, &block)
end

#empty?Boolean

Checks if at least one database exists

Returns:

  • (Boolean)


74
75
76
# File 'lib/dbagile/core/repository.rb', line 74

def empty?
  databases.empty?
end

#file_resolverObject

Returns a file resolver Proc instance



54
55
56
57
58
59
60
61
62
# File 'lib/dbagile/core/repository.rb', line 54

def file_resolver
  @file_resolver ||= lambda{|f| 
    if f[0, 1] == '/'
      f
    else
      File.join(File.expand_path(self.root_path), f) 
    end
  }
end

#friendly_pathObject

Returns a friendly path to be printed to user



44
45
46
# File 'lib/dbagile/core/repository.rb', line 44

def friendly_path
  DbAgile::FileSystemTools::friendly_path!(root_path)
end

#has_database?(name) ⇒ Boolean

Checks if a database exists

Returns:

  • (Boolean)


79
80
81
# File 'lib/dbagile/core/repository.rb', line 79

def has_database?(name)
  !database(name).nil?
end

#index_fileObject

Returns the path to the index file



49
50
51
# File 'lib/dbagile/core/repository.rb', line 49

def index_file
  File.join(root_path, REPOSITORY_INDEX_FILE_NAME)
end

#remove(db) ⇒ Object

Removes a database from this repository



132
133
134
135
# File 'lib/dbagile/core/repository.rb', line 132

def remove(db)
  db = self.database(db)
  db.nil? ? nil : databases.delete(db)
end

#resolve_file(f) ⇒ Object

Resolves a file which could be relative to repository root path



65
66
67
# File 'lib/dbagile/core/repository.rb', line 65

def resolve_file(f)
  file_resolve.call(f)
end

#save!Repository

Saves the repository

Returns:

Raises:

  • IOError if something goes wrong when saving the repository



186
187
188
189
190
# File 'lib/dbagile/core/repository.rb', line 186

def save!
  DbAgile::Robustness::valid_rw_file!(self.index_file)
  flush(self.index_file)
  self
end