Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

+ after_create hook in DSL

  • Loading branch information...
commit 4679266c43ee97e95ace554add85299f33cc2416 1 parent a2f6fb3
Kaspar Schiess authored June 04, 2010
17  lib/floor_manager/employee.rb
@@ -12,21 +12,22 @@ def self.from_dsl(klass_name, &block)
12 12
 
13 13
     def initialize(klass_name)
14 14
       @klass_name = klass_name
15  
-      @attributes = []
  15
+      @attributes = Hash.new { |h,k| h[k] = Array.new }
16 16
     end
17 17
     
18 18
     # Build this employee in memory. 
19 19
     #
20 20
     def build(floor, overrides)
21  
-      produce_instance.tap { |i| apply_attributes(i, overrides, floor) }
  21
+      produce_instance.tap { |i| apply_attributes(i, :none, floor, overrides) }
22 22
     end
23 23
     
24 24
     # Create this employee in the database. 
25 25
     #
26 26
     def create(floor, overrides)
27 27
       produce_instance.tap { |i| 
28  
-        apply_attributes(i, overrides, floor)
29  
-        i.save! }
  28
+        apply_attributes(i, :none, floor, overrides)
  29
+        i.save!
  30
+        apply_attributes(i, :after_create, floor) }
30 31
     end
31 32
     
32 33
     # Returns just the attributes that would be used.
@@ -45,8 +46,8 @@ def reset
45 46
     # interface. This method is mainly used by the DSL to store actions to
46 47
     # take.
47 48
     #
48  
-    def add_attribute action
49  
-      @attributes << action
  49
+    def add_attribute filter, action
  50
+      @attributes[filter] << action
50 51
     end
51 52
   protected
52 53
     def produce_instance
@@ -60,9 +61,9 @@ def produce_instance
60 61
     # specified in the factory for this employee and then overriding them with
61 62
     # what was given in +overrides+.
62 63
     #
63  
-    def apply_attributes(instance, overrides, floor)
  64
+    def apply_attributes(instance, filter, floor, overrides={})
64 65
       # First apply all attributes that were given in the factory definition. 
65  
-      @attributes.
  66
+      @attributes[filter].
66 67
         each do |action|
67 68
           action.apply(instance, floor, self)
68 69
         end
16  lib/floor_manager/employee/dsl.rb
@@ -6,12 +6,12 @@ class DSL < BlankSlate
6 6
     #     relationship.set :gun
7 7
     #   end
8 8
     #
9  
-    class AssocProxy < Struct.new(:employee, :field)
  9
+    class AssocProxy < Struct.new(:employee, :field, :dsl)
10 10
       def set(*create_args)
11  
-        employee.add_attribute AttributeAction::AssocSet.new(field, create_args)
  11
+        dsl._add_attribute AttributeAction::AssocSet.new(field, create_args)
12 12
       end
13 13
       def append(*create_args)
14  
-        employee.add_attribute AttributeAction::AssocAppend.new(field, create_args)
  14
+        dsl._add_attribute AttributeAction::AssocAppend.new(field, create_args)
15 15
       end
16 16
     end
17 17
 
@@ -54,16 +54,20 @@ def method_missing(sym, *args, &block)
54 54
       if args.size == 1
55 55
         # Immediate attribute
56 56
         value = args.first
57  
-        @employee.add_attribute AttributeAction::Immediate.new(sym, value)
  57
+        _add_attribute AttributeAction::Immediate.new(sym, value)
58 58
       elsif block
59 59
         # Lazy attribute
60  
-        @employee.add_attribute AttributeAction::Block.new(sym, block)
  60
+        _add_attribute AttributeAction::Block.new(sym, block)
61 61
       elsif args.empty?
62 62
         # Maybe: #set / #append proxy?
63  
-        AssocProxy.new(@employee, sym)
  63
+        AssocProxy.new(@employee, sym, self)
64 64
       else
65 65
         super
66 66
       end
67 67
     end
  68
+
  69
+    def _add_attribute(action)
  70
+      @employee.add_attribute @filter, action
  71
+    end
68 72
   end
69 73
 end

0 notes on commit 4679266

Please sign in to comment.
Something went wrong with that request. Please try again.