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

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

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

Резултати

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

Код

module Store
def initialize(storage = nil)
@id_counter = 1
@store = storage
end
def self.not_implemented(method)
raise NotImplementedError.new "method #{method} not implemented"
end
def self.hash_matches(tested, tester)
match = true
tester.each { |key, value| match &= tested[key] == value }
match
end
def storage
@store
end
def add_id_to_record(record)
id = @id_counter
@id_counter = id + 1
record.merge({id: id})
end
def create
Store.not_implemented "create"
end
def find
Store.not_implemented "find"
end
def update
Store.not_implemented "update"
end
def delete
Store.not_implementedd "delete"
end
end
class ArrayStore
include Store
def initialize
super([])
end
def create(record)
@store.push add_id_to_record(record)
end
def find(record)
@store.select { |tested| Store.hash_matches tested, record }
end
def update(id, record)
index = @store.index { |tested| tested[:id] == id }
@store[index] = @store[index].merge record
end
def delete(record)
@store.delete_if { |tested| Store.hash_matches tested, record }
end
end
class HashStore
include Store
def initialize
super {}
end
def create(record)
record_with_id = add_id_to_record record
@store[record_with_id[:id]] = record_with_id
end
def find(record)
@store.select { |_, tested| Store.hash_matches tested, record }
end
def update(id, record)
@store[id] = @store[id].merge record
end
def delete(record)
@store.delete_if { |_, tested| Store.hash_matches tested, record }
end
end
class DataModel
class<<self
attr_accessor :store_container, :attributes_container, :id
def initialize(hash)
hash.each { |key, value| instance_variable_set "@#{key}", value }
@id = hash[:id]
end
def attributes(*attrs)
return @attributes if attrs.empty?
@attributes = attrs
attrs.each do |attr|
define_method("#{attr}=") { |val| instance_variable_set "@#{attr}", val }
define_method(attr) { instance_variable_get "@#{attr}" }
define_method("find_by_#{attr}") { |v| puts attr => v }
end
end
def to_h
hash = {}
@@attributes.each { |attr| hash[attr] = instance_variable_get "@#{attr}" }
hash
end
def save
if @id == nil
@store_container.create.to_h
else
@store_container.update
end
end
def data_store(store_type)
@store_container = store_type unless store_container.nil?
@store_container
end
end
end
class User < DataModel
attributes :first_name, :last_name
data_store HashStore.new
def initialize(hash_methods = {})
end
end

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

.FFFFFFFFFFFFFFFFFFFFFF..

