Class: Fit::FixtureLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/fit/fixture_loader.rb

Constant Summary collapse

@@fixture_packages =
Set.new(['Fit::'])

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_fixture_package(module_name) ⇒ Object

This method adds the name of a module as a ‘package’ we should search for fixtures. Supports import_fixture



76
77
78
# File 'lib/fit/fixture_loader.rb', line 76

def FixtureLoader.add_fixture_package module_name
  @@fixture_packages << module_name + '::'
end

.fixture_packagesObject



79
80
81
# File 'lib/fit/fixture_loader.rb', line 79

def FixtureLoader.fixture_packages
  @@fixture_packages
end

Instance Method Details

#find_class(name) ⇒ Object

Try to load the named class. We first see if it’s already loaded by looking for the class name. If not, we convert the class name to a file name (by changing ‘::’ to ‘/’, then try to require that file. We then look for the constant again. This means that the class Example::Sqrt must be in the file Example/sqrt.rb or the file example/sqrt.rb.



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
# File 'lib/fit/fixture_loader.rb', line 26

def find_class name
  klass = find_constant name
  unless klass
    ([''] + @@fixture_packages.to_a).detect do |prefix|
      file_path = (prefix + name).gsub(/::/, '/').gsub(/\./, '/')
      classname = basename = File::basename(file_path)
      if basename.index('$')
        basename_parts = basename.split(/\$/)
        basename = basename_parts[0]
        classname = basename_parts.join('::')
      end
      file_basename = basename.split(/([A-Z][^A-Z]+)/).delete_if {|e| e.empty?}.collect {|e| e.downcase!}.join('_')
      file_dirname = File::dirname(file_path)
      file_name = (file_dirname == '.' ? '' : file_dirname + '/') + file_basename

      require_file file_name

      if file_dirname == '.'
        klass_name = classname
      else
        klass_name = File::dirname(file_path).split(%r{/}).collect { |e|
          e.index(/[A-Z]/).nil? ? e.capitalize : e
        }.join('::') + "::#{classname}"
      end
      klass = find_constant klass_name
    end
  end
  klass
end

#find_constant(name) ⇒ Object



56
57
58
59
60
61
# File 'lib/fit/fixture_loader.rb', line 56

def find_constant name
  class_name = name.gsub '.', '::'
  return class_name.split('::').inject(Object) { |par, const| par.const_get(const) }
rescue NameError
  return nil
end

#find_fixture_class(name) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/fit/fixture_loader.rb', line 12

def find_fixture_class name
  camelizedName = (name.split(/[^a-zA-Z0-9.:$]/).collect { |word| first = word.slice!(0,1).upcase; first + word }).join.chomp('.')
  klasses = ([camelizedName, camelizedName + 'Fixture'].collect { |n| find_class(n) }).compact
  raise "Fixture #{name} not found." if klasses.length == 0
  klass = klasses.find { |k| k < Fixture }
  raise "#{name} is not a fixture." unless klass
  klass
end

#load(name) ⇒ Object



7
8
9
10
# File 'lib/fit/fixture_loader.rb', line 7

def load name
  klass = find_fixture_class name
  klass.new
end

#require_file(file_name) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/fit/fixture_loader.rb', line 63

def require_file file_name
  begin
    require file_name.downcase
  rescue LoadError
    require file_name
  end
rescue LoadError
  #raise "Couldn't load file #{file_name} or file #{file_name.downcase}"
end