Permalink
Browse files

More tests for the week and other changes, thand!

  • Loading branch information...
1 parent 56c79bf commit 199e2b58885d06454162f5d8432dac76953feb53 @jwaldrip committed Nov 9, 2012
View
74 lib/memory_model/associations/base.rb
@@ -4,6 +4,23 @@ class MemoryModel::Associations::Base
attr_reader :owner_class, :name, :type, :options
+ class << self
+
+ def belongs_to(owner, name, options={})
+ owner.send :field, options[:foreign_key] || "#{name}_id"
+ new owner, name, :instance, options
+ end
+
+ def has_one(owner, name, options={})
+ new owner, name, :instance, options
+ end
+
+ def has_many(owner, name, options={})
+ new owner, name, :collection, options
+ end
+
+ end
+
def initialize(owner, name, type, options={})
@owner_class = owner
@name = name
@@ -12,26 +29,17 @@ def initialize(owner, name, type, options={})
owner_class.associations = owner_class.associations + Array.wrap(self)
end
- def set_association(parent, instance)
- if instance? && parent?
- parent.update(foreign_key => instance.id)
-
- elsif instance? && child?
- existing = klass.all.select{ |item| item.send foreign_key == parent.id }
- existing.each { |item| item.update(foreign_key => nil) }
- instance.update(foreign_key => parent.id)
+ def set_association(parent, object)
+ if instance?
+ set_instance(parent, object)
elsif collection? && parent?
- instance.map do |item|
- raise "Invalid Class" unless item.is_a?(klass)
- item.send("#{foreign_key}=", parent.id)
- end
+ set_collection(parent, object)
+
else
raise "Invalid Association!"
end
-
- instance
end
def load_association(parent)
@@ -57,6 +65,15 @@ def foreign_key
private
+ # Modifiers
+
+ def unassociate_parent_from_all(parent)
+ existing = klass.all.select{ |item| item.send(foreign_key) == parent.id }
+ existing.each { |item| item.update(foreign_key => nil) }
+ end
+
+ # Conditionals
+
def parent?
klass.fields.include?(foreign_key)
end
@@ -73,6 +90,35 @@ def collection?
type == :collection
end
+ # Setters
+
+ def set_collection(parent, collection)
+ unassociate_parent_from_all(parent)
+ collection.map do |item|
+ raise "Invalid Class" unless item.is_a?(klass)
+ item.send("#{foreign_key}=", parent.id)
+ end
+ end
+
+ def set_instance(parent, instance)
+ raise "Invalid Class" unless instance.is_a?(klass)
+
+ if parent?
+ instance.save
+ parent.update(foreign_key => instance.id)
+
+ else child?
+ unassociate_parent_from_all(parent)
+ instance.update(foreign_key => parent.id)
+
+ end
+
+ instance
+
+ end
+
+ # Getters
+
def load_collection(parent)
Proxy.new(self, parent)
end
View
7 lib/memory_model/associations/class_methods.rb
@@ -3,16 +3,15 @@ module MemoryModel::Associations::ClassMethods
Base = MemoryModel::Associations::Base
def belongs_to(name, options={})
- field options[:foreign_key] || "#{name}_id"
- Base.new(self, name, :instance, options)
+ Base.belongs_to(self, name, options)
end
def has_one(name, options={})
- Base.new(self, name, :instance, options)
+ Base.has_one(self, name, options)
end
def has_many(name, options={})
- Base.new(self, name, :collection, options)
+ Base.has_many(self, name, options)
end
end
View
7 lib/memory_model/base.rb
@@ -46,8 +46,7 @@ def initialize(attrs={})
end
def save
- found_instance = collection.find{ |item| item.id == id }
- if found_instance
+ if (found_instance = klass.collection.find { |item| item.id == id })
instance = found_instance.attributes=(self)
else
self.attributes = { id: klass.next_id }
@@ -57,4 +56,8 @@ def save
instance
end
+ def reload!
+ self.replace klass.find(id)
+ end
+
end
View
93 spec/memory_model/associations/base_spec.rb
@@ -6,21 +6,94 @@
let(:other_klass) { Class.new(MemoryModel::Base) }
subject { MemoryModel::Associations::Base }
- describe ".new"
+ describe ".new" do
+
+ it "should set a custom foreign key" do
+ association = subject.new(klass, :test, nil, foreign_key: "foreign_id")
+ association.foreign_key.should == "foreign_id"
+ end
+
+ it "should set a custom class" do
+ association = subject.new(klass, :test, nil, class: other_klass)
+ association.klass.should == other_klass
+ end
+
+ it "should set a custom class by name" do
+ association = subject.new(klass, :test, nil, class_name: "Object")
+ association.klass.should == Object
+ end
+
+ end
+
+ describe ".belongs_to" do
+
+ let!(:klass) { Class.new(MemoryModel::Base) }
+ let!(:other_klass) { Class.new(MemoryModel::Base) }
+ let!(:belongs_to){ subject.belongs_to(klass, :test_association, class: other_klass) }
+ let(:instance) { klass.create }
+ let(:other_instance) { other_klass.create }
+
+ describe "#set_association" do
+
+ it "should set an instance" do
+ belongs_to.set_association(instance, other_instance)
+ instance.test_association.should == other_instance
+ end
+
+ end
+
+ describe "#load_association" do
+
+ it "should load an instance" do
+ instance.test_association = other_instance
+ belongs_to.load_association(instance).should == other_instance
+ end
+
+ end
- it "should set a custom foreign key" do
- association = subject.new(klass, :test, nil, foreign_key: "foreign_id")
- association.foreign_key.should == "foreign_id"
end
- it "should set a custom class" do
- association = subject.new(klass, :test, nil, class: other_klass)
- association.klass.should == other_klass
+ describe ".has_one" do
+
+ describe "#set_association" do
+
+ it "should set an instance" do
+ belongs_to.set_association(instance, other_instance)
+ instance.test_association.should == other_instance
+ end
+
+ end
+
+ describe "#load_association" do
+
+ it "should load an instance" do
+ instance.test_association = other_instance
+ belongs_to.load_association(instance).should == other_instance
+ end
+
+ end
+
end
- it "should set a custom class by name" do
- association = subject.new(klass, :test, nil, class_name: "Object")
- association.klass.should == Object
+ describe ".has_many" do
+
+ describe "#set_association" do
+
+ it "should set a collection" do
+ belongs_to.set_association(collection, other_instance)
+ instance.test_association.should == other_instance
+ end
+
+ end
+
+ describe "#load_association" do
+
+ it "should load a collection" do
+
+ end
+
+ end
+
end
end
View
18 spec/memory_model/base_spec.rb
@@ -16,6 +16,24 @@
end
end
+ describe "#reload" do
+
+ let!(:klass) do
+ Class.new(MemoryModel::Base) do
+ field :name
+ end
+ end
+ let(:instance){ klass.create }
+
+ it "Should reload the object" do
+ dup_instance = klass.find(instance.id)
+ dup_instance.update({ name: "Jason" })
+ instance.reload!
+ instance.should == dup_instance
+ end
+
+ end
+
end

0 comments on commit 199e2b5

Please sign in to comment.