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

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

Към профила на Милен Дончев

Резултати

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

Код

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
raise DeleteUnsavedRecordError.new if element.nil?
self.class.store.delete(element)
end
def ==(other)
return true if self.id == other.id
return true if self.object_id == other.object_id
false
end
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
DataModel::DeleteUnsavedRecordError = Class.new(StandardError) do
attr_reader :object
def initialize(object = nil)
@object = object
end
end
DataModel::UnknownAttributeError = Class.new(StandardError) do
attr_reader :object
def initialize(object = nil)
@object = object
end
end
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
return @attributes_data if attr == []
@attributes_data = []
attr.each do |x|
attr_accessor(x)
define_singleton_method('find_by_' + x.to_s) do |item|
@store.find({x.to_s.to_sym => item}.to_h)
end
@attributes_data << x
end
define_singleton_method('find_by_id') { |item| @store.find(id: item) }
end
def data_store(store = nil)
return @store if store.nil?
@store = store
end
def where(search_option)
result = []
missing = (search_option.keys - attributes_data).first
unless missing.nil?
raise DataModel::UnknownAttributeError.new, "Unknown attribute #{missing}"
end
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end

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

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

Failures:

  1) DataModel has #find_by_<attribute> methods
     Failure/Error: expect(user_model.find_by_first_name('Ivan').map(&:id)).to eq [record.id]
     NoMethodError:
       undefined method `id' for {:first_name=>"Ivan", :last_name=>"Ivanov", :age=>nil, :id=>1}:Hash
     # /tmp/d20161202-15620-1nwf27k/spec.rb:43:in `map'
     # /tmp/d20161202-15620-1nwf27k/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)>'

  2) DataModel equality comparison compares by id if both records are saved
     Failure/Error: modified_ivan = user_model.where(id: ivan.id).first
     DataModel::UnknownAttributeError:
       Unknown attribute id
     # /tmp/d20161202-15620-1nwf27k/solution.rb:186:in `where'
     # /tmp/d20161202-15620-1nwf27k/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)>'

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

  4) DataModel#delete deletes only the record for which it is called
     Failure/Error: ivan.delete
     NoMethodError:
       undefined method `delete' for 1:Fixnum
     # /tmp/d20161202-15620-1nwf27k/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)>'

Finished in 0.02753 seconds
25 examples, 4 failures

Failed examples:

rspec /tmp/d20161202-15620-1nwf27k/spec.rb:39 # DataModel has #find_by_<attribute> methods
rspec /tmp/d20161202-15620-1nwf27k/spec.rb:92 # DataModel equality comparison compares by id if both records are saved
rspec /tmp/d20161202-15620-1nwf27k/spec.rb:108 # DataModel equality comparison uses #equal? if there are no ids
rspec /tmp/d20161202-15620-1nwf27k/spec.rb:151 # DataModel#delete deletes only the record for which it is called

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

Милен обнови решението на 01.12.2016 11:23 (преди над 7 години)

+class Helper
+ def self.hash_constains_in_hash(hash, searched)
+ eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
+ return true if eql
+
+ contains = true
+ searched.each do |key, value|
+ contains = false if hash[key] != value
+ end
+ contains
+ end
+
+ def self.hash_to_obj(obj, hash)
+ hash.each do |key, value|
+ setter_name = key.to_s + '='
+ obj.send(setter_name, value)
+ end
+ obj
+ end
+end
+
+class Store
+ attr_reader :incrementator
+
+ def initialize
+ @incrementator = 1
+ end
+
+ private
+
+ def insert_id_if_none(hash)
+ if hash[:id].nil?
+ hash[:id] = @incrementator
+ @incrementator += 1
+ end
+ end
+end
+
+class ArrayStore < Store
+ attr_reader :storage
+
+ def initialize
+ super
+ @storage = []
+ end
+
+ def create(hash)
+ insert_id_if_none(hash)
+ @storage << hash
+ end
+
+ def find(search_option)
+ result = []
+ @storage.each do |current|
+ result << current if Helper.hash_constains_in_hash(current, search_option)
+ end
+ result
+ end
+
+ def update(id, hash)
+ element = find(id: id).first
+ hash.each do |key, value|
+ element[key] = value
+ end
+ end
+
+ def delete(search_option)
+ elements = find(search_option)
+ @storage -= elements
+ end
+end
+
+class HashStore < Store
+ attr_reader :storage
+
+ def initialize
+ super
+ @storage = {}
+ end
+
+ def create(hash)
+ insert_id_if_none(hash)
+ @storage[hash[:id]] = hash
+ end
+
+ def find(search_option)
+ result = []
+ @storage.each do |_, current|
+ result << current if Helper.hash_constains_in_hash(current, search_option)
+ end
+ result
+ end
+
+ def update(id, hash)
+ element = find(id: id).first
+ hash.each do |key, value|
+ element[key] = value
+ end
+ end
+
+ def delete(search_option)
+ elements = find(search_option)
+ @storage = @storage.delete_if { |_, value| elements.include? value }
+ end
+end
+
+class DataModel
+ attr_reader :id
+ attr_reader :attributes_data
+
+ def initialize(data = nil)
+ unless data.nil?
+ data.each do |method_name, value|
+ setter_name = method_name.to_s + '='
+ public_send(setter_name, value) if respond_to? setter_name
+ # because id is private, no one should be modifying it
+ send(setter_name, value) if setter_name == 'id='
+ end
+ end
+ end
+
+ def self.attributes(*attr)
+ @attributes_data = []
+ attr.each do |x|
+ attr_accessor(x)
+ @attributes_data << x
+ end
+ end
+
+ def self.data_store(store)
+ @store = store
+ end
+
+ def self.where(search_option)
+ result = []
+ @store.find(search_option).each do |element|
+ result << Helper.hash_to_obj(self.new, element)
+ end
+ result
+ end
+
+ def save
+ hash = self_to_h
+ if @store.find(hash) == []
+ @store.create(hash)
+ else
+ @store.update(hash[:id], hash)
+ end
+ @id = hash[:id]
+ end
+
+ def delete
+ element = @store.find(self_to_h).first
+ @store.delete(element)
+ end
+
+ private
+
+ def self_to_h
+ result = {}
+ @attributes_data.each { |method| result[method] = public_send(method) }
+ result[:id] = @id unless @id.nil?
+ result
+ end
+
+ attr_writer :id
+end

Милен обнови решението на 01.12.2016 18:32 (преди над 7 години)

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
- attr_reader :id
- attr_reader :attributes_data
+ attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
- def self.attributes(*attr)
- @attributes_data = []
- attr.each do |x|
- attr_accessor(x)
- @attributes_data << x
- end
- end
-
- def self.data_store(store)
- @store = store
- end
-
- def self.where(search_option)
- result = []
- @store.find(search_option).each do |element|
- result << Helper.hash_to_obj(self.new, element)
- end
- result
- end
-
def save
hash = self_to_h
- if @store.find(hash) == []
- @store.create(hash)
+ if self.class.store.find(hash) == []
+ self.class.store.create(hash)
else
- @store.update(hash[:id], hash)
+ self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
- element = @store.find(self_to_h).first
- @store.delete(element)
+ element = self.class.store.find(self_to_h).first
+ self.class.store.delete(element)
end
private
def self_to_h
result = {}
- @attributes_data.each { |method| result[method] = public_send(method) }
+ self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
+end
+
+class << DataModel
+ attr_accessor :attributes_data
+ attr_accessor :store
- attr_writer :id
+ def attributes(*attr)
+ @attributes_data = []
+ attr.each do |x|
+ attr_accessor(x)
+ @attributes_data << x
+ end
+ end
+
+ def data_store(store)
+ @store = store
+ end
+
+ def where(search_option)
+ result = []
+ @store.find(search_option).each do |element|
+ result << Helper.hash_to_obj(self.new, element)
+ end
+ result
+ end
end

Милен обнови решението на 01.12.2016 18:53 (преди над 7 години)

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
self.class.store.delete(element)
end
+ def ==(other)
+ return true if self.id == other.id
+ return true if self.object_id == other.object_id
+ false
+ end
+
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
@attributes_data = []
attr.each do |x|
attr_accessor(x)
@attributes_data << x
end
end
def data_store(store)
@store = store
end
def where(search_option)
result = []
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end

Милен обнови решението на 01.12.2016 19:01 (преди над 7 години)

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
self.class.store.delete(element)
end
def ==(other)
return true if self.id == other.id
return true if self.object_id == other.object_id
false
end
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
+ return @attributes_data if attr == []
@attributes_data = []
attr.each do |x|
attr_accessor(x)
@attributes_data << x
end
end
- def data_store(store)
+ def data_store(store = nil)
+ return @store if store.nil?
@store = store
end
def where(search_option)
result = []
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end

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

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
-
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
-
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
-
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
-
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
self.class.store.delete(element)
end
def ==(other)
return true if self.id == other.id
return true if self.object_id == other.object_id
false
end
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
-
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
return @attributes_data if attr == []
@attributes_data = []
attr.each do |x|
attr_accessor(x)
+ define_singleton_method('find_by_' + x.to_s) do |item|
+ @store.find({x.to_s.to_sym => item}.to_h)
+ end
@attributes_data << x
end
+ define_singleton_method('find_by_id') { |item| @store.find(id: item) }
end
def data_store(store = nil)
return @store if store.nil?
@store = store
end
def where(search_option)
result = []
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end

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

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
self.class.store.delete(element)
end
def ==(other)
return true if self.id == other.id
return true if self.object_id == other.object_id
false
end
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
+
+DataModel::DeleteUnsavedRecordError = Class.new(StandardError) do
+ attr_reader :object
+
+ def initialize(object = nil)
+ @object = object
+ end
+end
+
+DataModel::UnknownAttributeError = Class.new(StandardError) do
+ attr_reader :object
+
+ def initialize(object = nil)
+ @object = object
+ end
+end
+
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
return @attributes_data if attr == []
@attributes_data = []
attr.each do |x|
attr_accessor(x)
define_singleton_method('find_by_' + x.to_s) do |item|
@store.find({x.to_s.to_sym => item}.to_h)
end
@attributes_data << x
end
define_singleton_method('find_by_id') { |item| @store.find(id: item) }
end
def data_store(store = nil)
return @store if store.nil?
@store = store
end
def where(search_option)
result = []
+ missing = (search_option.keys - attributes_data).first
+ unless missing.nil?
+ raise DataModel::UnknownAttributeError.new, "Unknown attribute #{missing}"
+ end
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end

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

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
+ raise DeleteUnsavedRecordError.new if element.nil?
self.class.store.delete(element)
end
def ==(other)
return true if self.id == other.id
return true if self.object_id == other.object_id
false
end
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
DataModel::DeleteUnsavedRecordError = Class.new(StandardError) do
attr_reader :object
def initialize(object = nil)
@object = object
end
end
DataModel::UnknownAttributeError = Class.new(StandardError) do
attr_reader :object
def initialize(object = nil)
@object = object
end
end
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
return @attributes_data if attr == []
@attributes_data = []
attr.each do |x|
attr_accessor(x)
define_singleton_method('find_by_' + x.to_s) do |item|
@store.find({x.to_s.to_sym => item}.to_h)
end
@attributes_data << x
end
define_singleton_method('find_by_id') { |item| @store.find(id: item) }
end
def data_store(store = nil)
return @store if store.nil?
@store = store
end
def where(search_option)
result = []
missing = (search_option.keys - attributes_data).first
unless missing.nil?
raise DataModel::UnknownAttributeError.new, "Unknown attribute #{missing}"
end
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end

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

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
raise DeleteUnsavedRecordError.new if element.nil?
self.class.store.delete(element)
end
def ==(other)
return true if self.id == other.id
return true if self.object_id == other.object_id
false
end
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
DataModel::DeleteUnsavedRecordError = Class.new(StandardError) do
attr_reader :object
- def initialize(object = nil)
+ def initialize(object)
@object = object
end
end
DataModel::UnknownAttributeError = Class.new(StandardError) do
attr_reader :object
- def initialize(object = nil)
+ def initialize(object)
@object = object
end
end
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
return @attributes_data if attr == []
@attributes_data = []
attr.each do |x|
attr_accessor(x)
define_singleton_method('find_by_' + x.to_s) do |item|
@store.find({x.to_s.to_sym => item}.to_h)
end
@attributes_data << x
end
define_singleton_method('find_by_id') { |item| @store.find(id: item) }
end
def data_store(store = nil)
return @store if store.nil?
@store = store
end
def where(search_option)
result = []
missing = (search_option.keys - attributes_data).first
unless missing.nil?
raise DataModel::UnknownAttributeError.new, "Unknown attribute #{missing}"
end
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end

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

class Helper
def self.hash_constains_in_hash(hash, searched)
eql = !hash[:id].nil? && !searched[:id].nil? && hash[:id] == searched[:id]
return true if eql
contains = true
searched.each do |key, value|
contains = false if hash[key] != value
end
contains
end
def self.hash_to_obj(obj, hash)
hash.each do |key, value|
setter_name = key.to_s + '='
obj.send(setter_name, value)
end
obj
end
end
class Store
attr_reader :incrementator
def initialize
@incrementator = 1
end
private
def insert_id_if_none(hash)
if hash[:id].nil?
hash[:id] = @incrementator
@incrementator += 1
end
end
end
class ArrayStore < Store
attr_reader :storage
def initialize
super
@storage = []
end
def create(hash)
insert_id_if_none(hash)
@storage << hash
end
def find(search_option)
result = []
@storage.each do |current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage -= elements
end
end
class HashStore < Store
attr_reader :storage
def initialize
super
@storage = {}
end
def create(hash)
insert_id_if_none(hash)
@storage[hash[:id]] = hash
end
def find(search_option)
result = []
@storage.each do |_, current|
result << current if Helper.hash_constains_in_hash(current, search_option)
end
result
end
def update(id, hash)
element = find(id: id).first
hash.each do |key, value|
element[key] = value
end
end
def delete(search_option)
elements = find(search_option)
@storage = @storage.delete_if { |_, value| elements.include? value }
end
end
class DataModel
attr_accessor :id
def initialize(data = nil)
unless data.nil?
data.each do |method_name, value|
setter_name = method_name.to_s + '='
public_send(setter_name, value) if respond_to? setter_name
# because id is private, no one should be modifying it
send(setter_name, value) if setter_name == 'id='
end
end
end
def save
hash = self_to_h
if self.class.store.find(hash) == []
self.class.store.create(hash)
else
self.class.store.update(hash[:id], hash)
end
@id = hash[:id]
end
def delete
element = self.class.store.find(self_to_h).first
raise DeleteUnsavedRecordError.new if element.nil?
self.class.store.delete(element)
end
def ==(other)
return true if self.id == other.id
return true if self.object_id == other.object_id
false
end
private
def self_to_h
result = {}
self.class.attributes_data.each { |method| result[method] = public_send(method) }
result[:id] = @id unless @id.nil?
result
end
end
-
DataModel::DeleteUnsavedRecordError = Class.new(StandardError) do
attr_reader :object
-
- def initialize(object)
+ def initialize(object = nil)
@object = object
end
end
-
DataModel::UnknownAttributeError = Class.new(StandardError) do
attr_reader :object
-
- def initialize(object)
+ def initialize(object = nil)
@object = object
end
end
-
class << DataModel
attr_accessor :attributes_data
attr_accessor :store
def attributes(*attr)
return @attributes_data if attr == []
@attributes_data = []
attr.each do |x|
attr_accessor(x)
define_singleton_method('find_by_' + x.to_s) do |item|
@store.find({x.to_s.to_sym => item}.to_h)
end
@attributes_data << x
end
define_singleton_method('find_by_id') { |item| @store.find(id: item) }
end
def data_store(store = nil)
return @store if store.nil?
@store = store
end
def where(search_option)
result = []
missing = (search_option.keys - attributes_data).first
unless missing.nil?
raise DataModel::UnknownAttributeError.new, "Unknown attribute #{missing}"
end
@store.find(search_option).each do |element|
result << Helper.hash_to_obj(self.new, element)
end
result
end
end