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

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

Към профила на Ралица Дарджонова

Резултати

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

Код

module InstanceMethods
def save
if self.id == nil
self.id = self.storage.smallest_available
self.storage.create(@attributes)
else
self.storage.update(id, @attributes)
end
end
def delete
if self.id == nil
raise DeleteUnsavedRecordError
else
id_to_delete = {id: @attributes[:id]}
self.storage.delete(id_to_delete)
self.id = nil
end
end
def ==(other)
if self.class == other.class
if self.id != nil && other.id != nil && self.id == other.id
true
else
self.equal?(other)
end
else
false
end
end
def all_attributes
self.class.all_attributes
end
def storage
self.class.storage
end
end
class DataModel
include InstanceMethods
def initialize(hash = {})
@attributes = {}
self.all_attributes.each do |key|
@attributes[key] = hash[key]
end
@attributes[:id] = nil
end
class << self
attr_accessor :all_attributes, :storage
def attributes(*args)
self.all_attributes = []
if !args.empty?
(args << :id ).uniq.each do |attribute|
if attribute.is_a? Symbol
self.all_attributes << attribute
make_setters_and_getters attribute
end
end
else self.all_attributes
end
end
def make_setters_and_getters(arg)
define_method arg do
@attributes[arg]
end
define_method "#{arg}=" do |value|
@attributes[arg] = value
end
define_singleton_method "find_by_#{arg}" do |value|
where(arg.to_sym => value)
end
end
def data_store(new_data_store = nil)
if new_data_store != nil
self.storage = new_data_store
else self.storage
end
end
def where(check_hash)
check_hash.keys.each do |key|
raise UnknownAttributeError unless all_attributes.include? key
end
self.storage.find(check_hash).map do |record|
obj = User.new(record)
obj.id = record[:id]
obj
end
end
end
class DeleteUnsavedRecordError < StandardError
end
class UnknownAttributeError < StandardError
end
end
module CommonMethodsStore
def create(new_record)
id = smallest_available
@storage[id] = new_record
end
def update(id, update_hash)
if id == nil
create(update_hash)
else
update_hash.keys.each do |key|
@storage[id][key] = update_hash[key]
end
end
end
attr_reader :storage
def smallest_available
range = 1..@storage.length + 1
range.each do |id|
return id if @storage[id] == nil
end
end
end
class HashStore
include CommonMethodsStore
def initialize
@storage = {}
end
def delete(check_hash)
@storage.each do |id_record, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
@storage.delete id_record if flag == 1
end
end
def find(check_hash)
compatible = []
@storage.each do |_, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
compatible << record if flag == 1
end
compatible
end
end
class ArrayStore
include CommonMethodsStore
def initialize
@storage = [{}]
end
def delete(check_hash)
@storage.each do |id_record, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
@storage.delete_at id_record if flag == 1
end
end
def find(check_hash)
compatible = []
@storage.each do |record|
flag = 0
if record != nil
check_hash.keys.each { |key| flag = 1 if record[key] == check_hash[key] }
end
compatible << record if flag == 1
end
compatible
end
end

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

