Решение на Пета задача - DataModel от Дарин Тодоров

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

Към профила на Дарин Тодоров

Резултати

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

Код

class DataModel
def initialize(hash = {})
@attributes = hash
@data_store
@id
end
class << self
def attributes(*args)
@attributes = args.to_a.map { |item| item.to_sym }
end
def data_store
end
def where
end
end
def save
end
def delete
end
end
class DataStore
attr_reader :storage
def initialize
@storage
end
def create
end
def find
end
def update
end
def delete
end
end
class ArrayStore < DataStore
attr_reader :id
def initialize
@storage = []
@id = 1
end
def create(record)
@storage[@id] = record
@id += 1
end
def find(request)
finded = []
request1 = request.to_a
new_storage = @storage.map { |item| item.to_a }
new_storage.each do |item|
finded.push(item.to_h) if (item & request1) != []
end
finded
end
def update(id_num, new_record)
@storage[id_num] = new_record
end
def delete(request)
@storage.delete(request)
end
end
class HashStore < DataStore
attr_reader :storage, :id
def initialize
@storage = {}
@id = 1
end
def create(record)
@storage[@id] = record
@id += 1
end
def find(request)
finded = []
request1 = request.to_a
new_storage = @storage.each_value.map { |hash| hash.to_a }
new_storage.each do |item|
finded.push(item.to_h) if (item & request1) != []
end
finded
end
def update(id_num, new_record)
@storage[id_num] = new_record
end
def delete(request)
@storage.delete_if { |_, value| value == request }
end
end

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

FFFFFFFFFFFFFFF..F.F..F.F

Failures:

  1) DataModel creates attribute accessors
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:13:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  2) DataModel has attributes and data_model getters
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:22:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  3) DataModel accepts attributes when initializing
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:28:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  4) DataModel has #find_by_<attribute> methods
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:40:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  5) DataModel id generation creates id on first save and does not change it
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:49:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  6) DataModel id generation does not reuse ids
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:63:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  7) DataModel id generation does not break when there are two models with the same store
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:78:in `block (4 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:76:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:76:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:76:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  8) DataModel equality comparison compares by id if both records are saved
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:93:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  9) DataModel equality comparison uses #equal? if there are no ids
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:109:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  10) DataModel.where finds records by attributes
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  11) DataModel.where finds records by multiple attributes
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  12) DataModel.where returns empty collection when nothing is found
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  13) DataModel.where raises an error if the query is by an unknown key
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  14) DataModel#delete deletes only the record for which it is called
     Failure/Error: data_store store
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-om7hh0/solution.rb:12:in `data_store'
     # /tmp/d20161202-15620-om7hh0/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `new'
     # /tmp/d20161202-15620-om7hh0/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-om7hh0/spec.rb:152:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  15) DataModel#delete raises an error if the record is not saved
     Failure/Error: DataModel::DeleteUnsavedRecordError
     NameError:
       uninitialized constant DataModel::DeleteUnsavedRecordError
     # /tmp/d20161202-15620-om7hh0/spec.rb:164:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  16) HashStore behaves like a data store #update updates the attributes of a record with a given ID
     Failure/Error: expect(store.find(id: 2)).to eq [{id: 2, name: 'Georgi'}]
       
       expected: [{:id=>2, :name=>"Georgi"}]
            got: [{:id=>2, :name=>"Pesho"}, {:id=>2, :name=>"Georgi"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>2, :name=>"Georgi"}]
       +[{:id=>2, :name=>"Pesho"}, {:id=>2, :name=>"Georgi"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-om7hh0/spec.rb:235
     # /tmp/d20161202-15620-om7hh0/spec.rb:201:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  17) HashStore behaves like a data store #delete can delete multiple records with a single query
     Failure/Error: expect(store.find({})).to eq [gosho]
       
       expected: [{:id=>3, :name=>"Gosho"}]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>3, :name=>"Gosho"}]
       +[]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-om7hh0/spec.rb:235
     # /tmp/d20161202-15620-om7hh0/spec.rb:229:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  18) ArrayStore behaves like a data store #update updates the attributes of a record with a given ID
     Failure/Error: expect(store.find(id: 2)).to eq [{id: 2, name: 'Georgi'}]
       
       expected: [{:id=>2, :name=>"Georgi"}]
            got: [{:id=>2, :name=>"Pesho"}, {:id=>2, :name=>"Georgi"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>2, :name=>"Georgi"}]
       +[{:id=>2, :name=>"Pesho"}, {:id=>2, :name=>"Georgi"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-om7hh0/spec.rb:239
     # /tmp/d20161202-15620-om7hh0/spec.rb:201:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  19) ArrayStore behaves like a data store #delete can delete multiple records with a single query
     Failure/Error: expect(store.find({})).to eq [gosho]
       
       expected: [{:id=>3, :name=>"Gosho"}]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>3, :name=>"Gosho"}]
       +[]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-om7hh0/spec.rb:239
     # /tmp/d20161202-15620-om7hh0/spec.rb:229:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.02351 seconds
