sinsiliux-hornsby 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hornsby.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), 'hornsby_context')
2
+
1
3
  class Hornsby
2
4
  @@delete_sql = "DELETE FROM %s"
3
5
 
@@ -6,21 +8,27 @@ class Hornsby
6
8
  end
7
9
 
8
10
  cattr_reader :scenarios
11
+ # @@namespaces = {}
9
12
  @@scenarios = {}
10
13
  @@executed_scenarios = Set.new
11
- cattr_reader :global_scenarios
12
- # @@namespaces = {}
14
+ @@global_executed_scenarios = []
15
+
16
+ @@global_context = HornsbyContext
17
+ @@context = nil
13
18
 
14
19
  def self.configure_rspec(config, options = {})
15
20
  load(options[:filename])
16
21
 
22
+ @@context = @@global_context
17
23
  @@global_scenarios = Hornsby.build(options[:scenarios]) if options[:scenarios]
24
+ @@global_executed_scenarios = @@executed_scenarios.to_a
18
25
 
19
26
  config.include(HornsbySpecHelper)
20
27
 
21
28
  config.before do
22
- @@executed_scenarios = Set.new(@@global_scenarios.collect {|s| s.scenario })
23
- @@global_scenarios.each {|s| s.copy_ivars(self, true)} if @@global_scenarios
29
+ @@context = @@global_context.clone
30
+ @@executed_scenarios = Set.new(@@global_executed_scenarios)
31
+ Hornsby.copy_ivars(self, true)
24
32
  ActiveRecord::Base.connection.increment_open_transactions
25
33
  ActiveRecord::Base.connection.transaction_joinable = false
26
34
  ActiveRecord::Base.connection.begin_db_transaction
@@ -53,13 +61,6 @@ class Hornsby
53
61
  self.new(scenario, &block)
54
62
  end
55
63
 
56
- def self.namespace(name, &block)
57
- end
58
-
59
- def self.reset!
60
- @@scenarios = {}
61
- end
62
-
63
64
  def self.delete_tables
64
65
  tables.each { |t| ActiveRecord::Base.connection.delete(@@delete_sql % t) }
65
66
  end
@@ -72,6 +73,10 @@ class Hornsby
72
73
  %w( schema_info )
73
74
  end
74
75
 
76
+ def self.copy_ivars(to, reload = false)
77
+ @@context.copy_ivars(to, reload)
78
+ end
79
+
75
80
  attr_reader :scenario
76
81
 
77
82
  def initialize(scenario, &block)
@@ -97,32 +102,13 @@ class Hornsby
97
102
  end
98
103
 
99
104
  def build
100
- @context = context = Module.new
101
-
102
- # TODO move this elsewhere
103
- context.module_exec do
104
- def self.method_missing(meth_id, *args, &block)
105
- begin
106
- rec = meth_id.to_s.classify.constantize.send(:create!, *args)
107
- yield(rec) if block_given?
108
- rescue
109
- super
110
- end
111
- end
112
- end
113
-
114
- ivars = context.instance_variables
115
-
116
- build_parent_scenarios(context)
117
- build_scenario(context)
118
-
119
- @context_ivars = context.instance_variables - ivars
120
-
105
+ build_parent_scenarios(@@context)
106
+ build_scenario(@@context)
121
107
  self
122
108
  end
123
109
 
124
110
  def build_scenario(context)
125
- surface_errors { context.module_eval(&@block) } unless @@executed_scenarios.include?(@scenario)
111
+ surface_errors { context.execute(@@context == @@global_context, &@block) } unless @@executed_scenarios.include?(@scenario)
126
112
  @@executed_scenarios << @scenario
127
113
  end
128
114
 
@@ -145,19 +131,12 @@ class Hornsby
145
131
  puts
146
132
  raise error
147
133
  end
148
-
149
- def copy_ivars(to, reload = false)
150
- @context_ivars.each do |iv|
151
- v = @context.instance_variable_get(iv)
152
- v.reload if reload and v.respond_to?(:reload)
153
- to.instance_variable_set(iv, v)
154
- end
155
- end
156
134
  end
