class Repository::MemoryRepository

Implements AbstractRepository for memory repositories It implements the following paradigm:

1. Repositories are created by using MemoryRepository.create()
2. Existing repositories are opened by using either SubversionRepository.open()
   or SubversionRepository.new()

Public Instance Methods

add_user(user_id, permissions) click to toggle source

Adds a user to the repository and grants him/her the provided permissions

# File lib/repo/memory_repository.rb, line 195
def add_user(user_id, permissions)
  if @users.key?(user_id)
    raise UserAlreadyExistent.new(user_id +" exists already")
  end
  @users[user_id] = permissions
end
close() click to toggle source

Closes the repository. This does nothing except set a proper value for the closed? function It is not important to close memory repositories (is it possible?)

# File lib/repo/memory_repository.rb, line 285
def close
  @opened = false
end
closed?() click to toggle source

Resturns whether or not the repository is closed. This will return a value corresponding to whether the open or close functions have been called but is otherwise meaningless in a MemoryRepository

# File lib/repo/memory_repository.rb, line 292
def closed?
  return !@opened
end
commit(transaction) click to toggle source
# File lib/repo/memory_repository.rb, line 115
def commit(transaction)
  jobs = transaction.jobs
  # make a deep copy of current revision
  new_rev = copy_revision(@current_revision)
  new_rev.user_id = transaction.user_id # set commit-user for new revision
  jobs.each do |job|
    case job[:action]
    when :add_path
      begin
        new_rev = make_directory(new_rev, job[:path])
      rescue Repository::Conflict => e
        transaction.add_conflict(e)
      end
    when :add
      begin
        new_rev = add_file(new_rev, job[:path], job[:file_data], job[:mime_type])
      rescue Repository::Conflict => e
        transaction.add_conflict(e)
      end
    when :remove
      begin
        new_rev = remove_file(new_rev, job[:path], job[:expected_revision_number])
      rescue Repository::Conflict => e
        transaction.add_conflict(e)
      end
    when :replace
      begin
        new_rev = replace_file_content(new_rev, job[:path], job[:file_data], job[:mime_type], job[:expected_revision_number])
      rescue Repository::Conflict => e
        transaction.add_conflict(e)
      end
    end
  end

  if transaction.conflicts?
    return false
  end

  # everything went fine, so push old revision to history revisions,
  # make new_rev the latest one and create a mapping for timestamped
  # revisions
  timestamp = Time.now
  new_rev.timestamp = timestamp
  @revision_history.push(@current_revision)
  @current_revision = new_rev
  @current_revision.__increment_revision_number() # increment revision number
  @timestamps_revisions[timestamp._dump] = @current_revision
  @@repositories[@repository_location] = self
  return true
end
download_as_string(files) click to toggle source
Alias for: stringify_files
expand_path(file_name, dir_string = "/") click to toggle source

Converts a pathname to an absolute pathname

# File lib/repo/memory_repository.rb, line 297
def expand_path(file_name, dir_string = "/")
  expanded = File.expand_path(file_name, dir_string)
  if RUBY_PLATFORM =~ /(:?mswin|mingw)/ #only if the platform is Windows
    expanded = expanded[2..-1]#remove the drive letter ('D:')
  end
  return expanded
end
get_latest_revision() click to toggle source

Returns the latest revision number (as a RepositoryRevision object)

# File lib/repo/memory_repository.rb, line 167
def get_latest_revision()
  return @current_revision
end
get_permissions(user_id) click to toggle source

Gets permissions for a given user

# File lib/repo/memory_repository.rb, line 240
def get_permissions(user_id)
  if !@users.key?(user_id)
    raise UserNotFound.new(user_id + " not found")
  end
  return @users[user_id]
end
get_revision(rev_num) click to toggle source

Return a RepositoryRevision for a given rev_num (int)

# File lib/repo/memory_repository.rb, line 172
def get_revision(rev_num)
  if (@current_revision.revision_number == rev_num)
    return @current_revision
  end
  @revision_history.each do |revision|
    if (revision.revision_number == rev_num)
      return revision
    end
  end
  # revision with the specified revision number does not exist,
  # so raise error
  raise RevisionDoesNotExist
end
get_revision_by_timestamp(timestamp, path = nil) click to toggle source

Return a RepositoryRevision for a given timestamp

# File lib/repo/memory_repository.rb, line 187
def get_revision_by_timestamp(timestamp, path = nil)
  if !timestamp.kind_of?(Time)
    raise "Was expecting a timestamp of type Time"
  end
  return get_revision_number_by_timestamp(timestamp, path)
end
get_transaction(user_id, comment="") click to toggle source
# File lib/repo/memory_repository.rb, line 108
def get_transaction(user_id, comment="")
  if user_id.nil?
    raise "Expected a user_id (Repository.get_transaction(user_id))"
  end
  return Repository::Transaction.new(user_id, comment)
end
get_users(permissions) click to toggle source

Gets a list of users with AT LEAST the provided permissions. Returns nil if there aren’t any.

# File lib/repo/memory_repository.rb, line 217
def get_users(permissions)
  result_list = []
  @users.each do |user, perm|
    if perm >= permissions
      result_list.push(user)
    end
  end
  if !result_list.empty?
    return result_list
  else
    return nil
  end