Failures:

  1) DataModel has attributes and data_model getters
     Failure/Error: expect(user_model.data_store).to eq data_store
     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # /tmp/d20161202-15620-ipymgn/solution.rb:125:in `data_store'
     # /tmp/d20161202-15620-ipymgn/spec.rb:24: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: record = user_model.new(
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:28:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:28:in `new'
     # /tmp/d20161202-15620-ipymgn/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)>'

  3) DataModel has #find_by_<attribute> methods
     Failure/Error: record = user_model.new(first_name: 'Ivan', last_name: 'Ivanov')
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:40:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:40:in `new'
     # /tmp/d20161202-15620-ipymgn/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)>'

  4) DataModel id generation creates id on first save and does not change it
     Failure/Error: record = user_model.new(first_name: 'Ivan', last_name: 'Ivanov')
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:49:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:49:in `new'
     # /tmp/d20161202-15620-ipymgn/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)>'

  5) DataModel id generation does not reuse ids
     Failure/Error: ivan = user_model.new(first_name: 'Ivan')
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:63:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:63:in `new'
     # /tmp/d20161202-15620-ipymgn/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)>'

  6) DataModel id generation does not break when there are two models with the same store
     Failure/Error: ivan = user_model.new(first_name: 'Ivan')
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:81:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:81:in `new'
     # /tmp/d20161202-15620-ipymgn/spec.rb:81: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 = user_model.new(first_name: 'Ivan')
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:93:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:93:in `new'
     # /tmp/d20161202-15620-ipymgn/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)>'

  8) DataModel equality comparison uses #equal? if there are no ids
     Failure/Error: first_user  = user_model.new(first_name: 'Ivan')
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:109:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:109:in `new'
     # /tmp/d20161202-15620-ipymgn/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)>'

  9) DataModel.where finds records by attributes
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `new'
     # /tmp/d20161202-15620-ipymgn/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 finds records by multiple attributes
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `new'
     # /tmp/d20161202-15620-ipymgn/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 returns empty collection when nothing is found
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `new'
     # /tmp/d20161202-15620-ipymgn/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 raises an error if the query is by an unknown key
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:119:in `new'
     # /tmp/d20161202-15620-ipymgn/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#delete deletes only the record for which it is called
     Failure/Error: ivan = user_model.new(first_name: 'Ivan').save
     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # /tmp/d20161202-15620-ipymgn/spec.rb:152:in `initialize'
     # /tmp/d20161202-15620-ipymgn/spec.rb:152:in `new'
     # /tmp/d20161202-15620-ipymgn/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)>'

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

  15) HashStore behaves like a data store #create saves a new record
     Failure/Error: store.create(user)
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ipymgn/spec.rb:235
     # /tmp/d20161202-15620-ipymgn/solution.rb:76:in `create'
     # /tmp/d20161202-15620-ipymgn/spec.rb:176: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 #find can find elements by attributes
     Failure/Error: store.create(user)
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ipymgn/spec.rb:235
     # /tmp/d20161202-15620-ipymgn/solution.rb:76:in `create'
     # /tmp/d20161202-15620-ipymgn/spec.rb:185: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 #update updates the attributes of a record with a given ID
     Failure/Error: store.create(user)
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ipymgn/spec.rb:235
     # /tmp/d20161202-15620-ipymgn/solution.rb:76:in `create'
     # /tmp/d20161202-15620-ipymgn/spec.rb:198: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 only updates records with the correct IDs
     Failure/Error: store.create(georgi)
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ipymgn/spec.rb:235
     # /tmp/d20161202-15620-ipymgn/solution.rb:76:in `create'
     # /tmp/d20161202-15620-ipymgn/spec.rb:207: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 #delete can delete multiple records with a single query
     Failure/Error: store.create(first_pesho)
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ipymgn/spec.rb:235
     # /tmp/d20161202-15620-ipymgn/solution.rb:76:in `create'
     # /tmp/d20161202-15620-ipymgn/spec.rb:223: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) 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-ipymgn/spec.rb:239
     # /tmp/d20161202-15620-ipymgn/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)>'

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

  22) ArrayStore 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'})
     TypeError:
       no implicit conversion from nil to integer
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-ipymgn/spec.rb:239
     # /tmp/d20161202-15620-ipymgn/solution.rb:60:in `[]'
     # /tmp/d20161202-15620-ipymgn/solution.rb:60:in `update'
     # /tmp/d20161202-15620-ipymgn/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)>'

Finished in 0.01931 seconds
25 examples, 22 failures

Failed examples:

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

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

Здравко обнови решението на 01.12.2016 20:52 (преди около 8 години)