.F.F.F.F.FF.FFF..F....F.F

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-4on9os/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 has #find_by_<attribute> methods
     Failure/Error: expect(user_model.find_by_first_name('Ivan').map(&:id)).to eq [record.id]
     NameError:
       uninitialized constant #<Class:DataModel>::User
     # /tmp/d20161202-15620-4on9os/solution.rb:94:in `block in where'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `map'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `where'
     # /tmp/d20161202-15620-4on9os/solution.rb:78:in `block in make_setters_and_getters'
     # /tmp/d20161202-15620-4on9os/spec.rb:43: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 id generation does not reuse ids
     Failure/Error: expect(georgi.id).to eq 2
       
       expected: 2
            got: 1
       
       (compared using ==)
     # /tmp/d20161202-15620-4on9os/spec.rb:71: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) DataModel equality comparison compares by id if both records are saved
     Failure/Error: modified_ivan = user_model.where(id: ivan.id).first
     NameError:
       uninitialized constant #<Class:DataModel>::User
     # /tmp/d20161202-15620-4on9os/solution.rb:94:in `block in where'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `map'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `where'
     # /tmp/d20161202-15620-4on9os/spec.rb:102: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.where finds records by attributes
     Failure/Error: records = user_model.where(first_name: 'Ivan').map(&:last_name)
     NameError:
       uninitialized constant #<Class:DataModel>::User
     # /tmp/d20161202-15620-4on9os/solution.rb:94:in `block in where'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `map'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `where'
     # /tmp/d20161202-15620-4on9os/spec.rb:125: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.where finds records by multiple attributes
     Failure/Error: records = user_model.where(
     NameError:
       uninitialized constant #<Class:DataModel>::User
     # /tmp/d20161202-15620-4on9os/solution.rb:94:in `block in where'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `map'
     # /tmp/d20161202-15620-4on9os/solution.rb:93:in `where'
     # /tmp/d20161202-15620-4on9os/spec.rb:130: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.where raises an error if the query is by an unknown key
     Failure/Error: expect { user_model.where(middle_name: 'Ivanov') }.to raise_error(
       expected DataModel::UnknownAttributeError with "Unknown attribute middle_name", got #<DataModel::UnknownAttributeError: DataModel::UnknownAttributeError> with backtrace:
         # /tmp/d20161202-15620-4on9os/solution.rb:91:in `block in where'
         # /tmp/d20161202-15620-4on9os/solution.rb:90:in `each'
         # /tmp/d20161202-15620-4on9os/solution.rb:90:in `where'
         # /tmp/d20161202-15620-4on9os/spec.rb:143:in `block (4 levels) in <top (required)>'
         # /tmp/d20161202-15620-4on9os/spec.rb:143: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)>'
     # /tmp/d20161202-15620-4on9os/spec.rb:143: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#delete deletes only the record for which it is called
     Failure/Error: ivan.delete
     ArgumentError:
       wrong number of arguments (given 0, expected 1)
     # /tmp/d20161202-15620-4on9os/spec.rb:156:in `delete'
     # /tmp/d20161202-15620-4on9os/spec.rb:156: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#delete raises an error if the record is not saved
     Failure/Error: expect { user_model.new(first_name: 'Ivan').delete }.to raise_error(
       expected DataModel::DeleteUnsavedRecordError, got #<NameError: uninitialized constant InstanceMethods::DeleteUnsavedRecordError> with backtrace:
         # /tmp/d20161202-15620-4on9os/solution.rb:13:in `delete'
         # /tmp/d20161202-15620-4on9os/spec.rb:163:in `block (4 levels) in <top (required)>'
         # /tmp/d20161202-15620-4on9os/spec.rb:163: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)>'
     # /tmp/d20161202-15620-4on9os/spec.rb:163: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) 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 `[]=' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-4on9os/spec.rb:235
     # /tmp/d20161202-15620-4on9os/solution.rb:118:in `block in update'
     # /tmp/d20161202-15620-4on9os/solution.rb:117:in `each'
     # /tmp/d20161202-15620-4on9os/solution.rb:117:in `update'
     # /tmp/d20161202-15620-4on9os/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)>'

  11) 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'})
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-4on9os/spec.rb:239
     # /tmp/d20161202-15620-4on9os/solution.rb:118:in `block in update'
     # /tmp/d20161202-15620-4on9os/solution.rb:117:in `each'
     # /tmp/d20161202-15620-4on9os/solution.rb:117:in `update'
     # /tmp/d20161202-15620-4on9os/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)>'

  12) ArrayStore behaves like a data store #delete can delete multiple records with a single query
     Failure/Error: store.delete(name: 'Pesho')
     NoMethodError:
       undefined method `[]' for nil:NilClass
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-4on9os/spec.rb:239
     # /tmp/d20161202-15620-4on9os/solution.rb:172:in `block (2 levels) in delete'
     # /tmp/d20161202-15620-4on9os/solution.rb:171:in `each'
     # /tmp/d20161202-15620-4on9os/solution.rb:171:in `block in delete'
     # /tmp/d20161202-15620-4on9os/solution.rb:169:in `each'
     # /tmp/d20161202-15620-4on9os/solution.rb:169:in `delete'
     # /tmp/d20161202-15620-4on9os/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)>'

Finished in 0.0341 seconds
25 examples, 12 failures

Failed examples:

rspec /tmp/d20161202-15620-4on9os/spec.rb:21 # DataModel has attributes and data_model getters
rspec /tmp/d20161202-15620-4on9os/spec.rb:39 # DataModel has #find_by_<attribute> methods
rspec /tmp/d20161202-15620-4on9os/spec.rb:62 # DataModel id generation does not reuse ids
rspec /tmp/d20161202-15620-4on9os/spec.rb:92 # DataModel equality comparison compares by id if both records are saved
rspec /tmp/d20161202-15620-4on9os/spec.rb:124 # DataModel.where finds records by attributes
rspec /tmp/d20161202-15620-4on9os/spec.rb:129 # DataModel.where finds records by multiple attributes
rspec /tmp/d20161202-15620-4on9os/spec.rb:142 # DataModel.where raises an error if the query is by an unknown key
rspec /tmp/d20161202-15620-4on9os/spec.rb:151 # DataModel#delete deletes only the record for which it is called
rspec /tmp/d20161202-15620-4on9os/spec.rb:162 # DataModel#delete raises an error if the record is not saved
rspec /tmp/d20161202-15620-4on9os/spec.rb:196 # HashStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-4on9os/spec.rb:196 # ArrayStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-4on9os/spec.rb:218 # ArrayStore behaves like a data store #delete can delete multiple records with a single query

История (3 версии и 2 коментара)

Ралица обнови решението на 29.11.2016 23:37 (преди над 7 години)

+module InstanceMethods
+ def save
+ if self.id == nil
+ self.id = self.storage.smallest_available
+ self.storage.create(@attributes)
+ else
+ self.storage.update(id, @attributes)
+ end
+ end
+
+ def delete
+ if self.id == nil
+ raise DeleteUnsavedRecordError
+ else
+ id_to_delete = {id: @attributes[:id]}
+ self.storage.delete(id_to_delete)
+ self.id = nil
+ end
+ end
+
+ def ==(other)
+ if self.class == other.class
+ if self.id != nil && other.id != nil && self.id == other.id
+ true
+ else
+ self.equal?(other)
+ end
+ else
+ false
+ end
+ end
+end
+
+class DataModel
+ include InstanceMethods
+ class << self
+ attr_accessor :all_attributes, :storage
+ end
+
+ def all_attributes
+ self.class.all_attributes
+ end
+
+ def storage
+ self.class.storage
+ end
+
+ def initialize(hash = {})
+ @attributes = {}
+ self.all_attributes.each do |key|
+ @attributes[key] = hash[key]
+ end
+ @attributes[:id] = nil
+ @attributes[:object] = self
+ end
+
+ def self.attributes(*args)
+ if !args.empty?
+ self.all_attributes = ([:id, :object] + args).uniq
+ self.all_attributes.each do |attribute|
+ make_setters_and_getters attribute
+ end
+ else self.all_attributes
+ end
+ end
+
+ def self.make_setters_and_getters(arg)
+ self.class_eval("def #{arg}; @attributes[:#{arg}]; end")
+ self.class_eval("def #{arg}=(val);@attributes[:#{arg}]=val;end")
+ self.class_eval("def self.find_by_#{arg}(value); where({#{arg}:value}); end")
+ end
+
+ def self.data_store(new_data_store = nil)
+ if new_data_store != nil
+ self.storage = new_data_store
+ else
+ self.storage
+ end
+ end
+
+ def self.where(check_hash)
+ check_hash.keys.each do |key|
+ raise UnknownAttributeError unless all_attributes.include? key
+ end
+ (self.storage.find check_hash).map { |record| record[:object] }
+ end
+
+ class DeleteUnsavedRecordError < StandardError
+ end
+ class UnknownAttributeError < StandardError
+ end
+end
+
+module CommonMethodsStore
+ def create(new_record)
+ id = smallest_available
+ @storage[id] = new_record
+ end
+
+ def update(id, update_hash)
+ if id == nil
+ create(update_hash)
+ else
+ update_hash.keys.each do |key|
+ @storage[id][key] = update_hash[key]
+ end
+ end
+ end
+
+ attr_reader :storage
+
+ def smallest_available
+ range = 1..@storage.length + 1
+ range.each do |id|
+ return id if @storage[id] == nil
+ end
+ end
+end
+
+class HashStore
+ include CommonMethodsStore
+ def initialize
+ @storage = {}
+ end
+
+ def delete(check_hash)
+ @storage.each do |id_record, record|
+ flag = 1
+ check_hash.keys.each do |key|
+ flag = 0 unless record[key] == check_hash[key]
+ end
+ @storage.delete id_record if flag == 1
+ end
+ end
+
+ def find(check_hash)
+ compatible = []
+ @storage.each do |_, record|
+ flag = 1
+ check_hash.keys.each do |key|
+ flag = 0 unless record[key] == check_hash[key]
+ end
+ compatible << record if flag == 1
+ end
+ compatible
+ end
+end
+
+class ArrayStore
+ include CommonMethodsStore
+ def initialize
+ @storage = [{}]
+ end
+
+ def delete(check_hash)
+ @storage.each do |id_record, record|
+ flag = 1
+ check_hash.keys.each do |key|
+ flag = 0 unless record[key] == check_hash[key]
+ end
+ @storage.delete_at id_record if flag == 1
+ end
+ end
+
+ def find(check_hash)
+ compatible = []
+ @storage.each do |record|
+ flag = 0
+ if record != nil
+ check_hash.keys.each { |key| flag = 1 if record[key] == check_hash[key] }
+ end
+ compatible << record if flag == 1
+ end
+ compatible
+ end
+end

Ралица обнови решението на 01.12.2016 16:21 (преди над 7 години)

module InstanceMethods
def save
if self.id == nil
self.id = self.storage.smallest_available
self.storage.create(@attributes)
else
self.storage.update(id, @attributes)
end
end
def delete
if self.id == nil
raise DeleteUnsavedRecordError
else
id_to_delete = {id: @attributes[:id]}
self.storage.delete(id_to_delete)
self.id = nil
end
end
def ==(other)
if self.class == other.class
if self.id != nil && other.id != nil && self.id == other.id
true
else
self.equal?(other)
end
else
false
end
end
-end
-class DataModel
- include InstanceMethods
- class << self
- attr_accessor :all_attributes, :storage
- end
-
def all_attributes
self.class.all_attributes
end
def storage
self.class.storage
end
+end
+class DataModel
+ include InstanceMethods
def initialize(hash = {})
@attributes = {}
self.all_attributes.each do |key|
@attributes[key] = hash[key]
end
@attributes[:id] = nil
@attributes[:object] = self
end
- def self.attributes(*args)
- if !args.empty?
- self.all_attributes = ([:id, :object] + args).uniq
- self.all_attributes.each do |attribute|
- make_setters_and_getters attribute
+ class << self
+ attr_accessor :all_attributes, :storage
+
+ def attributes(*args)
+ self.all_attributes = []
+ if !args.empty?
+ (args << :id << :object).uniq.each do |attribute|
+ if attribute.is_a? Symbol
+ self.all_attributes << attribute
+ make_setters_and_getters attribute
+ end
+ end
+ else self.all_attributes
end
- else self.all_attributes
end
- end
- def self.make_setters_and_getters(arg)
- self.class_eval("def #{arg}; @attributes[:#{arg}]; end")
- self.class_eval("def #{arg}=(val);@attributes[:#{arg}]=val;end")
- self.class_eval("def self.find_by_#{arg}(value); where({#{arg}:value}); end")
- end
+ def make_setters_and_getters(arg)
+ define_method arg do
+ @attributes[arg]
+ end
- def self.data_store(new_data_store = nil)
- if new_data_store != nil
- self.storage = new_data_store
- else
- self.storage
+ define_method "#{arg}=" do |value|
+ @attributes[arg] = value
+ end
+
+ define_singleton_method "find_by_#{arg}" do |value|
+ where(arg.to_sym => value)
+ end
end
- end
- def self.where(check_hash)
- check_hash.keys.each do |key|
- raise UnknownAttributeError unless all_attributes.include? key
+ def data_store(new_data_store = nil)
+ if new_data_store != nil
+ self.storage = new_data_store
+ else
+ self.storage
+ end
end
- (self.storage.find check_hash).map { |record| record[:object] }
+
+ def where(check_hash)
+ check_hash.keys.each do |key|
+ raise UnknownAttributeError unless all_attributes.include? key
+ end
+ (self.storage.find check_hash).map { |record| record[:object] }
+ end
end
class DeleteUnsavedRecordError < StandardError
end
class UnknownAttributeError < StandardError
end
end
module CommonMethodsStore
def create(new_record)
id = smallest_available
@storage[id] = new_record
end
def update(id, update_hash)
if id == nil
create(update_hash)
else
update_hash.keys.each do |key|
@storage[id][key] = update_hash[key]
end
end
end
attr_reader :storage
def smallest_available
range = 1..@storage.length + 1
range.each do |id|
return id if @storage[id] == nil
end
end
end
class HashStore
include CommonMethodsStore
def initialize
@storage = {}
end
def delete(check_hash)
@storage.each do |id_record, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
@storage.delete id_record if flag == 1
end
end
def find(check_hash)
compatible = []
@storage.each do |_, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
compatible << record if flag == 1
end
compatible
end
end
class ArrayStore
include CommonMethodsStore
def initialize
@storage = [{}]
end
def delete(check_hash)
@storage.each do |id_record, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
@storage.delete_at id_record if flag == 1
end
end
def find(check_hash)
compatible = []
@storage.each do |record|
flag = 0
if record != nil
check_hash.keys.each { |key| flag = 1 if record[key] == check_hash[key] }
end
compatible << record if flag == 1
end
compatible
end
-end
+end

Ралица обнови решението на 01.12.2016 22:38 (преди над 7 години)

module InstanceMethods
def save
if self.id == nil
self.id = self.storage.smallest_available
self.storage.create(@attributes)
else
self.storage.update(id, @attributes)
end
end
def delete
if self.id == nil
raise DeleteUnsavedRecordError
else
id_to_delete = {id: @attributes[:id]}
self.storage.delete(id_to_delete)
self.id = nil
end
end
def ==(other)
if self.class == other.class
if self.id != nil && other.id != nil && self.id == other.id
true
else
self.equal?(other)
end
else
false
end
end
def all_attributes
self.class.all_attributes
end
def storage
self.class.storage
end
end
class DataModel
include InstanceMethods
def initialize(hash = {})
@attributes = {}
self.all_attributes.each do |key|
@attributes[key] = hash[key]
end
@attributes[:id] = nil
- @attributes[:object] = self
end
class << self
attr_accessor :all_attributes, :storage
def attributes(*args)
self.all_attributes = []
if !args.empty?
- (args << :id << :object).uniq.each do |attribute|
+ (args << :id ).uniq.each do |attribute|
if attribute.is_a? Symbol
self.all_attributes << attribute
make_setters_and_getters attribute
end
end
else self.all_attributes
end
end
def make_setters_and_getters(arg)
define_method arg do
@attributes[arg]
end
define_method "#{arg}=" do |value|
@attributes[arg] = value
end
define_singleton_method "find_by_#{arg}" do |value|
where(arg.to_sym => value)
end
end
def data_store(new_data_store = nil)
if new_data_store != nil
self.storage = new_data_store
- else
- self.storage
+ else self.storage
end
end
def where(check_hash)
check_hash.keys.each do |key|
raise UnknownAttributeError unless all_attributes.include? key
end
- (self.storage.find check_hash).map { |record| record[:object] }
+ self.storage.find(check_hash).map do |record|
+ obj = User.new(record)
+ obj.id = record[:id]
+ obj
+ end
end
end
class DeleteUnsavedRecordError < StandardError
end
class UnknownAttributeError < StandardError
end
end
module CommonMethodsStore
def create(new_record)
id = smallest_available
@storage[id] = new_record
end
def update(id, update_hash)
if id == nil
create(update_hash)
else
update_hash.keys.each do |key|
@storage[id][key] = update_hash[key]
end
end
end
attr_reader :storage
def smallest_available
range = 1..@storage.length + 1
range.each do |id|
return id if @storage[id] == nil
end
end
end
class HashStore
include CommonMethodsStore
def initialize
@storage = {}
end
def delete(check_hash)
@storage.each do |id_record, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
@storage.delete id_record if flag == 1
end
end
def find(check_hash)
compatible = []
@storage.each do |_, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
compatible << record if flag == 1
end
compatible
end
end
class ArrayStore
include CommonMethodsStore
def initialize
@storage = [{}]
end
def delete(check_hash)
@storage.each do |id_record, record|
flag = 1
check_hash.keys.each do |key|
flag = 0 unless record[key] == check_hash[key]
end
@storage.delete_at id_record if flag == 1
end
end
def find(check_hash)
compatible = []
@storage.each do |record|
flag = 0
if record != nil
check_hash.keys.each { |key| flag = 1 if record[key] == check_hash[key] }
end
compatible << record if flag == 1
end
compatible
end
end