Решение на Пета задача от Пламен Никифоров

Обратно към всички решения

Към профила на Пламен Никифоров

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 14 успешни тест(а)
  • 16 неуспешни тест(а)

Код

require 'base64'
require 'date'
class ObjectStore
attr_accessor :branch_list, :current_branch
def initialize
@current_branch = Branch.new("master", self)
@branch_list = [@current_branch]
end
def self.init
ObjectStore.new
end
def branch
@current_branch
end
def add(name, object)
@current_branch.objects_to_add << [name, object]
object = @current_branch.objects_to_add[-1][1]
message = "Added #{@current_branch.objects_to_add[-1][0]} to storage."
GoodResponse.new(message, object)
end
def make_changes
@current_branch.current_objects -= @current_branch.objects_to_remove
@current_branch.current_objects += @current_branch.objects_to_add
@current_branch.objects_to_remove = []
@current_branch.objects_to_add = []
end
def calculate_changes
changes = 0
changes += @current_branch.objects_to_add.size
changes += @current_branch.objects_to_remove.size
end
def commit(comment)
changes = calculate_changes
if changes == 0
BadResponse.new("Nothing to commit, working directory clean.")
else
make_changes
key = Base64.encode64(DateTime.now.to_s + comment)
comment_info = [@current_branch.current_objects, comment, DateTime.now]
@current_branch.commits[key] = comment_info
last_values = @current_branch.commits.values.last
@head = Commit.new(last_values, @current_branch.commits.keys.last)
GoodResponse.new("#{comment}\n\t#{changes} objects changed",@head)
end
end
def remove(name)
response = BadResponse.new("Object #{name} is not committed.")
@current_branch.current_objects.each do |object|
if object[0] == name
@current_branch.objects_to_remove << object
response = GoodResponse.new("Added #{name} for removal.",object)
end
end
response
end
def clean_commits(commits, hash)
commits.each do |key, commit|
if clean
commits.delete(key)
end
if key == hash
clean = true
end
end
commits
end
def checkout(commit_hash)
if @current_branch.commits.has_key?(commit_hash)
clean_commits(@current_branch.commits, commit_hash)
@current_branch.current_objects = @current_branch.commits[commit_hash]
commit = @current_branch.commits[commit_hash]
GoodResponse.new("HEAD is now at #{commit_hash}.", commit)
else
BadResponse.new("Commit #{commit_hash} does not exist.")
end
end
def visualise_log(log)
result = ""
log = log.to_a.reverse.to_h
log.each do |hash, commit|
result += "Commit #{hash}\nDate: #{commit[2]}\n\n\t#{commit[1]}"
result += "\n"
end
result.chop
Response.new(result)
end
def log
if @current_branch.commits
result = visualise_log(@current_branch.commits)
else
branch_name = @current_branch.branch_name
message = "Branch #{branch_name} does not have any commits yet."
BadResponse.new(message)
end
end
def head
if @current_branch.commits == {}
branch_name = @current_branch.branch_name
message = "Branch #{branch_name} does not have any commits yet."
BadResponse.new(message)
else
GoodResponse.new("#{@current_branch.commits.values.last[1]}",@head)
end
end
def get_objects_names
result = []
@current_branch.commits.values.last[0].each do |object|
result << object[0]
end
end
def get(name)
result = BadResponse.new("Object #{name} is not committed.")
get_objects_names.each do |object|
if object[0] == name
result = GoodResponse.new("Found object #{name}.", object)
end
end
result
end
end
class Commit
attr_accessor :hash, :objects, :message, :date
def initialize(data, hash)
@objects = data[0]
@message = data[1]
@date = data[2]
@hash = hash
end
end
class Branch
attr_accessor :objects_to_add, :objects_to_remove
attr_accessor :current_objects, :commits, :branch_name, :object_store
def initialize(branch_name, object_store, current_objects = [], commits = {})
@branch_name = branch_name
@object_store = object_store
@current_objects = current_objects
@commits = commits
@objects_to_add = []
@objects_to_remove = []
end
def create(branch_name)
response = Response.new("Created branch #{branch_name}")
@object_store.branch_list.each do |branch|
if branch.branch_name == branch
response = BadResponse.new("Branch #{branch_name} already exists.")
end
end
if response.kind_of?(Response)
branch = Branch.new(branch_name, @object_store,@current_objects, @commits)
@object_store.branch_list << branch
end
response
end
def checkout(branch_name)
result = BadResponse.new("Branch #{branch_name} does not exist.")
@object_store.branch_list.each do |branch|
if branch.branch_name == branch_name
result = Response.new("Switched to branch #{branch_name}.")
@object_store.current_branch = branch
end
end
result
end
def search_for_branch(name)
found = nil
@object_store.branch_list.each do |branch|
if branch.branch_name == name
found = branch
end
end
found
end
def remove(name)
current_branch_name = @object_store.current_branch.branch_name
if search_for_branch(name)
if search_for_branch(name).branch_name == current_branch_name
BadResponse.new("Cannot remove current branch.")
else
@object_store.branch_list.delete(search_for_branch(name))
Response.new("Removed branch #{name}.")
end
else
BadResponse.new("Branch #{name} does not exist.")
end
end
def list_to_string(list)
result = ""
list.each do |element|
if element == @object_store.current_branch.branch_name
result += "* " + element
else
result += " " + element
end
result += "\n"
end
result.chop
end
def list
result = []
@object_store.branch_list.each do |branch|
result << branch.branch_name
end
result.sort_by!{ |name| name.downcase }
Response.new(list_to_string(result))
end
end
class Response
def initialize(message)
@message = message
end
def message
"#{@message}"
end
def success?
true
end
def error?
false
end
end
class BadResponse < Response
def success?
false
end
def error?
true
end
end
class GoodResponse < Response
def initialize(message, result)
@message = message
@result = result
end
def result
@result
end
end