+module Store
+ def initialize(storage)
+ @id_counter = 1
+ @store = storage
+ end
+
+ def self.not_implemented(method)
+ raise NotImplementedError.new "method #{method} not implemented"
+ end
+
+ def self.hash_matches(tested, tester)
+ match = true
+ tester.each { |key, value| match &= tested[key] == value }
+ match
+ end
+
+ def storage
+ @store
+ end
+
+ def add_id_to_record(record)
+ id = @id_counter
+ @id_counter = id + 1
+ record.merge({id: id})
+ end
+
+ def create
+ Store.not_implemented "create"
+ end
+
+ def find
+ Store.not_implemented "find"
+ end
+
+ def update
+ Store.not_implemented "update"
+ end
+
+ def delete
+ Store.not_implementedd "delete"
+ end
+end
+
+class ArrayStore
+ include Store
+ def initialize
+ super([])
+ end
+
+ def create(record)
+ @store.push add_id_to_record(record)
+ end
+
+ def find(record)
+ @store.select { |tested| Store.hash_matches tested, record }
+ end
+
+ def update(id, record)
+ index = @store.index { |tested| tested[:id] == id }
+ @store[index] = @store[index].merge record
+ end
+
+ def delete(record)
+ @store.delete_if { |tested| Store.hash_matches tested, record }
+ end
+end
+
+class HashStore
+ include Store
+ def initialize
+ super {}
+ end
+
+ def create(record)
+ record_with_id = add_id_to_record record
+ @store[record_with_id[:id]] = record_with_id
+ end
+
+ def find(record)
+ @store.select { |_, tested| Store.hash_matches tested, record }
+ end
+
+ def update(id, record)
+ @store[id] = @store[id].merge record
+ end
+
+ def delete(record)
+ @store.delete_if { |_, tested| Store.hash_matches tested, record }
+ end
+end

Здравко обнови решението на 01.12.2016 21:09 (преди около 8 години)

module Store
- def initialize(storage)
+ def initialize(storage = nil)
@id_counter = 1
@store = storage
end
def self.not_implemented(method)
raise NotImplementedError.new "method #{method} not implemented"
end
def self.hash_matches(tested, tester)
match = true
tester.each { |key, value| match &= tested[key] == value }
match
end
def storage
@store
end
def add_id_to_record(record)
id = @id_counter
@id_counter = id + 1
record.merge({id: id})
end
def create
Store.not_implemented "create"
end
def find
Store.not_implemented "find"
end
def update
Store.not_implemented "update"
end
def delete
Store.not_implementedd "delete"
end
end
class ArrayStore
include Store
def initialize
super([])
end
def create(record)
@store.push add_id_to_record(record)
end
def find(record)
@store.select { |tested| Store.hash_matches tested, record }
end
def update(id, record)
index = @store.index { |tested| tested[:id] == id }
@store[index] = @store[index].merge record
end
def delete(record)
@store.delete_if { |tested| Store.hash_matches tested, record }
end
end
class HashStore
include Store
def initialize
super {}
end
def create(record)
record_with_id = add_id_to_record record
@store[record_with_id[:id]] = record_with_id
end
def find(record)
@store.select { |_, tested| Store.hash_matches tested, record }
end
def update(id, record)
@store[id] = @store[id].merge record
end
def delete(record)
@store.delete_if { |_, tested| Store.hash_matches tested, record }
end
-end
+end
+
+class DataModel
+ class<<self
+ attr_accessor :store_container
+ attr_reader :attributes_container
+
+ def attributes(*attrs)
+ if attrs.empty?
+ @attributes_container.map { |key| key.to_sym }
+ else
+ @attributes_container = attrs
+ end
+ end
+
+ def data_store(store_type)
+ @store_container = store_type unless store_container.nil?
+ @store_container
+ end
+
+ def print
+ @store_container
+ end
+
+ def safe
+
+ end
+
+ def delete
+
+ end
+ end
+end

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

