Решение на Пета задача от Малина Демирова

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

Към профила на Малина Демирова

Резултати

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

Код

require 'time'
require 'digest/sha1'
class ObjectStore
attr_reader :objects, :instructions, :names, :date, :hash, :commit_messages
def initialize(name, object)
@date = DateTime.now
@instructions = {}
@commit_messages = {}
@objects = object
@names = name
@hash = Digest::SHA1.new
end
COMMANDS = [ :add, :commit, :remove, :get, :message, :head, :log].freeze
COMMANDS.each do |command_name|
define_method(command_name) do |arguments|
@instructions << [command_name, *arguments]
end
end
def self.init(&instructions)
instance_eval(&instructions) if instructions || block_given?
self
end
def self.message(name)
thing = name
return "Added #{thing} to stage."
end
def self.message_count(text, count)
if count == 1
return "#{text.capitalize} '\n\t' #{count} object changed"
else
return "#{text.capitalize} '\n\t' #{count} objects changed"
end
end
def self.add(name, object)
@objects = {}
@objects[name] = object
@objects[name]
self.message(name)
#TODO add more objects?
end
def self.commit(message)
count = @objects.length
if message != "" || count != 0
date, hash_string = Time.now.rfc2822 , date.to_s + message
hash = Digest::SHA1.hexdigest(hash_string)
@objects.select do |object|
@commit_messages = {:object =>
{:message => message, :hash => hash, :date => date}}
end
@commit_messages
self.message_count(message,count)
else
return "Nothing to commit, working directory clean."
end
end
def self.remove(name)
for_removing = self.get('name')
if for_removing == nil
return "Object #{name} is not committed."
else
for_removing.delete
return "Added #{name} for removal. '\n '" + for_removing
end
end
def success?
end
def error?
end
def self.head
last_object, final_result = @commit_messages.values.last , nil
if last_object == nil
return "Branch #{name} does not have any commits yet"
else
#message, hash, date = last_object.values_at(:message, :hash, :date)
final_result = result
return final_result.to_s
end
end
def self.result
last = @commit_messages.values.last
message, hash, date = last.values_at(:message, :hash, :date)
final_result = message + " " + hash + " " + date
return final_result
end
def self.get(name)
taken_object = ""
@objects.select do |object|
if object[name] == name
taken_object = @objects[name]
end
end.first
if taken_object == ""
return "Object #{name} is not commited."
else
return "Found object #{name}." + " \n " + taken_object
end
end
def self.log
end
end
class Branch
#TODO
end

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

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) ObjectStore can add objects
     Failure/Error: actual.message == message &&
     NoMethodError:
       undefined method `message' for "Added object to stage.":String
     # /tmp/d20160111-5693-161gogt/spec.rb:6:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-161gogt/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 commit objects
     Failure/Error: expect(repo.commit("So cool!")).to be_success("So cool!\n\t2 objects changed", repo.head.result)
     NoMethodError:
       undefined method `result' for #<String:0x007fecbd160f10>
     # /tmp/d20160111-5693-161gogt/spec.rb:30: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 cannot commit without changed objects
     Failure/Error: result.message == message &&
     NoMethodError:
       undefined method `message' for "So cool!! '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:15:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-161gogt/spec.rb:35: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 remove objects
     Failure/Error: expect(repo.remove("object1")).to be_success("Added object1 for removal.", "content1")
     ArgumentError:
       wrong number of arguments (0 for 1+)
     # /tmp/d20160111-5693-161gogt/solution.rb:72:in `delete'
     # /tmp/d20160111-5693-161gogt/solution.rb:72:in `remove'
     # /tmp/d20160111-5693-161gogt/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)>'

  5) ObjectStore can commit changes which include only removed objects
     Failure/Error: repo.remove("object2")
     ArgumentError:
       wrong number of arguments (0 for 1+)
     # /tmp/d20160111-5693-161gogt/solution.rb:72:in `delete'
     # /tmp/d20160111-5693-161gogt/solution.rb:72:in `remove'
     # /tmp/d20160111-5693-161gogt/spec.rb:52: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 cannot remove objects that are not committed
     Failure/Error: expect(repo.remove("object2")).to be_error("Object object2 is not committed.")
     ArgumentError:
       wrong number of arguments (0 for 1+)
     # /tmp/d20160111-5693-161gogt/solution.rb:72:in `delete'
     # /tmp/d20160111-5693-161gogt/solution.rb:72:in `remove'
     # /tmp/d20160111-5693-161gogt/spec.rb:60: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 can show head
     Failure/Error: last_commit = repo.commit("There we go").result
     NoMethodError:
       undefined method `result' for "There we go '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:68: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 cannot show head for empty repository
     Failure/Error: result.message == message &&
     NoMethodError:
       undefined method `message' for #<String:0x007fecbd104dc8>
     # /tmp/d20160111-5693-161gogt/spec.rb:15:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-161gogt/spec.rb:74: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 can show log of changes for a single commit
     Failure/Error: commit = repo.commit("So cool!").result
     NoMethodError:
       undefined method `result' for "So cool! '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:81: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 show log of changes for a single commit
     Failure/Error: commit = repo.commit("So cool!").result
     NoMethodError:
       undefined method `result' for "So cool! '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:90: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 show log of changes for multiple commits
     Failure/Error: commit1 = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for "First commit '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:98: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 shows the log for the current branch only
     Failure/Error: commit1 = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for "First commit '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:114: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 show log for empty repository
     Failure/Error: result.message == message &&
     NoMethodError:
       undefined method `message' for nil:NilClass
     # /tmp/d20160111-5693-161gogt/spec.rb:15:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-161gogt/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)>'

  14) ObjectStore can list branches
     Failure/Error: repo.branch.create("develop")
     NoMethodError:
       undefined method `branch' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/spec.rb:138: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 create branches
     Failure/Error: expect(repo.branch.create("develop")).to be_success("Created branch develop.")
     NoMethodError:
       undefined method `branch' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/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)>'

  16) ObjectStore cannot create branch if already exists
     Failure/Error: expect(repo.branch.create("master")).to be_error("Branch master already exists.")
     NoMethodError:
       undefined method `branch' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/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)>'

  17) ObjectStore can switch branches
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for "First commit '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:156: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)>'

  18) ObjectStore cannot switch to nonexisting branch
     Failure/Error: expect(repo.branch.checkout("develop")).to be_error("Branch develop does not exist.")
     NoMethodError:
       undefined method `branch' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/spec.rb:168: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)>'

  19) ObjectStore can remove branch
     Failure/Error: repo.branch.create("develop")
     NoMethodError:
       undefined method `branch' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/spec.rb:173: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)>'

  20) ObjectStore cannot remove current branch
     Failure/Error: expect(repo.branch.remove("master")).to be_error("Cannot remove current branch.")
     NoMethodError:
       undefined method `branch' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/spec.rb:179: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)>'

  21) ObjectStore cannot remove nonexisting branch
     Failure/Error: expect(repo.branch.remove("develop")).to be_error("Branch develop does not exist.")
     NoMethodError:
       undefined method `branch' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/spec.rb:184: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)>'

  22) ObjectStore can be initialized with block
     Failure/Error: $second_commit = commit("Second commit").result
     NoMethodError:
       undefined method `result' for "Second commit '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:193:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-161gogt/solution.rb:25:in `instance_eval'
     # /tmp/d20160111-5693-161gogt/solution.rb:25:in `init'
     # /tmp/d20160111-5693-161gogt/spec.rb:188: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)>'

  23) ObjectStore can return objects
     Failure/Error: expect(repo.get("number")).to be_success("Found object number.", 21)
     TypeError:
       no implicit conversion of Fixnum into String
     # /tmp/d20160111-5693-161gogt/solution.rb:115:in `+'
     # /tmp/d20160111-5693-161gogt/solution.rb:115:in `get'
     # /tmp/d20160111-5693-161gogt/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)>'

  24) ObjectStore cannot return not committed objects
     Failure/Error: result.message == message &&
     NoMethodError:
       undefined method `message' for "Object string is not commited.":String
     # /tmp/d20160111-5693-161gogt/spec.rb:15:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-161gogt/spec.rb:209: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)>'

  25) ObjectStore cannot return objects when no commits
     Failure/Error: result.message == message &&
     NoMethodError:
       undefined method `message' for "Object string is not commited.":String
     # /tmp/d20160111-5693-161gogt/spec.rb:15:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-161gogt/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)>'

  26) ObjectStore can checkout commits
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for "First commit '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:220: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)>'

  27) ObjectStore cannot checkout commits with nonexisting hashes
     Failure/Error: expect(repo.checkout("[not-present]")).to be_error("Commit [not-present] does not exist.")
     NoMethodError:
       undefined method `checkout' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/spec.rb:231: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)>'

  28) ObjectStore cannot checkout commits in empty repository
     Failure/Error: expect(repo.checkout("[not-present]")).to be_error("Commit [not-present] does not exist.")
     NoMethodError:
       undefined method `checkout' for ObjectStore:Class
     # /tmp/d20160111-5693-161gogt/spec.rb:236: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)>'

  29) ObjectStore can show the objects in a repo after overwriting an object
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for "First commit '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:242: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)>'

  30) ObjectStore can show the objects of a repo after removing an object
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for "First commit '\n\t' 1 object changed":String
     # /tmp/d20160111-5693-161gogt/spec.rb:258: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.01745 seconds