end
has_user?(user_id) click to toggle source

Semi-private - used by the bulk permissions assignments

# File lib/repo/memory_repository.rb, line 203
def has_user?(user_id)
  return @users.key?(user_id)
end
remove_user(user_id) click to toggle source

Removes a user from from the repository

# File lib/repo/memory_repository.rb, line 208
def remove_user(user_id)
  if !@users.key?(user_id)
    raise UserNotFound.new(user_id + " not found")
  end
  @users.delete(user_id)
end
set_permissions(user_id, permissions) click to toggle source

Sets permissions for the provided user

# File lib/repo/memory_repository.rb, line 232
def set_permissions(user_id, permissions)
  if !@users.key?(user_id)
    raise UserNotFound.new(user_id + " not found")
  end
  @users[user_id] = permissions
end
stringify_files(files) click to toggle source

Given either an array of, or a single object of class RevisionFile, return a stream of data for the user to download as the file(s).

# File lib/repo/memory_repository.rb, line 84
def stringify_files(files)
  is_array = files.kind_of? Array
  if (!is_array)
    files = [files]
  end
  files.collect! do |file|
    if (!file.kind_of? Repository::RevisionFile)
      raise TypeError.new("Expected a Repository::RevisionFile")
    end
    rev = get_revision(file.from_revision)
    content = rev.files_content[file.to_s]
    if content.nil?
      raise FileDoesNotExistConflict.new(File.join(file.path, file.name))
    end
    content # spews out content to be collected (Ruby collect!() magic) :-)
  end
  if (!is_array)
    return files.first
  else
    return files
  end
end
Also aliased as: download_as_string

Public Class Methods

access(connect_string) { |repository| ... } click to toggle source

Static method: Yields an existing Memory repository and closes it afterwards

# File lib/repo/memory_repository.rb, line 276
def self.access(connect_string)
  repository = self.open(connect_string)
  yield repository
  repository.close
end
create(location) click to toggle source

Creates memory repository at “virtual” location (they are identifiable by location)

# File lib/repo/memory_repository.rb, line 65
def self.create(location)
  if !MemoryRepository.repository_exists?(location)
    MemoryRepository.new(location) # create a repository if it doesn't exist
  end
  return true
end
delete(repo_path) click to toggle source

Static method: Deletes an existing memory repository

# File lib/repo/memory_repository.rb, line 73
def self.delete(repo_path)
  @@repositories.delete(repo_path)
end
delete_bulk_permissions(repo_names, user_ids) click to toggle source

Delete permissions for many repositories

# File lib/repo/memory_repository.rb, line 263
def self.delete_bulk_permissions(repo_names, user_ids)
  repo_names.each do |repo_name|
    repo = self.open(repo_name)
    user_ids.each do |user_id|
      if(repo.has_user?(user_id))
        repo.remove_user(user_id)
      end
    end
  end
  return true
end
new(location) click to toggle source

Constructor: Connects to an existing Memory repository; Note: A repository has to be created using ::create, if it is not yet existent Generally: Do not(!) call it with 2 parameters, use ::create instead!

# File lib/repo/memory_repository.rb, line 26
def initialize(location)

  # variables
  @users = {}                                 # hash of users (key) with corresponding permissions (value)
  @current_revision = MemoryRevision.new(0)   # the latest revision (we start from 0)
  @revision_history = []                      # a list (array) of old revisions (i.e. < @current_revision)
  # mapping (hash) of timestamps and revisions
  @timestamps_revisions = {}
  @timestamps_revisions[Time.now._dump] = @current_revision   # push first timestamp-revision mapping
  @repository_location = location
  @opened = true


  if MemoryRepository.repository_exists?(location)
    raise RepositoryCollision.new("There is already a repository at #{location}")
  end
  @@repositories[location] = self             # push new MemoryRepository onto repository list

end
open(location) click to toggle source

Open repository at specified location

# File lib/repo/memory_repository.rb, line 57
def self.open(location)
  if !self.repository_exists?(location)
    raise "Could not open repository at location #{location}"
  end
  return @@repositories[location] # return reference in question
end
purge_all() click to toggle source

Destroys all repositories

# File lib/repo/memory_repository.rb, line 78
def self.purge_all()
  @@repositories = {}
end
repository_exists?(path) click to toggle source

Checks if a memory repository exists at ‘path’

# File lib/repo/memory_repository.rb, line 47
def self.repository_exists?(path)
  @@repositories.each do |location, repo|
    if path == location
      return true
    end
  end
  return false
end
set_bulk_permissions(repo_names, user_id_permissions_map) click to toggle source

Set permissions for many repositories

# File lib/repo/memory_repository.rb, line 248
def self.set_bulk_permissions(repo_names, user_id_permissions_map)
  repo_names.each do |repo_name|
    repo = self.open(repo_name)
    user_id_permissions_map.each do |user_id, permissions|
      if(!repo.has_user?(user_id))
        repo.add_user(user_id, permissions)
      else
        repo.set_permissions(user_id, permissions)
      end
    end
  end
  return true
end