Лог от изпълнението

F..F....FFFFF.FFF....FF.FF..FF

Failures:

  1) ObjectStore can add objects
     Failure/Error: expect(repo.add("object", "content")).to be_success("Added object to stage.", "content")
       expected #<GoodResponse:0x007f26f609c490 @message="Added object to storage.", @result="content"> to be success "Added object to stage." and "content"
     # /tmp/d20160111-5693-t14h3u/spec.rb:23:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) ObjectStore can remove objects
     Failure/Error: expect(repo.remove("object1")).to be_success("Added object1 for removal.", "content1")
       expected #<GoodResponse:0x007f26f5d05138 @message="Added object1 for removal.", @result=["object1", "content1"]> to be success "Added object1 for removal." and "content1"
     # /tmp/d20160111-5693-t14h3u/spec.rb:43:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) ObjectStore can show log of changes for a single commit
     Failure/Error: expect(repo.log).to be_success("Commit #{commit_hash}\nDate: #{Time.now.strftime("%a %b %d %H:%M %Y %z")}\n\n\tSo cool!")
       expected #<Response:0x007f26f5c434c0 @message="Commit MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMFNvIGNvb2wh\n\nDate: 2016-01-11T11:54:38+02:00\n\n\tSo cool!\n"> to be success "Commit 381573fc6aa4a6bb947e617a74fc4e0a5e152245\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-t14h3u/spec.rb:83:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) ObjectStore can show log of changes for a single commit
     Failure/Error: expect(repo.log).to be_success("Commit #{commit_hash}\nDate: #{Time.now.strftime("%a %b %d %H:%M %Y %z")}\n\n\tSo cool!")
       expected #<Response:0x007f26f5c1a070 @message="Commit MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMFNvIGNvb2wh\n\nDate: 2016-01-11T11:54:38+02:00\n\n\tSo cool!\n"> to be success "Commit 381573fc6aa4a6bb947e617a74fc4e0a5e152245\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-t14h3u/spec.rb:92:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) ObjectStore can show log of changes for multiple commits
     Failure/Error: expect(repo.log).to be_success("Commit #{commit2_hash}\nDate: #{current_time}\n\n\tSecond commit\n\nCommit #{commit1_hash}\nDate: #{current_time}\n\n\tFirst commit")
       expected #<Response:0x007f26f5b802b8 @message="Commit MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMFNlY29uZCBjb21taXQ=\n\nDate: 2016-01-11T11:54:38+02:00\n\n\tSecond commit\nCommit MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMEZpcnN0IGNvbW1pdA==\n\nDate: 2016-01-11T11:54:38+02:00\n\n\tFirst commit\n"> to be success "Commit ffeb5391d1ec154dd4a821a1aeed876cad514a0d\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tSecond commit\n\nCommit 48bd765d675f355375408e84d12573236b16ddc2\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-t14h3u/spec.rb:108:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) ObjectStore shows the log for the current branch only
     Failure/Error: expect(repo.log).to be_success("Commit #{commit1_hash}\nDate: #{current_time}\n\n\tFirst commit")
       expected #<Response:0x007f26f5b622b8 @message="Commit MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMFNlY29uZCBjb21taXQ=\n\nDate: 2016-01-11T11:54:38+02:00\n\n\tSecond commit\nCommit MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMEZpcnN0IGNvbW1pdA==\n\nDate: 2016-01-11T11:54:38+02:00\n\n\tFirst commit\n"> to be success "Commit 48bd765d675f355375408e84d12573236b16ddc2\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-t14h3u/spec.rb:128:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) ObjectStore cannot show log for empty repository
     Failure/Error: expect(repo.log).to be_error("Branch master does not have any commits yet.")
       expected #<Response:0x007f26f5b40ac8 @message=""> to be error "Branch master does not have any commits yet."
     # /tmp/d20160111-5693-t14h3u/spec.rb:133:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) ObjectStore can create branches
     Failure/Error: expect(repo.branch.create("develop")).to be_success("Created branch develop.")
       expected #<Response:0x007f26f593bf98 @message="Created branch develop"> to be success "Created branch develop."
     # /tmp/d20160111-5693-t14h3u/spec.rb:145:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) ObjectStore cannot create branch if already exists
     Failure/Error: expect(repo.branch.create("master")).to be_error("Branch master already exists.")
       expected #<Response:0x007f26f5930710 @message="Created branch master"> to be error "Branch master already exists."
     # /tmp/d20160111-5693-t14h3u/spec.rb:150:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) ObjectStore can switch branches
     Failure/Error: expect(repo.head).to be_success("First commit", first_commit)
       expected #<GoodResponse:0x007f26f5906870 @message="Second commit", @result=#<Commit:0x007f26f59128c8 @objects=[["object1", "content1"], ["object2", "content2"]], @message="Second commit", @date=#<DateTime: 2016-01-11T11:54:38+02:00 ((2457399j,35678s,275603846n),+7200s,2299161j)>, @hash="MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMFNlY29uZCBjb21taXQ=\n">> to be success "First commit" and #<Commit:0x007f26f591bb08 @objects=[["object1", "content1"]], @message="First commit", @date=#<DateTime: 2016-01-11T11:54:38+02:00 ((2457399j,35678s,274978959n),+7200s,2299161j)>, @hash="MjAxNi0wMS0xMVQxMTo1NDozOCswMjowMEZpcnN0IGNvbW1pdA==\n">
     # /tmp/d20160111-5693-t14h3u/spec.rb:163:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) ObjectStore can be initialized with block
     Failure/Error: expect(repo.head).to be_success("Second commit", $second_commit)
       expected #<BadResponse:0x007f26f67d97a8 @message="Branch master does not have any commits yet."> to be success "Second commit" and nil
     # /tmp/d20160111-5693-t14h3u/spec.rb:195:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) ObjectStore can return objects
     Failure/Error: expect(repo.get("number")).to be_success("Found object number.", 21)
       expected #<GoodResponse:0x007f26f67d0f68 @message="Found object number.", @result=["number", 21]> to be success "Found object number." and 21
     # /tmp/d20160111-5693-t14h3u/spec.rb:202:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) ObjectStore cannot return objects when no commits
     Failure/Error: expect(repo.get("string")).to be_error("Object string is not committed.")
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160111-5693-t14h3u/solution.rb:124:in `get_objects_names'
     # /tmp/d20160111-5693-t14h3u/solution.rb:131:in `get'
     # /tmp/d20160111-5693-t14h3u/spec.rb:214:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) ObjectStore can checkout commits
     Failure/Error: expect(repo.checkout(first_commit.hash)).to be_success("HEAD is now at #{first_commit.hash}.", first_commit)
     NameError:
       undefined local variable or method `clean' for #<ObjectStore:0x007f26f67b53d0>
     # /tmp/d20160111-5693-t14h3u/solution.rb:69:in `block in clean_commits'
     # /tmp/d20160111-5693-t14h3u/solution.rb:68:in `each'
     # /tmp/d20160111-5693-t14h3u/solution.rb:68:in `clean_commits'
     # /tmp/d20160111-5693-t14h3u/solution.rb:82:in `checkout'
     # /tmp/d20160111-5693-t14h3u/spec.rb:223:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) ObjectStore can show the objects in a repo after overwriting an object
     Failure/Error: expect(first_commit.objects).to match_array(["content1"])
       expected collection contained:  ["content1"]
       actual collection contained:    [["object1", "content1"]]
       the missing elements were:      ["content1"]
       the extra elements were:        [["object1", "content1"]]
     # /tmp/d20160111-5693-t14h3u/spec.rb:243:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) ObjectStore can show the objects of a repo after removing an object
     Failure/Error: expect(first_commit.objects).to match_array(["content1", "content2", "content3"])
       expected collection contained:  ["content1", "content2", "content3"]
       actual collection contained:    [["object1", "content1"], ["object2", "content2"], ["object3", "content3"]]
       the missing elements were:      ["content1", "content2", "content3"]
       the extra elements were:        [["object1", "content1"], ["object2", "content2"], ["object3", "content3"]]
     # /tmp/d20160111-5693-t14h3u/spec.rb:259:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.03225 seconds
