Permalink
Browse files

Instance variables created in a dataset block are available in nested…

… contexts
  • Loading branch information...
1 parent 2bfc1c6 commit fb5d57260758b7fe3f905f950c2851c60da8561a @aiwilliams committed Jan 2, 2009
View
@@ -4,3 +4,6 @@
* Birthday!
+===
+
+* Sub-contexts get instance variables from ancestors
View
@@ -1,4 +1,5 @@
require 'dataset/version'
+require 'dataset/instance_methods'
require 'dataset/base'
require 'dataset/database/base'
require 'dataset/database/mysql'
@@ -76,13 +77,13 @@ def self.included(test_context) # :nodoc:
raise "I don't understand your test framework"
end
- test_context.extend ClassMethods
+ test_context.extend ContextClassMethods
end
# Methods that are added to the class that Dataset is included in (the test
# context class).
#
- module ClassMethods
+ module ContextClassMethods
def self.extended(context_class) # :nodoc:
context_class.module_eval do
include InstanceMethods
@@ -99,7 +100,7 @@ def self.extended(context_class) # :nodoc:
# Dataset::Database::Base).
def datasets_directory(path)
Dataset::Resolver.default = Dataset::DirectoryResolver.new(path)
- Dataset::ClassMethods.datasets_database_dump_path = File.join(path, '/tmp/dataset')
+ Dataset::ContextClassMethods.datasets_database_dump_path = File.join(path, '/tmp/dataset')
end
def add_dataset(*datasets, &block) # :nodoc:
@@ -114,20 +115,9 @@ def dataset_session_in_hierarchy # :nodoc:
self.dataset_session ||= begin
database_spec = ActiveRecord::Base.configurations['test'].with_indifferent_access
database_class = Dataset::Database.const_get(database_spec[:adapter].classify)
- database = database_class.new(database_spec, Dataset::ClassMethods.datasets_database_dump_path)
+ database = database_class.new(database_spec, Dataset::ContextClassMethods.datasets_database_dump_path)
Dataset::Session.new(database)
end
end
end
-
- module InstanceMethods # :nodoc:
- def extend_from_dataset_load(load)
- load.dataset_binding.block_variables.each do |k,v|
- instance_variable_set(k, v)
- end
- self.extend load.dataset_binding.record_methods
- self.extend load.dataset_binding.model_finders
- self.extend load.helper_methods
- end
- end
end
View
@@ -121,7 +121,7 @@ def load; end
# be available to your test methods. You have to be careful with this in a
# similar way that you must with an RSpec before :all block. Since the
# instance variables are pointing to the same instances accross all tests,
- # things can get weird if you intend to change their state. It's best use if
+ # things can get weird if you intend to change their state. It's best use is
# for loading objects that you want to read a lot without loading over and
# over again for each test.
#
@@ -146,11 +146,12 @@ def load; end
#
# When you need to go beyond the block, create a Dataset::Base subclass!
class Block < Base
+ include Dataset::InstanceMethods
+
def load # :nodoc:
+ dataset_session_binding.install_block_variables(self)
doload
- instance_variables.each do |name|
- dataset_session_binding.block_variables[name] = instance_variable_get(name)
- end
+ dataset_session_binding.copy_block_variables(self)
end
end
end
@@ -0,0 +1,10 @@
+module Dataset
+ module InstanceMethods # :nodoc:
+ def extend_from_dataset_load(load)
+ load.dataset_binding.install_block_variables(self)
+ self.extend load.dataset_binding.record_methods
+ self.extend load.dataset_binding.model_finders
+ self.extend load.helper_methods
+ end
+ end
+end
View
@@ -41,7 +41,7 @@ class Reload # :nodoc:
def initialize(load)
@load = load
- @dataset_binding = SessionBinding.new(load.dataset_binding)
+ @dataset_binding = SessionBinding.new(@load.dataset_binding)
end
end
end
@@ -170,6 +170,12 @@ def initialize(database_or_parent_binding)
end
end
+ def copy_block_variables(dataset_block)
+ dataset_block.instance_variables.each do |name|
+ self.block_variables[name] = dataset_block.instance_variable_get(name)
+ end
+ end
+
def create_model(record_type, *args)
insert(Dataset::Record::Model, record_type, *args)
end
@@ -200,6 +206,12 @@ def find_model(record_type, symbolic_name)
end
end
+ def install_block_variables(target)
+ block_variables.each do |k,v|
+ target.instance_variable_set(k,v)
+ end
+ end
+
def name_model(record, symbolic_name)
record_class = record.class.base_class
@model_finders.create_finder(record_class) unless @symbolic_names_to_ids.has_key?(record_class)
@@ -7,14 +7,14 @@
dataset.environments_root_path = "#{PLUGIT_ROOT}/environments"
vendor_directory = "#{PLUGIT_ROOT}/../vendor/plugins"
- dataset.environment :default, 'Edge versions of Rails and RSpec' do |env|
+ dataset.environment :default, 'Released versions of Rails and RSpec' do |env|
env.library :rails, :export => "git clone git://github.com/rails/rails.git" do |rails|
- rails.before_install { `git pull` }
+ rails.after_update { `git fetch origin 2-2-stable:2-2-stable; git co 2-2-stable` }
rails.load_paths = %w{/activesupport/lib /activerecord/lib /actionpack/lib}
rails.requires = %w{active_support active_record active_record/fixtures action_controller action_view}
end
env.library :rspec, :export => "git clone git://github.com/dchelimsky/rspec.git" do |rspec|
- rspec.after_update { `git pull && mkdir -p #{vendor_directory} && ln -sF #{File.expand_path('.')} #{vendor_directory + '/rspec'}` }
+ rspec.after_update { `git co -b rspecrelease 1.1.11 && mkdir -p #{vendor_directory} && ln -sF #{File.expand_path('.')} #{vendor_directory + '/rspec'}` }
rspec.requires = %w{spec}
end
end
@@ -113,6 +113,23 @@ def test_two; end
value_in_test.should == 'Hello'
end
+ it 'should copy instance variables from block to subclass blocks' do
+ value_in_subclass_block = nil
+ testcase = Class.new(Test::Unit::TestCase) do
+ dataset do
+ @myvar = 'Hello'
+ end
+ end
+ subclass = Class.new(testcase) do
+ dataset do
+ value_in_subclass_block = @myvar
+ end
+ end
+
+ run_testcase(subclass)
+ value_in_subclass_block.should == 'Hello'
+ end
+
it 'should load the dataset when the suite is run' do
load_count = 0
dataset = Class.new(Dataset::Base) do
View
@@ -6,7 +6,7 @@ namespace :db do
dataset_names = (ENV['DATASETS'] || 'default').split(',')
context = Class.new do
- extend Dataset::ClassMethods
+ extend Dataset::ContextClassMethods
datasets_directory [
"#{RAILS_ROOT}/spec/datasets",
"#{RAILS_ROOT}/test/datasets"

1 comment on commit fb5d572

@seancribbs

One thing I’ve had to work around in the Radiant code is where the datasets dump path is, especially when running in instance mode (where the user may not have permission to modify the gem). Why not use something based off of Dir.tmpdir?

Please sign in to comment.