lib/hornsby.rb in sinsiliux-hornsby-0.2.0 vs lib/hornsby.rb in sinsiliux-hornsby-0.2.1
- old
+ new
@@ -1,28 +1,36 @@
+require File.join(File.dirname(__FILE__), 'hornsby_context')
+
class Hornsby
@@delete_sql = "DELETE FROM %s"
def self.framework_root
RAILS_ROOT rescue Rails.root rescue Merb.root rescue ''
end
cattr_reader :scenarios
+ # @@namespaces = {}
@@scenarios = {}
@@executed_scenarios = Set.new
- cattr_reader :global_scenarios
- # @@namespaces = {}
+ @@global_executed_scenarios = []
+ @@global_context = HornsbyContext
+ @@context = nil
+
def self.configure_rspec(config, options = {})
load(options[:filename])
+ @@context = @@global_context
@@global_scenarios = Hornsby.build(options[:scenarios]) if options[:scenarios]
+ @@global_executed_scenarios = @@executed_scenarios.to_a
config.include(HornsbySpecHelper)
config.before do
- @@executed_scenarios = Set.new(@@global_scenarios.collect {|s| s.scenario })
- @@global_scenarios.each {|s| s.copy_ivars(self, true)} if @@global_scenarios
+ @@context = @@global_context.clone
+ @@executed_scenarios = Set.new(@@global_executed_scenarios)
+ Hornsby.copy_ivars(self, true)
ActiveRecord::Base.connection.increment_open_transactions
ActiveRecord::Base.connection.transaction_joinable = false
ActiveRecord::Base.connection.begin_db_transaction
end
@@ -51,17 +59,10 @@
def self.scenario(scenario, &block)
self.new(scenario, &block)
end
- def self.namespace(name, &block)
- end
-
- def self.reset!
- @@scenarios = {}
- end
-
def self.delete_tables
tables.each { |t| ActiveRecord::Base.connection.delete(@@delete_sql % t) }
end
def self.tables
@@ -70,10 +71,14 @@
def self.skip_tables
%w( schema_info )
end
+ def self.copy_ivars(to, reload = false)
+ @@context.copy_ivars(to, reload)
+ end
+
attr_reader :scenario
def initialize(scenario, &block)
case scenario
when Hash
@@ -95,36 +100,17 @@
def say(*messages)
puts messages.map { |message| "=> #{message}" }
end
def build
- @context = context = Module.new
-
- # TODO move this elsewhere
- context.module_exec do
- def self.method_missing(meth_id, *args, &block)
- begin
- rec = meth_id.to_s.classify.constantize.send(:create!, *args)
- yield(rec) if block_given?
- rescue
- super
- end
- end
- end
-
- ivars = context.instance_variables
-
- build_parent_scenarios(context)
- build_scenario(context)
-
- @context_ivars = context.instance_variables - ivars
-
+ build_parent_scenarios(@@context)
+ build_scenario(@@context)
self
end
def build_scenario(context)
- surface_errors { context.module_eval(&@block) } unless @@executed_scenarios.include?(@scenario)
+ surface_errors { context.execute(@@context == @@global_context, &@block) } unless @@executed_scenarios.include?(@scenario)
@@executed_scenarios << @scenario
end
def build_parent_scenarios(context)
@parents.each do |p|
@@ -143,21 +129,14 @@
puts
puts error.backtrace
puts
raise error
end
-
- def copy_ivars(to, reload = false)
- @context_ivars.each do |iv|
- v = @context.instance_variable_get(iv)
- v.reload if reload and v.respond_to?(:reload)
- to.instance_variable_set(iv, v)
- end
- end
end
module HornsbySpecHelper
def hornsby_scenario(*names)
- Hornsby.build(*names).each {|s| s.copy_ivars(self)}
+ Hornsby.build(*names)
+ Hornsby.copy_ivars(self)
end
end
\ No newline at end of file