30 examples, 16 failures

Failed examples:

rspec /tmp/d20160111-5693-t14h3u/spec.rb:21 # ObjectStore can add objects
rspec /tmp/d20160111-5693-t14h3u/spec.rb:38 # ObjectStore can remove objects
rspec /tmp/d20160111-5693-t14h3u/spec.rb:77 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-t14h3u/spec.rb:86 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-t14h3u/spec.rb:95 # ObjectStore can show log of changes for multiple commits
rspec /tmp/d20160111-5693-t14h3u/spec.rb:111 # ObjectStore shows the log for the current branch only
rspec /tmp/d20160111-5693-t14h3u/spec.rb:131 # ObjectStore cannot show log for empty repository
rspec /tmp/d20160111-5693-t14h3u/spec.rb:143 # ObjectStore can create branches
rspec /tmp/d20160111-5693-t14h3u/spec.rb:148 # ObjectStore cannot create branch if already exists
rspec /tmp/d20160111-5693-t14h3u/spec.rb:153 # ObjectStore can switch branches
rspec /tmp/d20160111-5693-t14h3u/spec.rb:187 # ObjectStore can be initialized with block
rspec /tmp/d20160111-5693-t14h3u/spec.rb:198 # ObjectStore can return objects
rspec /tmp/d20160111-5693-t14h3u/spec.rb:212 # ObjectStore cannot return objects when no commits
rspec /tmp/d20160111-5693-t14h3u/spec.rb:217 # ObjectStore can checkout commits
rspec /tmp/d20160111-5693-t14h3u/spec.rb:239 # ObjectStore can show the objects in a repo after overwriting an object
rspec /tmp/d20160111-5693-t14h3u/spec.rb:253 # ObjectStore can show the objects of a repo after removing an object

