Решение на Пета задача - DataModel от Петко Митков

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

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

Резултати

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

Код

class DataModel
attr_reader :id
def initialize(kwargz = {})
kwargz.each do |k, v|
if self.class.method_defined?(k)
var_name = "@#{k}"
instance_variable_set(var_name, v)
end
end
end
def self.attributes(*attrs)
if attrs.empty?
@attributes
else
attrs.each do |attr|
define_singleton_method("find_by_#{attr}") { |value| where([[attr, value]].to_h) }
attr_accessor attr
end
@attributes = attrs
@ids = {}
end
end
def self.data_store(store = nil)
store.nil? ? @data_store : @data_store = store
end
def self.where(kwargz)
found = @data_store.find(kwargz)
found.map { |k| @ids[k] }.map { |s| ObjectSpace._id2ref(s) }
end
def ==(other)
res = (other.instance_of?(self.class) && @id == other.id) ? true : false
res || (object_id == other.object_id) ? true : false
end
def save
@id = @id.nil? ? self.class.data_store.next_id : @id
hash = self.class.attributes
.map { |attr| [attr, instance_variable_get("@#{attr}")] }.to_h
self.class.data_store.create(hash)
self.class.ids[hash] = object_id
self
end
def delete
raise DeleteUnsavedRecordError if @id.nil?
self.class.data_store.delete_by_id(@id)
@id = nil
end
def self.ids
@ids.nil? ? nil : @ids
end
class DeleteUnsavedRecordError < StandardError
end
end
class ArrayStore
attr_reader :storage
def initialize
@storage = []
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def find(kwargz)
@storage.map do |val|
if val != nil
kwargz.all? { |k, v| val[k] == v } ? val : nil
end
end.compact
end
def update(id, attrs)
@storage[id - 1] == attrs
end
def delete(kwargz)
@storage.each_with_index do |value, index|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[index] = nil : nil
end
end
end
def delete_by_id(id)
@storage[id - 1] = nil
end
def next_id
@storage.size + 1
end
end
class HashStore
attr_reader :storage
def initialize
@storage = {}
end
def find(kwargz)
@storage.map do |_, entry|
if entry != nil
kwargz.all? { |k, v| entry[k] == v } ? entry : nil
end
end.compact
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def update(id, attrs)
@storage[id - 1] = attrs
end
def delete(kwargz)
@storage.each do |key, value|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[key] = nil : nil
end
end
end
def delete_by_id(id)
@storage[id - 1] = nil
end
def next_id
@storage.size + 1
end
end

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

.......FF...F....F....FF.

