Решение на Пета задача - DataModel от Симеон Гергинов

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

Към профила на Симеон Гергинов

Резултати

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

Код

class HashStore
attr_reader :storage
def initialize
@storage = {}
end
def create(entry)
value = entry[:id]
temp_hash = {id: value}
temp_hash.merge!(entry)
@storage[value] = temp_hash
end
def find(request)
arr = []
@storage.each do |_, values|
check = (request.to_a - values.to_a).empty?
arr << values if check
end
arr
end
def update(id, attributes)
@storage.each do |key, _|
check = (key == id)
@storage[k] = attributes if check
end
end
def delete(request)
@storage.each do |_, values|
check = (request.to_a - values.to_a).empty?
@storage.delete(values) if check
end
end
end
class ArrayStore
attr_reader :storage
def initialize
@storage = []
end
def create(entry)
@storage << entry
end
def find(request)
arr = []
@storage.each do |values|
check = (request.to_a - values.to_a).empty?
arr << values if check
end
arr
end
def update(id, attributes)
@storage.each do |value|
value.each do |k, _|
check = (k == id)
value[k] == attributes if check
end
end
end
def delete(request)
@storage.each do |value|
value.each do |_, v|
check = (v == request)
@storage.delete(value) if check
end
end
end
end
class DataModel
attr_accessor :new_hash
class << self
def getter(attribute_name)
define_method(attribute_name) do
instance_variable_get("@#{attribute_name}")
end
end
def setter(attribute_name)
define_method("#{attribute_name}=") do |value|
instance_variable_set("@#{attribute_name}", value)
end
end
def attributes(*model_attributes)
model_attributes.each do |attribute|
setter(attribute)
getter(attribute)
end
p model_attributes
end
def data_store(store)
store
end
def where(request)
data_store.find(request)
end
end
def initialize(hash = {})
@new_hash = {}
@new_hash = hash
self.class.attributes.each do |attribute|
nil unless hash[attribute]
end
end
def save
data_store.create(@new_hash)
end
def delete
data_store.delete(@new_hash)
end
end

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