История (1 версия и 0 коментара)

Пламен обнови решението на 23.11.2015 15:57 (преди над 8 години)

+require 'base64'
+require 'date'
+
+class ObjectStore
+
+ attr_accessor :branch_list, :current_branch
+
+ def initialize
+ @current_branch = Branch.new("master", self)
+ @branch_list = [@current_branch]
+ end
+
+ def self.init
+ ObjectStore.new
+ end
+
+ def branch
+ @current_branch
+ end
+
+ def add(name, object)
+ @current_branch.objects_to_add << [name, object]
+ object = @current_branch.objects_to_add[-1][1]
+ message = "Added #{@current_branch.objects_to_add[-1][0]} to storage."
+ GoodResponse.new(message, object)
+ end
+
+ def make_changes
+ @current_branch.current_objects -= @current_branch.objects_to_remove
+ @current_branch.current_objects += @current_branch.objects_to_add
+ @current_branch.objects_to_remove = []
+ @current_branch.objects_to_add = []
+ end
+
+ def calculate_changes
+ changes = 0
+ changes += @current_branch.objects_to_add.size
+ changes += @current_branch.objects_to_remove.size
+ end
+
+ def commit(comment)
+ changes = calculate_changes
+ if changes == 0
+ BadResponse.new("Nothing to commit, working directory clean.")
+ else
+ make_changes
+ key = Base64.encode64(DateTime.now.to_s + comment)
+ comment_info = [@current_branch.current_objects, comment, DateTime.now]
+ @current_branch.commits[key] = comment_info
+ last_values = @current_branch.commits.values.last
+ @head = Commit.new(last_values, @current_branch.commits.keys.last)
+ GoodResponse.new("#{comment}\n\t#{changes} objects changed",@head)
+ end
+ end
+
+ def remove(name)
+ response = BadResponse.new("Object #{name} is not committed.")
+ @current_branch.current_objects.each do |object|
+ if object[0] == name
+ @current_branch.objects_to_remove << object
+ response = GoodResponse.new("Added #{name} for removal.",object)
+ end
+ end
+ response
+ end
+
+ def clean_commits(commits, hash)
+ commits.each do |key, commit|
+ if clean
+ commits.delete(key)
+ end
+
+ if key == hash
+ clean = true
+ end
+ end
+ commits
+ end
+
+ def checkout(commit_hash)
+ if @current_branch.commits.has_key?(commit_hash)
+ clean_commits(@current_branch.commits, commit_hash)
+ @current_branch.current_objects = @current_branch.commits[commit_hash]
+ commit = @current_branch.commits[commit_hash]
+ GoodResponse.new("HEAD is now at #{commit_hash}.", commit)
+ else
+ BadResponse.new("Commit #{commit_hash} does not exist.")
+ end
+ end
+
+ def visualise_log(log)
+ result = ""
+ log = log.to_a.reverse.to_h
+ log.each do |hash, commit|
+ result += "Commit #{hash}\nDate: #{commit[2]}\n\n\t#{commit[1]}"
+ result += "\n"
+ end
+ result.chop
+ Response.new(result)
+ end
+
+ def log
+ if @current_branch.commits
+ result = visualise_log(@current_branch.commits)
+ else
+ branch_name = @current_branch.branch_name
+ message = "Branch #{branch_name} does not have any commits yet."
+ BadResponse.new(message)
+ end
+ end
+
+ def head
+ if @current_branch.commits == {}
+ branch_name = @current_branch.branch_name
+ message = "Branch #{branch_name} does not have any commits yet."
+ BadResponse.new(message)
+ else
+ GoodResponse.new("#{@current_branch.commits.values.last[1]}",@head)
+ end
+ end
+
+ def get_objects_names
+ result = []
+ @current_branch.commits.values.last[0].each do |object|
+ result << object[0]
+ end
+ end
+
+ def get(name)
+ result = BadResponse.new("Object #{name} is not committed.")
+ get_objects_names.each do |object|
+ if object[0] == name
+ result = GoodResponse.new("Found object #{name}.", object)
+ end
+ end
+ result
+ end
+
+end
+
+class Commit
+
+ attr_accessor :hash, :objects, :message, :date
+
+ def initialize(data, hash)
+ @objects = data[0]
+ @message = data[1]
+ @date = data[2]
+ @hash = hash
+ end
+
+end
+
+class Branch
+
+ attr_accessor :objects_to_add, :objects_to_remove
+ attr_accessor :current_objects, :commits, :branch_name, :object_store
+
+ def initialize(branch_name, object_store, current_objects = [], commits = {})
+ @branch_name = branch_name
+ @object_store = object_store
+ @current_objects = current_objects
+ @commits = commits
+ @objects_to_add = []
+ @objects_to_remove = []
+ end
+
+ def create(branch_name)
+ response = Response.new("Created branch #{branch_name}")
+ @object_store.branch_list.each do |branch|
+ if branch.branch_name == branch
+ response = BadResponse.new("Branch #{branch_name} already exists.")
+ end
+ end
+ if response.kind_of?(Response)
+ branch = Branch.new(branch_name, @object_store,@current_objects, @commits)
+ @object_store.branch_list << branch
+ end
+ response
+ end
+
+ def checkout(branch_name)
+ result = BadResponse.new("Branch #{branch_name} does not exist.")
+ @object_store.branch_list.each do |branch|
+ if branch.branch_name == branch_name
+ result = Response.new("Switched to branch #{branch_name}.")
+ @object_store.current_branch = branch
+ end
+ end
+ result
+ end
+
+ def search_for_branch(name)
+ found = nil
+ @object_store.branch_list.each do |branch|
+ if branch.branch_name == name
+ found = branch
+ end
+ end
+ found
+ end
+
+ def remove(name)
+ current_branch_name = @object_store.current_branch.branch_name
+ if search_for_branch(name)
+ if search_for_branch(name).branch_name == current_branch_name
+ BadResponse.new("Cannot remove current branch.")
+ else
+ @object_store.branch_list.delete(search_for_branch(name))
+ Response.new("Removed branch #{name}.")
+ end
+ else
+ BadResponse.new("Branch #{name} does not exist.")
+ end
+ end
+
+ def list_to_string(list)
+ result = ""
+ list.each do |element|
+ if element == @object_store.current_branch.branch_name
+ result += "* " + element
+ else
+ result += " " + element
+ end
+ result += "\n"
+ end
+ result.chop
+ end
+
+ def list
+ result = []
+ @object_store.branch_list.each do |branch|
+ result << branch.branch_name
+ end
+ result.sort_by!{ |name| name.downcase }
+ Response.new(list_to_string(result))
+ end
+
+end
+
+class Response
+
+ def initialize(message)
+ @message = message
+ end
+
+ def message
+ "#{@message}"
+ end
+
+ def success?
+ true
+ end
+
+ def error?
+ false
+ end
+
+end
+
+class BadResponse < Response
+
+ def success?
+ false
+ end
+
+ def error?
+ true
+ end
+
+end
+
+class GoodResponse < Response
+
+ def initialize(message, result)
+ @message = message
+ @result = result
+ end
+
+ def result
+ @result
+ end
+
+end