Froze rails gems
[depot.git] / vendor / rails / railties / lib / rails_generator / manifest.rb
1 module Rails
2 module Generator
3
4 # Manifest captures the actions a generator performs. Instantiate
5 # a manifest with an optional target object, hammer it with actions,
6 # then replay or rewind on the object of your choice.
7 #
8 # Example:
9 # manifest = Manifest.new { |m|
10 # m.make_directory '/foo'
11 # m.create_file '/foo/bar.txt'
12 # }
13 # manifest.replay(creator)
14 # manifest.rewind(destroyer)
15 class Manifest
16 attr_reader :target
17
18 # Take a default action target. Yield self if block given.
19 def initialize(target = nil)
20 @target, @actions = target, []
21 yield self if block_given?
22 end
23
24 # Record an action.
25 def method_missing(action, *args, &block)
26 @actions << [action, args, block]
27 end
28
29 # Replay recorded actions.
30 def replay(target = nil)
31 send_actions(target || @target, @actions)
32 end
33
34 # Rewind recorded actions.
35 def rewind(target = nil)
36 send_actions(target || @target, @actions.reverse)
37 end
38
39 # Erase recorded actions.
40 def erase
41 @actions = []
42 end
43
44 private
45 def send_actions(target, actions)
46 actions.each do |method, args, block|
47 target.send(method, *args, &block)
48 end
49 end
50 end
51
52 end
53 end