157
135
 
158
136
 
159
137
  module HornsbySpecHelper
160
138
  def hornsby_scenario(*names)
161
- Hornsby.build(*names).each {|s| s.copy_ivars(self)}
139
+ Hornsby.build(*names)
140
+ Hornsby.copy_ivars(self)
162
141
  end
163
142
  end
@@ -25,6 +25,14 @@ scenario(:cherry) do
25
25
  @cherry = Fruit.create! :species => 'cherry', :average_diameter => 3
26
26
  end
27
27
 
28
+ scenario(:big_cherry => :cherry) do
29
+ @big_cherry = Fruit.create! :species => @cherry.species, :average_diameter => 7
30
+ end
31
+
32
+ scenario(:cherry_basket => [:big_cherry, :cherry]) do
33
+ @basket = [@cherry, @big_cherry]
34
+ end
35
+
28
36
  # Hornsby.namespace(:pitted_fruit) do
29
37
  # scenario(:peach) do
30
38
  # @peach = Fruit.create! :species => 'peach'
data/spec/hornsby_spec.rb CHANGED
@@ -70,20 +70,27 @@ describe Hornsby, 'with preloaded cherry scenario' do
70
70
  @cherry.update_attribute(:average_diameter, 5)
71
71
  @cherry.average_diameter.should == 5
72
72
  end
73
+
74
+ it "should create big cherry" do
75
+ @big_cherry.species.should == 'cherry'
76
+ end
73
77
  end
74
78
 
75
79
  describe Hornsby, 'with many apples scenario' do
76
80
  before do
77
- hornsby_scenario :many_apples, :cherry
81
+ hornsby_scenario :many_apples, :cherry, :cherry_basket
78
82
  end
79
83
 
80
84
  it "should create only one apple" do
81
- puts Hornsby.send(:class_variable_get, :@@executed_scenarios).inspect
82
85
  Fruit.all(:conditions => 'species = "apple"').count.should == 1
83
86
  end
84
87
 
85
- it "should create only one cherry even if it was preloaded" do
86
- Fruit.all(:conditions => 'species = "cherry"').count.should == 1
88
+ it "should create only two cherries even if they were preloaded" do
89
+ Fruit.all(:conditions => 'species = "cherry"').count.should == 2
90
+ end
91
+
92
+ it "should contain cherries in basket if basket is loaded in test and cherries preloaded" do
93
+ @basket.should == [@cherry, @big_cherry]
87
94
  end
88
95
  end
89
96
 
data/spec/spec_helper.rb CHANGED
@@ -4,17 +4,17 @@ require 'activerecord'
4
4
  spec_dir = File.dirname(__FILE__)
5
5
  Dir.chdir spec_dir
6
6
 
7
- ActiveRecord::Base.logger = Logger.new(spec_dir + "/debug.log")
7
+ ActiveRecord::Base.logger = Logger.new("debug.log")
8
8
 
9
- databases = YAML::load(IO.read(spec_dir + "/db/database.yml"))
9
+ databases = YAML::load(IO.read("db/database.yml"))
10
10
  db_info = databases[ENV["DB"] || "test"]
11
11
  ActiveRecord::Base.establish_connection(db_info)
12
- load(File.join(spec_dir, "db", "schema.rb"))
12
+ load(File.join("db", "schema.rb"))
13
13
 
14
14
  require 'spec/autorun'
15
15
  require '../lib/hornsby'
16
16
  require 'db/fruit'
17
17
 
18
18
  Spec::Runner.configure do |config|
19
- Hornsby.configure_rspec(config, :filename => File.join(spec_dir, 'hornsby_scenario.rb'), :scenarios => :cherry)
19
+ Hornsby.configure_rspec(config, :filename => File.join('hornsby_scenario.rb'), :scenarios => :big_cherry)
20
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinsiliux-hornsby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrius Chamentauskas