25 examples, 19 failures

Failed examples:

rspec /tmp/d20161202-15620-om7hh0/spec.rb:12 # DataModel creates attribute accessors
rspec /tmp/d20161202-15620-om7hh0/spec.rb:21 # DataModel has attributes and data_model getters
rspec /tmp/d20161202-15620-om7hh0/spec.rb:27 # DataModel accepts attributes when initializing
rspec /tmp/d20161202-15620-om7hh0/spec.rb:39 # DataModel has #find_by_<attribute> methods
rspec /tmp/d20161202-15620-om7hh0/spec.rb:48 # DataModel id generation creates id on first save and does not change it
rspec /tmp/d20161202-15620-om7hh0/spec.rb:62 # DataModel id generation does not reuse ids
rspec /tmp/d20161202-15620-om7hh0/spec.rb:74 # DataModel id generation does not break when there are two models with the same store
rspec /tmp/d20161202-15620-om7hh0/spec.rb:92 # DataModel equality comparison compares by id if both records are saved
rspec /tmp/d20161202-15620-om7hh0/spec.rb:108 # DataModel equality comparison uses #equal? if there are no ids
rspec /tmp/d20161202-15620-om7hh0/spec.rb:124 # DataModel.where finds records by attributes
rspec /tmp/d20161202-15620-om7hh0/spec.rb:129 # DataModel.where finds records by multiple attributes
rspec /tmp/d20161202-15620-om7hh0/spec.rb:138 # DataModel.where returns empty collection when nothing is found
rspec /tmp/d20161202-15620-om7hh0/spec.rb:142 # DataModel.where raises an error if the query is by an unknown key
rspec /tmp/d20161202-15620-om7hh0/spec.rb:151 # DataModel#delete deletes only the record for which it is called
rspec /tmp/d20161202-15620-om7hh0/spec.rb:162 # DataModel#delete raises an error if the record is not saved
rspec /tmp/d20161202-15620-om7hh0/spec.rb:196 # HashStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-om7hh0/spec.rb:218 # HashStore behaves like a data store #delete can delete multiple records with a single query
rspec /tmp/d20161202-15620-om7hh0/spec.rb:196 # ArrayStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-om7hh0/spec.rb:218 # ArrayStore behaves like a data store #delete can delete multiple records with a single query

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

Дарин обнови решението на 01.12.2016 23:40 (преди над 7 години)

+class DataModel
+ def initialize(hash = {})
+ @attributes = hash
+ @data_store
+ @id
+ end
+ class << self
+ def attributes(*args)
+ @attributes = args.to_a.map { |item| item.to_sym }
+ end
+
+ def data_store
+ end
+
+ def where
+ end
+ end
+
+ def save
+ end
+
+ def delete
+ end
+end
+
+class DataStore
+ attr_reader :storage
+
+ def initialize
+ @storage
+ end
+
+ def create
+ end
+
+ def find
+ end
+
+ def update
+ end
+
+ def delete
+ end
+end
+
+class ArrayStore < DataStore
+ attr_reader :id
+
+ def initialize
+ @storage = []
+ @id = 1
+ end
+
+ def create(record)
+ @storage[@id] = record
+ @id += 1
+ end
+
+ def find(request)
+ finded = []
+ request1 = request.to_a
+ new_storage = @storage.map { |item| item.to_a }
+ new_storage.each do |item|
+ finded.push(item.to_h) if (item & request1) != []
+ end
+ finded
+ end
+
+ def update(id_num, new_record)
+ @storage[id_num] = new_record
+ end
+
+ def delete(request)
+ @storage.delete(request)
+ end
+end
+
+class HashStore < DataStore
+ attr_reader :storage, :id
+ def initialize
+ @storage = {}
+ @id = 1
+ end
+
+ def create(record)
+ @storage[@id] = record
+ @id += 1
+ end
+
+ def find(request)
+ finded = []
+ request1 = request.to_a
+ new_storage = @storage.each_value.map { |hash| hash.to_a }
+ new_storage.each do |item|
+ finded.push(item.to_h) if (item & request1) != []
+ end
+ finded
+ end
+
+ def update(id_num, new_record)
+ @storage[id_num] = new_record
+ end
+
+ def delete(request)
+ @storage.delete_if { |_, value| value == request }
+ end
+end