[:first_name, :last_name, :age]
[]
.[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name]
[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
[]
.[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
F[:first_name, :last_name, :age]
[]
FF..FFF..FFF

Failures:

  1) DataModel has attributes and data_model getters
     Failure/Error: expect(user_model.attributes).to include :first_name
       expected [] to include :first_name
       Diff:
       @@ -1,2 +1,2 @@
       -[:first_name]
       +[]
     # /tmp/d20161202-15620-cq4xw3/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)>'

  2) DataModel accepts attributes when initializing
     Failure/Error: expect(record.first_name).to eq 'Pesho'
       
       expected: "Pesho"
            got: nil
       
       (compared using ==)
     # /tmp/d20161202-15620-cq4xw3/spec.rb:34: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 has #find_by_<attribute> methods
     Failure/Error: record.save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51cab3410>:0x007fb51cab1db8>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/spec.rb:41: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 id generation creates id on first save and does not change it
     Failure/Error: expect(record.id).to be nil
     NoMethodError:
       undefined method `id' for #<#<Class:0x007fb51ca2e648>:0x007fb51ca2d6a8>
     # /tmp/d20161202-15620-cq4xw3/spec.rb:50: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)>'

  5) DataModel id generation does not reuse ids
     Failure/Error: ivan.save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51caa0220>:0x007fb51ca96130>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/spec.rb:64: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 break when there are two models with the same store
     Failure/Error: ivan.save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51ca78158>:0x007fb51ca73bf8>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/spec.rb:82: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 equality comparison compares by id if both records are saved
     Failure/Error: ivan.save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51c8cc610>:0x007fb51c8bcf58>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/spec.rb:94: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.where finds records by attributes
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51c8807b0>:0x007fb51c87f680>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/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)>'

  9) DataModel.where finds records by multiple attributes
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51c878880>:0x007fb51c877138>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/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)>'

  10) DataModel.where returns empty collection when nothing is found
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51c873330>:0x007fb51c8719b8>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/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 raises an error if the query is by an unknown key
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51d59a4a0>:0x007fb51d599bb8>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/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#delete deletes only the record for which it is called
     Failure/Error: ivan = user_model.new(first_name: 'Ivan').save
     NameError:
       undefined local variable or method `data_store' for #<#<Class:0x007fb51d57c1f8>:0x007fb51d577040>
     # /tmp/d20161202-15620-cq4xw3/solution.rb:120:in `save'
     # /tmp/d20161202-15620-cq4xw3/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)>'

  13) DataModel#delete raises an error if the record is not saved
     Failure/Error: DataModel::DeleteUnsavedRecordError
     NameError:
       uninitialized constant DataModel::DeleteUnsavedRecordError
     # /tmp/d20161202-15620-cq4xw3/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)>'

  14) HashStore behaves like a data store #update updates the attributes of a record with a given ID
     Failure/Error: store.update(2, {id: 2, name: 'Georgi'})
     NameError:
       undefined local variable or method `k' for #<HashStore:0x007fb51d4218d0>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-cq4xw3/spec.rb:235
     # /tmp/d20161202-15620-cq4xw3/solution.rb:27:in `block in update'
     # /tmp/d20161202-15620-cq4xw3/solution.rb:25:in `each'
     # /tmp/d20161202-15620-cq4xw3/solution.rb:25:in `update'
     # /tmp/d20161202-15620-cq4xw3/spec.rb:199: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) HashStore behaves like a data store #update only updates records with the correct IDs
     Failure/Error: store.update(2, {id: 2, name: 'Sasho'})
     NameError:
       undefined local variable or method `k' for #<HashStore:0x007fb51d416638>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-cq4xw3/spec.rb:235
     # /tmp/d20161202-15620-cq4xw3/solution.rb:27:in `block in update'
     # /tmp/d20161202-15620-cq4xw3/solution.rb:25:in `each'
     # /tmp/d20161202-15620-cq4xw3/solution.rb:25:in `update'
     # /tmp/d20161202-15620-cq4xw3/spec.rb:210: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 #delete can delete multiple records with a single query
     Failure/Error: expect(store.find({})).to eq [gosho]
       
       expected: [{:id=>3, :name=>"Gosho"}]
            got: [{:id=>1, :name=>"Pesho"}, {:id=>2, :name=>"Pesho"}, {:id=>3, :name=>"Gosho"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>3, :name=>"Gosho"}]
       +[{:id=>1, :name=>"Pesho"}, {:id=>2, :name=>"Pesho"}, {:id=>3, :name=>"Gosho"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-cq4xw3/spec.rb:235
     # /tmp/d20161202-15620-cq4xw3/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)>'

  17) 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"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>2, :name=>"Georgi"}]
       +[{:id=>2, :name=>"Pesho"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-cq4xw3/spec.rb:239
     # /tmp/d20161202-15620-cq4xw3/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)>'

  18) ArrayStore behaves like a data store #update only updates records with the correct IDs
     Failure/Error: expect(store.find(id: 2)).to eq [{id: 2, name: 'Sasho'}]
       
       expected: [{:id=>2, :name=>"Sasho"}]
            got: [{:id=>2, :name=>"Pesho"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>2, :name=>"Sasho"}]
       +[{:id=>2, :name=>"Pesho"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-cq4xw3/spec.rb:239
     # /tmp/d20161202-15620-cq4xw3/spec.rb:213: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: [{:id=>1, :name=>"Pesho"}, {:id=>2, :name=>"Pesho"}, {:id=>3, :name=>"Gosho"}]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>3, :name=>"Gosho"}]
       +[{:id=>1, :name=>"Pesho"}, {:id=>2, :name=>"Pesho"}, {:id=>3, :name=>"Gosho"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-cq4xw3/spec.rb:239
     # /tmp/d20161202-15620-cq4xw3/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.0267 seconds
25 examples, 19 failures

Failed examples:

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

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

Симеон обнови решението на 01.12.2016 20:06 (преди над 7 години)

+class HashStore
+ attr_reader :storage
+
+ def initialize
+ @storage = {}
+ end
+
+ def create(entry)
+ value = entry[:id]
+ temp_hash = {id: value}
+ temp_hash.merge!(entry)
+ @storage[value] = temp_hash
+ end
+
+ def find(request)
+ arr = []
+ @storage.each do |_, values|
+ check = (request.to_a - values.to_a).empty?
+ arr << values if check
+ end
+ arr
+ end
+
+ def update(id, attributes)
+ @storage.each do |key, _|
+ check = (key == id)
+ @storage[k] = attributes if check
+ end
+ end
+
+ def delete(request)
+ @storage.each do |_, values|
+ check = (request.to_a - values.to_a).empty?
+ @storage.delete(values) if check
+ end
+ end
+end
+
+class ArrayStore
+ attr_reader :storage
+
+ def initialize
+ @storage = []
+ end
+
+ def create(entry)
+ @storage << entry
+ end
+
+ def find(request)
+ arr = []
+ @storage.each do |values|
+ check = (request.to_a - values.to_a).empty?
+ arr << values if check
+ end
+ arr
+ end
+
+ def update(id, attributes)
+ @storage.each do |value|
+ value.each do |k, _|
+ check = (k == id)
+ value[k] == attributes if check
+ end
+ end
+ end
+
+ def delete(request)
+ @storage.each do |value|
+ value.each do |_, v|
+ check = (v == request)
+ @storage.delete(value) if check
+ end
+ end
+ end
+end
+
+class DataModel
+ attr_accessor :new_hash
+
+ class << self
+ def getter(attribute_name)
+ define_method(attribute_name) do
+ instance_variable_get("@#{attribute_name}")
+ end
+ end
+
+ def setter(attribute_name)
+ define_method("#{attribute_name}=") do |value|
+ instance_variable_set("@#{attribute_name}", value)
+ end
+ end
+
+ def attributes(*model_attributes)
+ model_attributes.each do |attribute|
+ setter(attribute)
+ getter(attribute)
+ end
+ p model_attributes
+ end
+
+ def data_store(store)
+ store
+ end
+
+ def where(request)
+ data_store.find(request)
+ end
+ end
+
+ def initialize(hash = {})
+ @new_hash = {}
+ @new_hash = hash
+ self.class.attributes.each do |attribute|
+ nil unless hash[attribute]
+ end
+ end
+
+ def save
+ data_store.create(@new_hash)
+ end
+
+ def delete
+ data_store.delete(@new_hash)
+ end
+end