module Store
def initialize(storage = nil)
@id_counter = 1
@store = storage
end
def self.not_implemented(method)
raise NotImplementedError.new "method #{method} not implemented"
end
def self.hash_matches(tested, tester)
match = true
tester.each { |key, value| match &= tested[key] == value }
match
end
def storage
@store
end
def add_id_to_record(record)
id = @id_counter
@id_counter = id + 1
record.merge({id: id})
end
def create
Store.not_implemented "create"
end
def find
Store.not_implemented "find"
end
def update
Store.not_implemented "update"
end
def delete
Store.not_implementedd "delete"
end
end
class ArrayStore
include Store
def initialize
super([])
end
def create(record)
@store.push add_id_to_record(record)
end
def find(record)
@store.select { |tested| Store.hash_matches tested, record }
end
def update(id, record)
index = @store.index { |tested| tested[:id] == id }
@store[index] = @store[index].merge record
end
def delete(record)
@store.delete_if { |tested| Store.hash_matches tested, record }
end
end
class HashStore
include Store
def initialize
super {}
end
def create(record)
record_with_id = add_id_to_record record
@store[record_with_id[:id]] = record_with_id
end
def find(record)
@store.select { |_, tested| Store.hash_matches tested, record }
end
def update(id, record)
@store[id] = @store[id].merge record
end
def delete(record)
@store.delete_if { |_, tested| Store.hash_matches tested, record }
end
end
class DataModel
class<<self
- attr_accessor :store_container
- attr_reader :attributes_container
+ attr_accessor :store_container, :attributes_container
+ def initialize
+ @attributes_stuff = {}
+ end
+
def attributes(*attrs)
if attrs.empty?
@attributes_container.map { |key| key.to_sym }
else
@attributes_container = attrs
end
+ @attributes_container.each do |sym|
+ define_method(sym.to_sym) { @attributes_stuff[sym.to_sym] }
+ define_method("#{sym}=".to_sym) { |v| @attributes_stuff[sym.to_sym] = v }
+ end
end
def data_store(store_type)
@store_container = store_type unless store_container.nil?
@store_container
end
def print
@store_container
end
+ def printf
+ @attributes_container
+ end
+
def safe
end
def delete
end
end
end
+
+class User < DataModel
+ attributes :first_name, :last_name
+ data_store HashStore.new
+
+ def initialize(hash_methods = {})
+ end
+end

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

module Store
def initialize(storage = nil)
@id_counter = 1
@store = storage
end
def self.not_implemented(method)
raise NotImplementedError.new "method #{method} not implemented"
end
def self.hash_matches(tested, tester)
match = true
tester.each { |key, value| match &= tested[key] == value }
match
end
def storage
@store
end
def add_id_to_record(record)
id = @id_counter
@id_counter = id + 1
record.merge({id: id})
end
def create
Store.not_implemented "create"
end
def find
Store.not_implemented "find"
end
def update
Store.not_implemented "update"
end
def delete
Store.not_implementedd "delete"
end
end
class ArrayStore
include Store
def initialize
super([])
end
def create(record)
@store.push add_id_to_record(record)
end
def find(record)
@store.select { |tested| Store.hash_matches tested, record }
end
def update(id, record)
index = @store.index { |tested| tested[:id] == id }
@store[index] = @store[index].merge record
end
def delete(record)
@store.delete_if { |tested| Store.hash_matches tested, record }
end
end
class HashStore
include Store
def initialize
super {}
end
def create(record)
record_with_id = add_id_to_record record
@store[record_with_id[:id]] = record_with_id
end
def find(record)
@store.select { |_, tested| Store.hash_matches tested, record }
end
def update(id, record)
@store[id] = @store[id].merge record
end
def delete(record)
@store.delete_if { |_, tested| Store.hash_matches tested, record }
end
end
class DataModel
class<<self
attr_accessor :store_container, :attributes_container
def initialize
@attributes_stuff = {}
end
def attributes(*attrs)
- if attrs.empty?
- @attributes_container.map { |key| key.to_sym }
- else
- @attributes_container = attrs
- end
- @attributes_container.each do |sym|
- define_method(sym.to_sym) { @attributes_stuff[sym.to_sym] }
- define_method("#{sym}=".to_sym) { |v| @attributes_stuff[sym.to_sym] = v }
+ attrs.each do |attr|
+ define_method("#{attr}=") { |val| instance_variable_set "@#{attr}", val }
+ define_method(attr) { instance_variable_get "@#{attr}" }
+ define_method("find_by_#{attr}") { |val| puts attr => val }
end
end
def data_store(store_type)
@store_container = store_type unless store_container.nil?
@store_container
end
def print
@store_container
end
def printf
@attributes_container
end
def safe
end
def delete
end
end
end
class User < DataModel
attributes :first_name, :last_name
data_store HashStore.new
def initialize(hash_methods = {})
end
end

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

