Class: Rasta::Fixture::FixtureLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rasta/fixture_runner.rb

Overview

Load and run a fixture, indicated by the spreadsheet tab name

Instance Method Summary collapse

Constructor Details

#initialize(sheet, loaded_classes) ⇒ FixtureLoader

Returns a new instance of FixtureLoader.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rasta/fixture_runner.rb', line 10

def initialize(sheet, loaded_classes)
  
  # We're making an assumption here
  # that the fixture class matches the 
  # spreadsheet tab name for the fixture
  base_sheet_name = sheet.name.dup
  base_sheet_name.gsub!(/#.*/, '')
  
  # If the tab name has a dotted extension, grab it
  tab_extension = base_sheet_name[/\..*/].sub(/^./,'') if base_sheet_name[/\..*/]
  
  # Remove any extension found from the name 
  base_sheet_name.gsub!(/\..*/, '')
  
  # Get a reference to the loaded class
  classname = find_class_by_name(base_sheet_name, loaded_classes)
  
  # Instantiate the test fixture
  begin
    fixture = classname.new
    fixture.initialize_run(sheet)
    fixture.tab_extension = tab_extension
  rescue ArgumentError => e
    raise ArgumentError, "Unable to load class #{@classname}. Make sure the class includes the Rasta fixture: #{e.inspect + e.backtrace.join("\n")}"
  end
  
  # Run the test fixture
  fixture.run
  fixture = nil
end

Instance Method Details

#find_class_by_name(classname, loaded_classes) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rasta/fixture_runner.rb', line 41

def find_class_by_name(classname, loaded_classes)
  config = Configuration.instance
  ObjectSpace.each_object(Class) do |klass| 
    # don't try to load a class unless it is 
    # something we actually loaded ourselves
    next unless loaded_classes.include?(klass.name)
    if config.module
      return klass if klass.name =~ /#{config.module}::#{classname}$/
    else
      return klass if klass.name =~ /(^|:)#{classname}$/
    end
  end
  raise ArgumentError, "Class '#{config.module}::#{classname}' not found!"
end