Module: RSpecCandy::Switcher

Extended by:
Switcher
Included in:
Switcher
Defined in:
lib/rspec_candy/switcher.rb

Instance Method Summary collapse

Instance Method Details

#active_record_loaded?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rspec_candy/switcher.rb', line 22

def active_record_loaded?
  defined?(ActiveRecord)
end

#active_record_versionObject



18
19
20
# File 'lib/rspec_candy/switcher.rb', line 18

def active_record_version
  ActiveRecord::VERSION::MAJOR
end

#define_matcher(*args, &block) ⇒ Object



49
50
51
# File 'lib/rspec_candy/switcher.rb', line 49

def define_matcher(*args, &block)
  rspec_matcher_registry.define(*args, &block)
end

#new_mock(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec_candy/switcher.rb', line 26

def new_mock(*args)
  case rspec_version
  when 1
    Spec::Mocks::Mock.new(*args)
  when 2
    RSpec::Mocks::Mock.new(*args)
  else
    RSpec::Mocks::Double.new(*args)
  end
end

#rspec_matcher_registryObject



45
46
47
# File 'lib/rspec_candy/switcher.rb', line 45

def rspec_matcher_registry
  rspec_root.const_get(:Matchers)
end

#rspec_rootObject



37
38
39
40
41
42
43
# File 'lib/rspec_candy/switcher.rb', line 37

def rspec_root
  if rspec_version == 1
    Spec
  else
    RSpec
  end
end

#rspec_versionObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rspec_candy/switcher.rb', line 5

def rspec_version
  begin
    require 'rspec/version'
    RSpec::Version::STRING.to_i
  rescue LoadError
    if defined?(Spec)
      1
    else
      raise 'Cannot determine RSpec version'
    end
  end
end