module Store
def initialize(storage = nil)
@id_counter = 1
@store = storage
end
def self.not_implemented(method)
raise NotImplementedError.new "method #{method} not implemented"
end
def self.hash_matches(tested, tester)
match = true
tester.each { |key, value| match &= tested[key] == value }
match
end
def storage
@store
end
def add_id_to_record(record)
id = @id_counter
@id_counter = id + 1
record.merge({id: id})
end
def create
Store.not_implemented "create"
end
def find
Store.not_implemented "find"
end
def update
Store.not_implemented "update"
end
def delete
Store.not_implementedd "delete"
end
end
class ArrayStore
include Store
def initialize
super([])
end
def create(record)
@store.push add_id_to_record(record)
end
def find(record)
@store.select { |tested| Store.hash_matches tested, record }
end
def update(id, record)
index = @store.index { |tested| tested[:id] == id }
@store[index] = @store[index].merge record
end
def delete(record)
@store.delete_if { |tested| Store.hash_matches tested, record }
end
end
class HashStore
include Store
def initialize
super {}
end
def create(record)
record_with_id = add_id_to_record record
@store[record_with_id[:id]] = record_with_id
end
def find(record)
@store.select { |_, tested| Store.hash_matches tested, record }
end
def update(id, record)
@store[id] = @store[id].merge record
end
def delete(record)
@store.delete_if { |_, tested| Store.hash_matches tested, record }
end
end
class DataModel
class<<self
- attr_accessor :store_container, :attributes_container
+ attr_accessor :store_container, :attributes_container, :id
- def initialize
- @attributes_stuff = {}
+ def initialize(hash)
+ hash.each { |key, value| instance_variable_set "@#{key}", value }
+ @id = hash[:id]
end
def attributes(*attrs)
+ return @attributes if attrs.empty?
+ @attributes = attrs
attrs.each do |attr|
define_method("#{attr}=") { |val| instance_variable_set "@#{attr}", val }
define_method(attr) { instance_variable_get "@#{attr}" }
- define_method("find_by_#{attr}") { |val| puts attr => val }
+ define_method("find_by_#{attr}") { |v| puts attr => v }
end
end
- def data_store(store_type)
- @store_container = store_type unless store_container.nil?
- @store_container
+ def to_h
+ hash = {}
+ @@attributes.each { |attr| hash[attr] = instance_variable_get "@#{attr}" }
+ hash
end
- def print
- @store_container
+ def save
+ if @id == nil
+ puts "create"
+ else
+ puts "update", @id, to_h
+ end
end
- def printf
- @attributes_container
+ def data_store(store_type)
+ @store_container = store_type unless store_container.nil?
+ @store_container
end
def safe
end
def delete
end
end
end
class User < DataModel
attributes :first_name, :last_name
data_store HashStore.new
def initialize(hash_methods = {})
end
-end
+end

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