30 examples, 30 failures

Failed examples:

rspec /tmp/d20160111-5693-161gogt/spec.rb:21 # ObjectStore can add objects
rspec /tmp/d20160111-5693-161gogt/spec.rb:26 # ObjectStore can commit objects
rspec /tmp/d20160111-5693-161gogt/spec.rb:33 # ObjectStore cannot commit without changed objects
rspec /tmp/d20160111-5693-161gogt/spec.rb:38 # ObjectStore can remove objects
rspec /tmp/d20160111-5693-161gogt/spec.rb:46 # ObjectStore can commit changes which include only removed objects
rspec /tmp/d20160111-5693-161gogt/spec.rb:56 # ObjectStore cannot remove objects that are not committed
rspec /tmp/d20160111-5693-161gogt/spec.rb:63 # ObjectStore can show head
rspec /tmp/d20160111-5693-161gogt/spec.rb:72 # ObjectStore cannot show head for empty repository
rspec /tmp/d20160111-5693-161gogt/spec.rb:77 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-161gogt/spec.rb:86 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-161gogt/spec.rb:95 # ObjectStore can show log of changes for multiple commits
rspec /tmp/d20160111-5693-161gogt/spec.rb:111 # ObjectStore shows the log for the current branch only
rspec /tmp/d20160111-5693-161gogt/spec.rb:131 # ObjectStore cannot show log for empty repository
rspec /tmp/d20160111-5693-161gogt/spec.rb:136 # ObjectStore can list branches
rspec /tmp/d20160111-5693-161gogt/spec.rb:143 # ObjectStore can create branches
rspec /tmp/d20160111-5693-161gogt/spec.rb:148 # ObjectStore cannot create branch if already exists
rspec /tmp/d20160111-5693-161gogt/spec.rb:153 # ObjectStore can switch branches
rspec /tmp/d20160111-5693-161gogt/spec.rb:166 # ObjectStore cannot switch to nonexisting branch
rspec /tmp/d20160111-5693-161gogt/spec.rb:171 # ObjectStore can remove branch
rspec /tmp/d20160111-5693-161gogt/spec.rb:177 # ObjectStore cannot remove current branch
rspec /tmp/d20160111-5693-161gogt/spec.rb:182 # ObjectStore cannot remove nonexisting branch
rspec /tmp/d20160111-5693-161gogt/spec.rb:187 # ObjectStore can be initialized with block
rspec /tmp/d20160111-5693-161gogt/spec.rb:198 # ObjectStore can return objects
rspec /tmp/d20160111-5693-161gogt/spec.rb:205 # ObjectStore cannot return not committed objects
rspec /tmp/d20160111-5693-161gogt/spec.rb:212 # ObjectStore cannot return objects when no commits
rspec /tmp/d20160111-5693-161gogt/spec.rb:217 # ObjectStore can checkout commits
rspec /tmp/d20160111-5693-161gogt/spec.rb:227 # ObjectStore cannot checkout commits with nonexisting hashes
rspec /tmp/d20160111-5693-161gogt/spec.rb:234 # ObjectStore cannot checkout commits in empty repository
rspec /tmp/d20160111-5693-161gogt/spec.rb:239 # ObjectStore can show the objects in a repo after overwriting an object
rspec /tmp/d20160111-5693-161gogt/spec.rb:253 # ObjectStore can show the objects of a repo after removing an object

