Permalink
Browse files

+ after_create hook in DSL

  • Loading branch information...
1 parent a2f6fb3 commit 4679266c43ee97e95ace554add85299f33cc2416 @kschiess committed Jun 4, 2010
Showing with 19 additions and 14 deletions.
  1. +9 −8 lib/floor_manager/employee.rb
  2. +10 −6 lib/floor_manager/employee/dsl.rb
View
17 lib/floor_manager/employee.rb
@@ -12,21 +12,22 @@ def self.from_dsl(klass_name, &block)
def initialize(klass_name)
@klass_name = klass_name
- @attributes = []
+ @attributes = Hash.new { |h,k| h[k] = Array.new }
end
# Build this employee in memory.
#
def build(floor, overrides)
- produce_instance.tap { |i| apply_attributes(i, overrides, floor) }
+ produce_instance.tap { |i| apply_attributes(i, :none, floor, overrides) }
end
# Create this employee in the database.
#
def create(floor, overrides)
produce_instance.tap { |i|
- apply_attributes(i, overrides, floor)
- i.save! }
+ apply_attributes(i, :none, floor, overrides)
+ i.save!
+ apply_attributes(i, :after_create, floor) }
end
# Returns just the attributes that would be used.
@@ -45,8 +46,8 @@ def reset
# interface. This method is mainly used by the DSL to store actions to
# take.
#
- def add_attribute action
- @attributes << action
+ def add_attribute filter, action
+ @attributes[filter] << action
end
protected
def produce_instance
@@ -60,9 +61,9 @@ def produce_instance
# specified in the factory for this employee and then overriding them with
# what was given in +overrides+.
#
- def apply_attributes(instance, overrides, floor)
+ def apply_attributes(instance, filter, floor, overrides={})
# First apply all attributes that were given in the factory definition.
- @attributes.
+ @attributes[filter].
each do |action|
action.apply(instance, floor, self)
end
View
16 lib/floor_manager/employee/dsl.rb
@@ -6,12 +6,12 @@ class DSL < BlankSlate
# relationship.set :gun
# end
#
- class AssocProxy < Struct.new(:employee, :field)
+ class AssocProxy < Struct.new(:employee, :field, :dsl)
def set(*create_args)
- employee.add_attribute AttributeAction::AssocSet.new(field, create_args)
+ dsl._add_attribute AttributeAction::AssocSet.new(field, create_args)
end
def append(*create_args)
- employee.add_attribute AttributeAction::AssocAppend.new(field, create_args)
+ dsl._add_attribute AttributeAction::AssocAppend.new(field, create_args)
end
end
@@ -54,16 +54,20 @@ def method_missing(sym, *args, &block)
if args.size == 1
# Immediate attribute
value = args.first
- @employee.add_attribute AttributeAction::Immediate.new(sym, value)
+ _add_attribute AttributeAction::Immediate.new(sym, value)
elsif block
# Lazy attribute
- @employee.add_attribute AttributeAction::Block.new(sym, block)
+ _add_attribute AttributeAction::Block.new(sym, block)
elsif args.empty?
# Maybe: #set / #append proxy?
- AssocProxy.new(@employee, sym)
+ AssocProxy.new(@employee, sym, self)
else
super
end
end
+
+ def _add_attribute(action)
+ @employee.add_attribute @filter, action
+ end
end
end

0 comments on commit 4679266

Please sign in to comment.