module Store
def initialize(storage = nil)
@id_counter = 1
@store = storage
end
def self.not_implemented(method)
raise NotImplementedError.new "method #{method} not implemented"
end
def self.hash_matches(tested, tester)
match = true
tester.each { |key, value| match &= tested[key] == value }
match
end
def storage
@store
end
def add_id_to_record(record)
id = @id_counter
@id_counter = id + 1
record.merge({id: id})
end
def create
Store.not_implemented "create"
end
def find
Store.not_implemented "find"
end
def update
Store.not_implemented "update"
end
def delete
Store.not_implementedd "delete"
end
end
class ArrayStore
include Store
def initialize
super([])
end
def create(record)
@store.push add_id_to_record(record)
end
def find(record)
@store.select { |tested| Store.hash_matches tested, record }
end
def update(id, record)
index = @store.index { |tested| tested[:id] == id }
@store[index] = @store[index].merge record
end
def delete(record)
@store.delete_if { |tested| Store.hash_matches tested, record }
end
end
class HashStore
include Store
def initialize
super {}
end
def create(record)
record_with_id = add_id_to_record record
@store[record_with_id[:id]] = record_with_id
end
def find(record)
@store.select { |_, tested| Store.hash_matches tested, record }
end
def update(id, record)
@store[id] = @store[id].merge record
end
def delete(record)
@store.delete_if { |_, tested| Store.hash_matches tested, record }
end
end
class DataModel
class<<self
attr_accessor :store_container, :attributes_container, :id
def initialize(hash)
hash.each { |key, value| instance_variable_set "@#{key}", value }
@id = hash[:id]
end
def attributes(*attrs)
return @attributes if attrs.empty?
@attributes = attrs
attrs.each do |attr|
define_method("#{attr}=") { |val| instance_variable_set "@#{attr}", val }
define_method(attr) { instance_variable_get "@#{attr}" }
define_method("find_by_#{attr}") { |v| puts attr => v }
end
end
def to_h
hash = {}
@@attributes.each { |attr| hash[attr] = instance_variable_get "@#{attr}" }
hash
end
def save
if @id == nil
- puts "create"
+ @store_container.create.to_h
else
puts "update", @id, to_h
end
end
def data_store(store_type)
@store_container = store_type unless store_container.nil?
@store_container
- end
-
- def safe
-
- end
-
- def delete
-
end
end
end
class User < DataModel
attributes :first_name, :last_name
data_store HashStore.new
def initialize(hash_methods = {})
end
end

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

module Store
def initialize(storage = nil)
@id_counter = 1
@store = storage
end
def self.not_implemented(method)
raise NotImplementedError.new "method #{method} not implemented"
end
def self.hash_matches(tested, tester)
match = true
tester.each { |key, value| match &= tested[key] == value }
match
end
def storage
@store
end
def add_id_to_record(record)
id = @id_counter
@id_counter = id + 1
record.merge({id: id})
end
def create
Store.not_implemented "create"
end
def find
Store.not_implemented "find"
end
def update
Store.not_implemented "update"
end
def delete
Store.not_implementedd "delete"
end
end
class ArrayStore
include Store
def initialize
super([])
end
def create(record)
@store.push add_id_to_record(record)
end
def find(record)
@store.select { |tested| Store.hash_matches tested, record }
end
def update(id, record)
index = @store.index { |tested| tested[:id] == id }
@store[index] = @store[index].merge record
end
def delete(record)
@store.delete_if { |tested| Store.hash_matches tested, record }
end
end
class HashStore
include Store
def initialize
super {}
end
def create(record)
record_with_id = add_id_to_record record
@store[record_with_id[:id]] = record_with_id
end
def find(record)
@store.select { |_, tested| Store.hash_matches tested, record }
end
def update(id, record)
@store[id] = @store[id].merge record
end
def delete(record)
@store.delete_if { |_, tested| Store.hash_matches tested, record }
end
end
class DataModel
class<<self
attr_accessor :store_container, :attributes_container, :id
def initialize(hash)
hash.each { |key, value| instance_variable_set "@#{key}", value }
@id = hash[:id]
end
def attributes(*attrs)
return @attributes if attrs.empty?
@attributes = attrs
attrs.each do |attr|
define_method("#{attr}=") { |val| instance_variable_set "@#{attr}", val }
define_method(attr) { instance_variable_get "@#{attr}" }
define_method("find_by_#{attr}") { |v| puts attr => v }
end
end
def to_h
hash = {}
@@attributes.each { |attr| hash[attr] = instance_variable_get "@#{attr}" }
hash
end
def save
if @id == nil
@store_container.create.to_h
else
- puts "update", @id, to_h
+ @store_container.update
end
end
def data_store(store_type)
@store_container = store_type unless store_container.nil?
@store_container
end
end
end
class User < DataModel
attributes :first_name, :last_name
data_store HashStore.new
def initialize(hash_methods = {})
end
end