История (3 версии и 2 коментара)

Малина обнови решението на 22.11.2015 20:42 (преди над 8 години)

+require 'date'
+require 'digest/sha1'
+
+class ObjectStore
+attr_reader :objects, :instructions, :names, :date, :hash
+
+def initialize(name, object, head)
+@date = DateTime.now
+@instructions = {}
+@objects = object
+@names = name
+@hash = Digest::SHA1.new
+end
+
+COMMANDS = [ :add, :commit, :remove, :get, :message, :head].freeze
+
+COMMANDS.each do |command_name|
+define_method(command_name) do |arguments|
+@instructions << [command_name, *arguments]
+end
+end
+
+def self.init(&instructions)
+instance_eval(&instructions) if instructions
+self
+end
+
+def self.message(name)
+thing = name
+return "Added #{thing} to stage."
+end
+
+def self.message_count(text, count)
+if count == 1
+return "#{text.capitalize}\n\t #{count} object changed"
+else
+return "#{text.capitalize}\n\t #{count} objects changed"
+end
+end
+
+def self.add(name, object)
+@objects = {}
+@objects[name] = object
+
+@objects[name]
+self.message(name)
+#TODO add more objects?
+end
+
+
+def self.commit(message)
+if message != ""
+count = @objects.length
+self.message_count(message,count)
+else
+return "Nothing to commit, working directory clean."
+end
+end
+
+def self.remove(name)
+for_removing = self.get(@objects[name])
+if for_removing == ""
+return "Object #{name} is not committed."
+else
+for_removing.delete
+return "Added #{name} for removal. '\n '" + for_removing
+end
+end
+
+
+def success?
+
+end
+
+def error?
+
+end
+
+def self.head
+last_object = @objects[1..-1]
+#message = last_object[commit(message)]
+result = last_object
+
+return result.to_s
+end
+
+def self.get(name)
+taken_object = ""
+@objects.select do |object|
+if object[name] == name
+taken_object = @objects[name]
+end
+end.first
+
+if taken_object == ""
+return "Object #{name} is not commited."
+else
+return "Found object #{name}." + " \n " + taken_object
+end
+#TODO add message
+end
+
+end
+
+class Branch
+#TODO
+end

