Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_extensions_plugins / plugins / scenarios / lib / scenarios / .svn / text-base / base.rb.svn-base
blob3885af3c464629ef8c9d8e3534d91714ee24ac87
1 module Scenarios
2   class Base
3     class << self
4       # Class method to load the scenario. Used internally by the Scenarios
5       # plugin.
6       def load
7         new.load_scenarios(used_scenarios + [self])
8       end
9       
10       # Class method for your own scenario to define helper methods that will
11       # be included into the scenario and all specs that include the scenario
12       def helpers(&block)
13         mod = (const_get(:Helpers) rescue const_set(:Helpers, Module.new))
14         mod.module_eval(&block) if block_given?
15         mod
16       end
17       
18       # Class method for your own scenario to define the scenarios that it
19       # depends on. If your scenario depends on other scenarios those
20       # scenarios will be loaded before the load method on your scenario is
21       # executed.
22       def uses(*scenarios)
23         names = scenarios.map(&:to_scenario).reject { |n| used_scenarios.include?(n) }
24         used_scenarios.concat(names)
25       end
26       
27       # Class method that returns the scenarios used by your scenario.
28       def used_scenarios # :nodoc:
29         @used_scenarios ||= []
30         @used_scenarios = (@used_scenarios.collect(&:used_scenarios) + @used_scenarios).flatten.uniq
31       end
32       
33       # Returns the scenario class.
34       def to_scenario
35         self
36       end
37     end
38     
39     include TableMethods
40     include Loading
41     
42     attr_reader :table_config
43     
44     # Initialize a scenario with a Configuration. Used internally by the
45     # Scenarios plugin.
46     def initialize(config = Configuration.new)
47       @table_config = config
48       table_config.update_scenario_helpers self.class
49       self.extend table_config.table_readers
50       self.extend table_config.scenario_helpers
51     end
52     
53     # This method should be implemented in your scenarios. You may also have
54     # scenarios that simply use other scenarios, so it is not required that
55     # this be overridden.
56     def load
57     end
58     
59     # Unload a scenario, sort of. This really only deletes the records, all of
60     # them, of every table this scenario modified. The goal is to maintain a
61     # clean database for successive runs. Used internally by the Scenarios
62     # plugin.
63     def unload
64       return if unloaded?
65       record_metas.each_value { |meta| blast_table(meta.table_name) }
66       @unloaded = true
67     end
68     
69     def unloaded?
70       @unloaded == true
71     end
72   end
73 end