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

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

Към профила на Антон Сотиров

Резултати

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

Код

class DataModel
# attr_accessor attributes
def initialize(hash = {})
end
def delete(hash)
data_store.delete(hash)
end
def save
end
def self.data_store(new_class)
@data_store = new_class.new
end
def self.attributes(*att)
@attributes = *att
end
def self.where
end
end
class ArrayStore
def initialize
@array = []
@id = 0
end
def create(hash)
@id += 1
@array.push(@id => hash.merge(id: @id))
end
def find(hash)
@array.reject { |item| item != hash }
end
def delete(hash)
@array.delete_if { |item| item == hash }
end
def update(id, hash)
end
end
class HashStore
def initialize
@hash = {}
@id = 0
end
def create(hash)
@id += 1
@hash.merge!(@id => hash.merge(id: @id))
end
def find(hash)
@hash.reject { |item| item != hash }
end
def delete(hash)
@hash = (1..@id).each { |key, _| h[key].delete_if { |k, _| k == hash } }
end
end
# not enough time for this homework due to intense week... Thats all that i got
# within the time at my disposal.

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

FFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) DataModel creates attribute accessors
     Failure/Error: data_store store
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc42912ee50 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc4291a0690 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc42918ed00 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429175350 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429130fc0 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc428f9fc60 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc428f853d8 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:78:in `block (4 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:76:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:76:in `new'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc428f721c0 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429ad4130 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429ca1e18 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429c9f4b0 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429c9c8f0 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429c95690 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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
     NoMethodError:
       undefined method `new' for #<HashStore:0x007fc429c7ec60 @hash={}, @id=0>
     # /tmp/d20161202-15620-ifx9iv/solution.rb:15:in `data_store'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `initialize'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `new'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:6:in `block (2 levels) in <top (required)>'
     # /tmp/d20161202-15620-ifx9iv/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-ifx9iv/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 #create saves a new record
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1 @@
       -[{:id=>2, :name=>"Pesho"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:235
     # /tmp/d20161202-15620-ifx9iv/spec.rb:178: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 #find can find elements by attributes
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: {}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1 @@
       -[{:id=>2, :name=>"Pesho"}]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:235
     # /tmp/d20161202-15620-ifx9iv/spec.rb:187: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) 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'})
     NoMethodError:
       undefined method `update' for #<HashStore:0x007fc429c358d0>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:235
     # /tmp/d20161202-15620-ifx9iv/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)>'

  19) HashStore behaves like a data store #update only updates records with the correct IDs
     Failure/Error: store.update(2, {id: 2, name: 'Sasho'})
     NoMethodError:
       undefined method `update' for #<HashStore:0x007fc429c2dbf8>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:235
     # /tmp/d20161202-15620-ifx9iv/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)>'

  20) HashStore behaves like a data store #delete can delete multiple records with a single query
     Failure/Error: store.delete(name: 'Pesho')
     NameError:
       undefined local variable or method `h' for #<HashStore:0x007fc429c23f18>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:235
     # /tmp/d20161202-15620-ifx9iv/solution.rb:65:in `block in delete'
     # /tmp/d20161202-15620-ifx9iv/solution.rb:65:in `each'
     # /tmp/d20161202-15620-ifx9iv/solution.rb:65:in `delete'
     # /tmp/d20161202-15620-ifx9iv/spec.rb:227: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)>'

  21) ArrayStore behaves like a data store #create saves a new record
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>2, :name=>"Pesho"}]
       +[]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:239
     # /tmp/d20161202-15620-ifx9iv/spec.rb:178: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)>'

  22) ArrayStore behaves like a data store #find can find elements by attributes
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>2, :name=>"Pesho"}]
       +[]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:239
     # /tmp/d20161202-15620-ifx9iv/spec.rb:187: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)>'

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

  24) ArrayStore behaves like a data store #update only updates records with the correct IDs
     Failure/Error: expect(store.find(id: 1)).to eq [georgi]
       
       expected: [{:id=>1, :name=>"Georgi"}]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[{:id=>1, :name=>"Georgi"}]
       +[]
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ifx9iv/spec.rb:239
     # /tmp/d20161202-15620-ifx9iv/spec.rb:212: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)>'

  25) 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-ifx9iv/spec.rb:239
     # /tmp/d20161202-15620-ifx9iv/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.01926 seconds
25 examples, 25 failures

Failed examples:

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

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

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

+class DataModel
+ # attr_accessor attributes
+ def initialize(hash = {})
+ end
+
+ def delete(hash)
+ data_store.delete(hash)
+ end
+
+ def save
+
+ end
+
+ def self.data_store(new_class)
+ @data_store = new_class.new
+ end
+
+ def self.attributes(*att)
+ @attributes = *att
+ end
+
+ def self.where
+ end
+end
+
+class ArrayStore
+ def initialize
+ @array = []
+ @id = 0
+ end
+
+ def create(hash)
+ @id += 1
+ @array.push(@id => hash.merge(id: @id))
+ end
+
+ def find(hash)
+ @array.reject { |item| item != hash }
+ end
+
+ def delete(hash)
+ @array.delete_if { |item| item == hash }
+ end
+
+ def update(id, hash)
+ end
+end
+
+class HashStore
+ def initialize
+ @hash = {}
+ @id = 0
+ end
+
+ def create(hash)
+ @id += 1
+ @hash.merge!(@id => hash.merge(id: @id))
+ end
+
+ def find(hash)
+ @hash.reject { |item| item != hash }
+ end
+
+ def delete(hash)
+ @hash = (1..@id).each { |key, _| h[key].delete_if { |k, _| k == hash } }
+
+ end
+end
+
+# not enough time for this homework due to intense week... Thats all that i got
+# within the time at my disposal.
+
+