Малина обнови решението на 23.11.2015 14:03 (преди над 8 години)

-require 'date'
+require 'time'
require 'digest/sha1'
class ObjectStore
-attr_reader :objects, :instructions, :names, :date, :hash
+attr_reader :objects, :instructions, :names, :date, :hash, :commit_messages
-def initialize(name, object, head)
+def initialize(name, object)
@date = DateTime.now
@instructions = {}
+@commit_messages = {}
@objects = object
@names = name
@hash = Digest::SHA1.new
end
-COMMANDS = [ :add, :commit, :remove, :get, :message, :head].freeze
+COMMANDS = [ :add, :commit, :remove, :get, :message, :head, :log].freeze
COMMANDS.each do |command_name|
define_method(command_name) do |arguments|
@instructions << [command_name, *arguments]
end
end
def self.init(&instructions)
instance_eval(&instructions) if instructions
self
end
def self.message(name)
thing = name
return "Added #{thing} to stage."
end
def self.message_count(text, count)
if count == 1
-return "#{text.capitalize}\n\t #{count} object changed"
+return "#{text.capitalize} '\n\t' #{count} object changed"
else
-return "#{text.capitalize}\n\t #{count} objects changed"
+return "#{text.capitalize} '\n\t' #{count} objects changed"
end
end
def self.add(name, object)
@objects = {}
@objects[name] = object
@objects[name]
self.message(name)
#TODO add more objects?
end
def self.commit(message)
-if message != ""
count = @objects.length
+if message != "" && count != 0
+date, hash_string = Time.now.rfc2822, date.to_s + message
+hash = Digest::SHA1.hexdigest(hash_string)
+@objects.select do |object|
+@commit_messages = {:object =>
+{:message => message, :hash => hash, :date => date}}
+end
+@commit_messages
self.message_count(message,count)
else
return "Nothing to commit, working directory clean."
end
end
def self.remove(name)
-for_removing = self.get(@objects[name])
-if for_removing == ""
+for_removing = self.get('name')
+if for_removing == nil
return "Object #{name} is not committed."
else
for_removing.delete
return "Added #{name} for removal. '\n '" + for_removing
end
end
def success?
end
def error?
end
def self.head
-last_object = @objects[1..-1]
-#message = last_object[commit(message)]
-result = last_object
-
-return result.to_s
+last_object = @commit_messages[1..-1]
+if last_object == nil
+return "Branch #{name} does not have any commits yet"
+else
+message, hash, date = last_object.values_at(:message, :hash, :date)
+result = message + hash + date
+return message + result
end
+end
def self.get(name)
taken_object = ""
@objects.select do |object|
if object[name] == name
taken_object = @objects[name]
end
end.first
if taken_object == ""
return "Object #{name} is not commited."
else
return "Found object #{name}." + " \n " + taken_object
end
-#TODO add message
+end
+
+def self.log
+
end
end
class Branch
#TODO
end