Failures:

  1) DataModel equality comparison compares by id if both records are saved
     Failure/Error: modified_ivan.first_name = 'Gosho'
     NoMethodError:
       undefined method `first_name=' for nil:NilClass
     # /tmp/d20161202-15620-ftsv2s/spec.rb:103: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)>'

  2) DataModel equality comparison uses #equal? if there are no ids
     Failure/Error: expect(first_user).to_not eq second_user
       
       expected: value != #<#<Class:0x007fb746eda530>:0x007fb746ed8b18 @first_name="Ivan">
            got: #<#<Class:0x007fb746eda530>:0x007fb746ed8cf8 @first_name="Ivan">
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -#<#<Class:0x007fb746eda530>:0x007fb746ed8b18 @first_name="Ivan">
       +#<#<Class:0x007fb746eda530>:0x007fb746ed8cf8 @first_name="Ivan">
     # /tmp/d20161202-15620-ftsv2s/spec.rb:112: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)>'

  3) DataModel.where raises an error if the query is by an unknown key
     Failure/Error: DataModel::UnknownAttributeError,
     NameError:
       uninitialized constant DataModel::UnknownAttributeError
     # /tmp/d20161202-15620-ftsv2s/spec.rb:144: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)>'

  4) 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-ftsv2s/spec.rb:235
     # /tmp/d20161202-15620-ftsv2s/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)>'

  5) 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-ftsv2s/spec.rb:239
     # /tmp/d20161202-15620-ftsv2s/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)>'

  6) 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-ftsv2s/spec.rb:239
     # /tmp/d20161202-15620-ftsv2s/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)>'

Finished in 0.02895 seconds
25 examples, 6 failures

Failed examples:

rspec /tmp/d20161202-15620-ftsv2s/spec.rb:92 # DataModel equality comparison compares by id if both records are saved
rspec /tmp/d20161202-15620-ftsv2s/spec.rb:108 # DataModel equality comparison uses #equal? if there are no ids
rspec /tmp/d20161202-15620-ftsv2s/spec.rb:142 # DataModel.where raises an error if the query is by an unknown key
rspec /tmp/d20161202-15620-ftsv2s/spec.rb:196 # HashStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-ftsv2s/spec.rb:196 # ArrayStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-ftsv2s/spec.rb:204 # ArrayStore behaves like a data store #update only updates records with the correct IDs

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

Петко обнови решението на 01.12.2016 22:30 (преди около 8 години)

+class DataModel
+ attr_reader :id
+
+ def initialize(kwargz = {})
+ kwargz.each do |k, v|
+ if self.class.method_defined?(k)
+ var_name = "@#{k}"
+ instance_variable_set(var_name, v)
+ end
+ end
+ end
+
+ def self.attributes(*attrs)
+ if attrs.size.empty?
+ @attributes
+ else
+ attrs.each { |attr| attr_accessor attr }
+ @attributes = attrs
+ end
+ end
+
+ def self.data_store(store = nil)
+ if store.nil?
+ @data_store
+ else
+ @data_store = store
+ end
+ end
+
+ def self.where(kwargz)
+ @storage.find(kwargz)
+ end
+
+ def ==(other)
+ other.instance_of?(self.class) && @id == other.id ? true : false
+ end
+end
+
+class ArrayStore
+ attr_reader :storage
+
+ def initialize
+ @storage = []
+ end
+
+ def create(kwargz)
+ @storage[@storage.size] = kwargz
+ end
+
+ def find(kwargz)
+ @storage.map do |val|
+ if val != nil
+ kwargz.all? { |k, v| val[k] == v } ? val : nil
+ end
+ end.compact
+ end
+
+ def update(id, attrs)
+ @storage[id - 1] == attrs
+ end
+
+ def delete(kwargz)
+ @storage.each_with_index do |value, index|
+ if value != nil
+ kwargz.all? { |k, v| value[k] == v } ? @storage[index] = nil : nil
+ end
+ end
+ end
+
+ def next_id
+ @storage.size
+ end
+end
+
+class HashStore
+ attr_reader :storage
+
+ def initialize
+ @storage = {}
+ end
+
+ def find(kwargz)
+ @storage.map do |_, entry|
+ if entry != nil
+ kwargz.all? { |k, v| entry[k] == v } ? entry : nil
+ end
+ end.compact
+ end
+
+ def create(kwargz)
+ @storage[@storage.size] = kwargz
+ end
+
+ def update(id, attrs)
+ @storage[id - 1] = attrs
+ end
+
+ def delete(kwargz)
+ @storage.each do |key, value|
+ if value != nil
+ kwargz.all? { |k, v| value[k] == v } ? @storage[key] = nil : nil
+ end
+ end
+ end
+
+ def next_id
+ @storage.size
+ end
+end

Петко обнови решението на 01.12.2016 22:38 (преди около 8 години)

class DataModel
attr_reader :id
def initialize(kwargz = {})
kwargz.each do |k, v|
if self.class.method_defined?(k)
var_name = "@#{k}"
instance_variable_set(var_name, v)
end
end
end
-
+
def self.attributes(*attrs)
- if attrs.size.empty?
+ if attrs.empty?
@attributes
else
- attrs.each { |attr| attr_accessor attr }
+ attrs.each do |attr|
+ define_method("find_by_#{attr}") { |value| @storage.find([attr, value].to_h) }
+ attr_accessor attr
+ end
@attributes = attrs
end
end
def self.data_store(store = nil)
if store.nil?
@data_store
else
@data_store = store
end
end
def self.where(kwargz)
@storage.find(kwargz)
end
def ==(other)
other.instance_of?(self.class) && @id == other.id ? true : false
+ end
+
+ def save
+
end
end
class ArrayStore
attr_reader :storage
def initialize
@storage = []
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def find(kwargz)
@storage.map do |val|
if val != nil
kwargz.all? { |k, v| val[k] == v } ? val : nil
end
end.compact
end
def update(id, attrs)
@storage[id - 1] == attrs
end
def delete(kwargz)
@storage.each_with_index do |value, index|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[index] = nil : nil
end
end
end
def next_id
@storage.size
end
end
class HashStore
attr_reader :storage
def initialize
@storage = {}
end
def find(kwargz)
@storage.map do |_, entry|
if entry != nil
kwargz.all? { |k, v| entry[k] == v } ? entry : nil
end
end.compact
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def update(id, attrs)
@storage[id - 1] = attrs
end
def delete(kwargz)
@storage.each do |key, value|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[key] = nil : nil
end
end
end
def next_id
@storage.size
end
end

Петко обнови решението на 01.12.2016 23:18 (преди около 8 години)

class DataModel
attr_reader :id
def initialize(kwargz = {})
kwargz.each do |k, v|
if self.class.method_defined?(k)
var_name = "@#{k}"
instance_variable_set(var_name, v)
end
end
end
-
+
def self.attributes(*attrs)
if attrs.empty?
@attributes
else
attrs.each do |attr|
- define_method("find_by_#{attr}") { |value| @storage.find([attr, value].to_h) }
+ name = "find_by_#{attr}"
+ define_singleton_method(name) { |value| @data_store.find([[attr, value]].to_h) }
attr_accessor attr
end
@attributes = attrs
end
end
def self.data_store(store = nil)
if store.nil?
@data_store
else
@data_store = store
end
end
def self.where(kwargz)
- @storage.find(kwargz)
+ @data_store.find(kwargz)
end
def ==(other)
other.instance_of?(self.class) && @id == other.id ? true : false
end
def save
-
+ @id = @id.nil? ? self.class.data_store.next_id : @id
+ hash = self.class.attributes
+ .map { |attr| [attr, instance_variable_get("@#{attr}")] }.to_h
+ self.class.data_store.create(hash)
+ self
end
end
class ArrayStore
attr_reader :storage
def initialize
@storage = []
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def find(kwargz)
@storage.map do |val|
if val != nil
kwargz.all? { |k, v| val[k] == v } ? val : nil
end
end.compact
end
def update(id, attrs)
@storage[id - 1] == attrs
end
def delete(kwargz)
@storage.each_with_index do |value, index|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[index] = nil : nil
end
end
end
def next_id
- @storage.size
+ @storage.size + 1
end
end
class HashStore
attr_reader :storage
def initialize
@storage = {}
end
def find(kwargz)
@storage.map do |_, entry|
if entry != nil
kwargz.all? { |k, v| entry[k] == v } ? entry : nil
end
end.compact
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def update(id, attrs)
@storage[id - 1] = attrs
end
def delete(kwargz)
@storage.each do |key, value|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[key] = nil : nil
end
end
end
def next_id
- @storage.size
+ @storage.size + 1
end
end

Петко обнови решението на 01.12.2016 23:33 (преди около 8 години)

class DataModel
attr_reader :id
def initialize(kwargz = {})
kwargz.each do |k, v|
if self.class.method_defined?(k)
var_name = "@#{k}"
instance_variable_set(var_name, v)
end
end
end
def self.attributes(*attrs)
if attrs.empty?
@attributes
else
attrs.each do |attr|
- name = "find_by_#{attr}"
- define_singleton_method(name) { |value| @data_store.find([[attr, value]].to_h) }
+ define_singleton_method("find_by_#{attr}") { |value| where([[attr, value]].to_h) }
attr_accessor attr
end
@attributes = attrs
+ @ids = {}
end
end
def self.data_store(store = nil)
if store.nil?
@data_store
else
@data_store = store
end
end
def self.where(kwargz)
- @data_store.find(kwargz)
+ found = @data_store.find(kwargz)
+ found.map { |k| @ids[k] }.map { |s| ObjectSpace._id2ref(s) }
end
def ==(other)
other.instance_of?(self.class) && @id == other.id ? true : false
end
def save
@id = @id.nil? ? self.class.data_store.next_id : @id
hash = self.class.attributes
.map { |attr| [attr, instance_variable_get("@#{attr}")] }.to_h
self.class.data_store.create(hash)
+ self.class.ids[hash] = object_id
self
+ end
+
+ def self.ids
+ @ids.nil? ? nil : @ids
end
end
class ArrayStore
attr_reader :storage
def initialize
@storage = []
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def find(kwargz)
@storage.map do |val|
if val != nil
kwargz.all? { |k, v| val[k] == v } ? val : nil
end
end.compact
end
def update(id, attrs)
@storage[id - 1] == attrs
end
def delete(kwargz)
@storage.each_with_index do |value, index|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[index] = nil : nil
end
end
end
def next_id
@storage.size + 1
end
end
class HashStore
attr_reader :storage
def initialize
@storage = {}
end
def find(kwargz)
@storage.map do |_, entry|
if entry != nil
kwargz.all? { |k, v| entry[k] == v } ? entry : nil
end
end.compact
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def update(id, attrs)
@storage[id - 1] = attrs
end
def delete(kwargz)
@storage.each do |key, value|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[key] = nil : nil
end
end
end
def next_id
@storage.size + 1
end
end

Петко обнови решението на 01.12.2016 23:47 (преди около 8 години)

class DataModel
attr_reader :id
def initialize(kwargz = {})
kwargz.each do |k, v|
if self.class.method_defined?(k)
var_name = "@#{k}"
instance_variable_set(var_name, v)
end
end
end
def self.attributes(*attrs)
if attrs.empty?
@attributes
else
attrs.each do |attr|
define_singleton_method("find_by_#{attr}") { |value| where([[attr, value]].to_h) }
attr_accessor attr
end
@attributes = attrs
@ids = {}
end
end
def self.data_store(store = nil)
- if store.nil?
- @data_store
- else
- @data_store = store
- end
+ store.nil? ? @data_store : @data_store = store
end
def self.where(kwargz)
found = @data_store.find(kwargz)
found.map { |k| @ids[k] }.map { |s| ObjectSpace._id2ref(s) }
end
def ==(other)
- other.instance_of?(self.class) && @id == other.id ? true : false
+ res = (other.instance_of?(self.class) && @id == other.id) ? true : false
+ res || (object_id == other.object_id) ? true : false
end
def save
@id = @id.nil? ? self.class.data_store.next_id : @id
hash = self.class.attributes
.map { |attr| [attr, instance_variable_get("@#{attr}")] }.to_h
self.class.data_store.create(hash)
self.class.ids[hash] = object_id
self
end
+ def delete
+ raise DeleteUnsavedRecordError if @id.nil?
+ self.class.data_store.delete_by_id(@id)
+
+ end
+
def self.ids
@ids.nil? ? nil : @ids
end
+
+ class DeleteUnsavedRecordError < StandardError
+ end
end
class ArrayStore
attr_reader :storage
def initialize
@storage = []
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def find(kwargz)
@storage.map do |val|
if val != nil
kwargz.all? { |k, v| val[k] == v } ? val : nil
end
end.compact
end
def update(id, attrs)
@storage[id - 1] == attrs
end
def delete(kwargz)
@storage.each_with_index do |value, index|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[index] = nil : nil
end
end
end
+ def delete_by_id(id)
+ @storage[id - 1] = nil
+ end
+
def next_id
@storage.size + 1
end
end
class HashStore
attr_reader :storage
def initialize
@storage = {}
end
def find(kwargz)
@storage.map do |_, entry|
if entry != nil
kwargz.all? { |k, v| entry[k] == v } ? entry : nil
end
end.compact
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def update(id, attrs)
@storage[id - 1] = attrs
end
def delete(kwargz)
@storage.each do |key, value|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[key] = nil : nil
end
end
+ end
+
+ def delete_by_id(id)
+ @storage[id - 1] = nil
end
def next_id
@storage.size + 1
end
end

Петко обнови решението на 01.12.2016 23:54 (преди около 8 години)

class DataModel
attr_reader :id
def initialize(kwargz = {})
kwargz.each do |k, v|
if self.class.method_defined?(k)
var_name = "@#{k}"
instance_variable_set(var_name, v)
end
end
end
def self.attributes(*attrs)
if attrs.empty?
@attributes
else
attrs.each do |attr|
define_singleton_method("find_by_#{attr}") { |value| where([[attr, value]].to_h) }
attr_accessor attr
end
@attributes = attrs
@ids = {}
end
end
def self.data_store(store = nil)
store.nil? ? @data_store : @data_store = store
end
def self.where(kwargz)
found = @data_store.find(kwargz)
found.map { |k| @ids[k] }.map { |s| ObjectSpace._id2ref(s) }
end
def ==(other)
res = (other.instance_of?(self.class) && @id == other.id) ? true : false
res || (object_id == other.object_id) ? true : false
end
def save
@id = @id.nil? ? self.class.data_store.next_id : @id
hash = self.class.attributes
.map { |attr| [attr, instance_variable_get("@#{attr}")] }.to_h
self.class.data_store.create(hash)
self.class.ids[hash] = object_id
self
end
def delete
raise DeleteUnsavedRecordError if @id.nil?
self.class.data_store.delete_by_id(@id)
+ @id = nil
end
def self.ids
@ids.nil? ? nil : @ids
end
class DeleteUnsavedRecordError < StandardError
end
end
class ArrayStore
attr_reader :storage
def initialize
@storage = []
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def find(kwargz)
@storage.map do |val|
if val != nil
kwargz.all? { |k, v| val[k] == v } ? val : nil
end
end.compact
end
def update(id, attrs)
@storage[id - 1] == attrs
end
def delete(kwargz)
@storage.each_with_index do |value, index|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[index] = nil : nil
end
end
end
def delete_by_id(id)
@storage[id - 1] = nil
end
def next_id
@storage.size + 1
end
end
class HashStore
attr_reader :storage
def initialize
@storage = {}
end
def find(kwargz)
@storage.map do |_, entry|
if entry != nil
kwargz.all? { |k, v| entry[k] == v } ? entry : nil
end
end.compact
end
def create(kwargz)
@storage[@storage.size] = kwargz
end
def update(id, attrs)
@storage[id - 1] = attrs
end
def delete(kwargz)
@storage.each do |key, value|
if value != nil
kwargz.all? { |k, v| value[k] == v } ? @storage[key] = nil : nil
end
end
end
def delete_by_id(id)
@storage[id - 1] = nil
end
def next_id
@storage.size + 1
end
end