Class: FixtureBackground::Background
- Inherits:
-
Object
- Object
- FixtureBackground::Background
- Defined in:
- lib/fixture_background/background.rb
Instance Attribute Summary collapse
-
#background_block ⇒ Object
readonly
Returns the value of attribute background_block.
Class Method Summary collapse
- .class_by_name(class_name) ⇒ Object
- .class_for_test(full_class_name, background_to_use, test_unit_class) ⇒ Object
- .create_class(full_class_name, parent_class, background_to_use) ⇒ Object
Instance Method Summary collapse
- #ancestors_and_own_background_blocks ⇒ Object
- #background_signature ⇒ Object
- #background_valid? ⇒ Boolean
- #class_for_test ⇒ Object
- #fixture_path ⇒ Object
- #generate! ⇒ Object
-
#initialize(full_class_name, test_unit_class, parent, blk) ⇒ Background
constructor
A new instance of Background.
Constructor Details
#initialize(full_class_name, test_unit_class, parent, blk) ⇒ Background
Returns a new instance of Background.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/fixture_background/background.rb', line 67 def initialize(full_class_name, test_unit_class, parent, blk) @test_unit_class = test_unit_class @full_class_name = full_class_name @parent = parent @background_block = blk FixtureBackground.clean_database! test_unit_class.set_callback(:setup, :before, :reset_active_record_fixture_cache, {:prepend => true}) test_unit_class.set_callback(:setup, :before, :setup_background_ivars) @generator = Generator.new( @full_class_name, background_signature, fixture_path, ancestors_and_own_background_blocks, @test_unit_class ) unless background_valid? end |
Instance Attribute Details
#background_block ⇒ Object (readonly)
Returns the value of attribute background_block.
65 66 67 |
# File 'lib/fixture_background/background.rb', line 65 def background_block @background_block end |
Class Method Details
.class_by_name(class_name) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/fixture_background/background.rb', line 57 def class_by_name(class_name) klass = Module.const_get(class_name) klass.is_a?(Class) && klass rescue NameError return false end |
.class_for_test(full_class_name, background_to_use, test_unit_class) ⇒ Object
14 15 16 17 |
# File 'lib/fixture_background/background.rb', line 14 def class_for_test(full_class_name, background_to_use, test_unit_class) full_class_name = full_class_name.gsub("::", "__") class_by_name(full_class_name) || create_class(full_class_name, test_unit_class, background_to_use) end |
.create_class(full_class_name, parent_class, background_to_use) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fixture_background/background.rb', line 19 def create_class(full_class_name, parent_class, background_to_use) klass = Class.new(parent_class) # Rails infers the Class to be tested by the name of the testcase klass.instance_eval " def name\n \"\#{parent_class.name}\"\n end\n EOT\n \n klass.class_eval <<-EOT\n cattr_accessor :fixture_background\n @@background_generated = false\n @@fixtures_enabled = false\n \n def initialize(*args)\n super\n\n if background = fixture_background\n if !@@background_generated\n background.generate!\n @@background_generated = true\n end\n if !@@fixtures_enabled\n self.class.fixtures :all\n self.class.teardown_suite { FixtureBackground.clean_database! }\n @@fixtures_enabled = true\n end\n end\n end\n EOT\n \n klass.fixture_background = background_to_use\n klass.fixture_path = background_to_use.fixture_path\n \n Object.const_set(full_class_name, klass)\nend\n" |
Instance Method Details
#ancestors_and_own_background_blocks ⇒ Object
87 88 89 |
# File 'lib/fixture_background/background.rb', line 87 def ancestors_and_own_background_blocks (@parent ? @parent.ancestors_and_own_background_blocks : []) << @background_block end |
#background_signature ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/fixture_background/background.rb', line 95 def background_signature stack = caller.reject { |line| line =~ Regexp.new(File.dirname(__FILE__)) } test_file_path = File.(stack.first.match(/^(.+\.rb):/)[1]) block_syntax = '' IO.read(test_file_path).scan(/(?:\A|\n)([ \t]*)background\s(?:do|{)(.*?)\n\1end/m) do |match| block_syntax << match[1].gsub(/\s+/, '') end Digest::MD5.hexdigest(block_syntax) end |
#background_valid? ⇒ Boolean
91 92 93 |
# File 'lib/fixture_background/background.rb', line 91 def background_valid? (IO.read("#{fixture_path}/.version") rescue nil) == background_signature end |
#class_for_test ⇒ Object
109 110 111 |
# File 'lib/fixture_background/background.rb', line 109 def class_for_test self.class.class_for_test(@full_class_name, self, @test_unit_class) end |
#fixture_path ⇒ Object
105 106 107 |
# File 'lib/fixture_background/background.rb', line 105 def fixture_path Rails.root.to_s + "/test/backgrounds/#{@full_class_name.underscore}/" end |
#generate! ⇒ Object
83 84 85 |
# File 'lib/fixture_background/background.rb', line 83 def generate! @generator.generate! if @generator end |