Малина обнови решението на 23.11.2015 17:11 (преди над 8 години)

require 'time'
require 'digest/sha1'
class ObjectStore
attr_reader :objects, :instructions, :names, :date, :hash, :commit_messages
def initialize(name, object)
@date = DateTime.now
@instructions = {}
@commit_messages = {}
@objects = object
@names = name
@hash = Digest::SHA1.new
end
COMMANDS = [ :add, :commit, :remove, :get, :message, :head, :log].freeze
COMMANDS.each do |command_name|
define_method(command_name) do |arguments|
@instructions << [command_name, *arguments]
end
end
def self.init(&instructions)
-instance_eval(&instructions) if instructions
+instance_eval(&instructions) if instructions || block_given?
self
end
def self.message(name)
thing = name
return "Added #{thing} to stage."
end
def self.message_count(text, count)
if count == 1
return "#{text.capitalize} '\n\t' #{count} object changed"
else
return "#{text.capitalize} '\n\t' #{count} objects changed"
end
end
def self.add(name, object)
@objects = {}
@objects[name] = object
-
@objects[name]
self.message(name)
#TODO add more objects?
end
def self.commit(message)
count = @objects.length
-if message != "" && count != 0
-date, hash_string = Time.now.rfc2822, date.to_s + message
+if message != "" || count != 0
+date, hash_string = Time.now.rfc2822 , date.to_s + message
hash = Digest::SHA1.hexdigest(hash_string)
@objects.select do |object|
@commit_messages = {:object =>
{:message => message, :hash => hash, :date => date}}
end
@commit_messages
self.message_count(message,count)
else
return "Nothing to commit, working directory clean."
end
end
def self.remove(name)
for_removing = self.get('name')
if for_removing == nil
return "Object #{name} is not committed."
else
for_removing.delete
return "Added #{name} for removal. '\n '" + for_removing
end
end
-
def success?
end
def error?
end
def self.head
-last_object = @commit_messages[1..-1]
+last_object, final_result = @commit_messages.values.last , nil
if last_object == nil
return "Branch #{name} does not have any commits yet"
else
-message, hash, date = last_object.values_at(:message, :hash, :date)
-result = message + hash + date
-return message + result
+#message, hash, date = last_object.values_at(:message, :hash, :date)
+final_result = result
+return final_result.to_s
end
end
+def self.result
+last = @commit_messages.values.last
+message, hash, date = last.values_at(:message, :hash, :date)
+final_result = message + " " + hash + " " + date
+return final_result
+end
+
+
def self.get(name)
taken_object = ""
@objects.select do |object|
if object[name] == name
taken_object = @objects[name]
end
end.first
if taken_object == ""
return "Object #{name} is not commited."
else
return "Found object #{name}." + " \n " + taken_object
end
end
def self.log
-
end
end
class Branch
#TODO
end

Като начало, оправи си идентацията на кода, в ужасен вид е в момента.

Относно функционалността, методите commit, add и т.н. трябва да връщат обект, който има методи message, result и прочее, а не обект от тип String (който няма въпросните методи message, result, ...)

Прегледай си грешките спрямо тестовете (пуснали сме ги в хранилището с домашните) и виж защо са.

Виж и нашето примерно решение, както и хвърли едно око на решенията на другите студенти.