Permalink
Browse files

. initial sketch

  • Loading branch information...
1 parent d80c547 commit 9596c6da71891df238ce35eb4b2366f0a0f98bed @kschiess committed Jun 3, 2010
Showing with 104 additions and 6 deletions.
  1. +44 −4 lib/floor_manager/employee.rb
  2. +8 −1 lib/floor_manager/floor.rb
  3. +6 −0 spec/support/model/spy.rb
  4. +46 −1 spec/unit/floor_manager_spec.rb
@@ -12,8 +12,11 @@ def initialize(employee, &block)
def method_missing(sym, *args, &block)
if args.size == 1
# Immediate attribute
+ value = args.first
+ @employee.add_attribute sym, Proc.new { value }
elsif block
# Lazy attribute
+ @employee.add_attribute sym, block
else
super
end
@@ -27,20 +30,57 @@ def self.from_dsl(klass_name, &block)
def initialize(klass_name)
@klass_name = klass_name
+ @attributes = []
+ end
+
+ def add_attribute(name, callable)
+ @attributes << [name, callable]
end
- def build
+ def build(floor)
+ produce_instance.tap { |i| apply_attributes(i, floor) }
+ end
+
+ def create(floor)
+ produce_instance.tap { |i|
+ apply_attributes(i, floor)
+ i.save! }
+ end
+
+ protected
+ def produce_instance
@klass_name.to_s.
camelcase.
constantize.
- build()
+ new
+ end
+
+ def apply_attributes(instance, floor)
+ @attributes.each do |name, value_producer|
+ instance.send("#{name}=", value_producer.call(instance, floor))
+ end
end
end
# A unique employee that will be build/created only once in the given floor.
class Unique < Base
- def build
- @instance ||= super
+ # REFACTOR: Redundancy
+ def build(floor)
+ return @instance if @instance
+ @instance = produce_instance
+ apply_attributes(@instance, floor)
+
+ @instance
+ end
+
+ # REFACTOR: Redundancy
+ def create(floor)
+ return @instance if @instance
+ @instance = produce_instance
+ apply_attributes(@instance, floor)
+ @instance.save!
+
+ @instance
end
end
@@ -36,9 +36,16 @@ def initialize
def method_missing(sym, *args, &block)
if args.size == 0 && employees.has_key?(sym)
- employees[sym].build
+ employees[sym].build(self)
else
super
end
end
+
+ def create(something)
+ employees[something.to_sym].create(self)
+ end
+ def build(something)
+ employees[something.to_sym].build(self)
+ end
end
@@ -12,4 +12,10 @@ def self.build(attrs={})
end
}
end
+
+ def initialize
+ @saved = false
+ end
+ def save!; @saved = true; end
+ def saved?; @saved; end
end
@@ -20,6 +20,45 @@
it "should build the same object twice" do
env.white.should == env.white
end
+
+ context "white spy" do
+ subject { env.white }
+
+ its(:name) { should == 'white spy'}
+ its(:opposite) { should == env.black }
+ end
+ context "black spy" do
+ subject { env.black }
+
+ its(:name) { should == 'black spy'}
+ its(:opposite) { should == env.white }
+ end
+
+ describe "<- #create(:white)" do
+ let(:white) { env.create(:white) }
+
+ it "should return a saved object" do
+ white.should be_saved
+ end
+ end
+ describe "<- #build(:white)" do
+ let(:white) { env.build(:white) }
+
+ it "should not return a saved object" do
+ white.should_not be_saved
+ end
+ it "should return the same object through method missing" do
+ white.should == env.white
+ end
+ context "when accessing the same object with #create" do
+ let(:created) { env.create(:white) }
+ subject { created }
+ it "should return the same object still" do
+ white.should == created
+ end
+ it { should be_saved }
+ end
+ end
end
context "environment with template definition" do
@@ -35,7 +74,13 @@
it "should return a new instance each time called" do
env.spy.should_not == env.spy
- end
+ end
+
+ context "spy" do
+ subject { env.spy }
+
+ its(:name) { should == 'white spy'}
+ end
end
end

0 comments on commit 9596c6d

Please sign in to comment.