Class: RspecGem

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_gem.rb,
lib/rspec_gem/version.rb

Constant Summary collapse

VERSION =
"0.2.4"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name_of_gem) ⇒ RspecGem

create task for your gem path - path into directory of your gem name_of_gem - name of your gem



9
10
11
12
13
# File 'lib/rspec_gem.rb', line 9

def initialize(path, name_of_gem)
  @@path = path
  @@name_of_gem = name_of_gem
  load File.expand_path("../tasks/rspec_gem.rake", __FILE__) if defined?(Rake)
end

Class Method Details

.name_of_gemObject



16
17
18
# File 'lib/rspec_gem.rb', line 16

def name_of_gem
  @@name_of_gem
end

.pathObject



20
21
22
# File 'lib/rspec_gem.rb', line 20

def path
  @@path
end

.path_rails_environmentObject



24
25
26
27
# File 'lib/rspec_gem.rb', line 24

def path_rails_environment
  return File.expand_path("config/environment", Rails.root) if defined?(Rails)
  File.expand_path("config/environment", ENV["PWD"])
end

.require_model_environmentObject

require active record with database



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rspec_gem.rb', line 45

def require_model_environment
  require_unit_environment
  ENV["RAILS_ENV"] ||= "test"

  # ActiveRecord
  require "yaml"
  require "active_record"
  ActiveRecord::Base.establish_connection(
    YAML.load(File.read(ENV["PWD"] + "/config/database.yml"))["test"]
  )

  # DatabaseCleaner
  require "database_cleaner"
  RSpec.configure do |config|
    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end
    config.before(:each) do
      DatabaseCleaner.start
    end
    config.after(:each) do
      DatabaseCleaner.clean
    end
  end

  # configurations into database
  ActiveRecord::Base.send(:configurations=, YAML::load(ERB.new(IO.read(ENV["PWD"] + "/config/database.yml")).result))
end

.require_rails_environmentObject

require full rails environment



30
31
32
# File 'lib/rspec_gem.rb', line 30

def require_rails_environment
  require File.expand_path("config/environment", ENV["PWD"])
end

.require_unit_environmentObject

require simply environment without active records



35
36
37
38
39
40
41
42
# File 'lib/rspec_gem.rb', line 35

def require_unit_environment
  require "active_support"
  require "active_support/dependencies"
  %w{ extensions helpers mailers models presenters }.each do |dir|
    ActiveSupport::Dependencies.autoload_paths <<
      File.expand_path("app/#{dir}", ENV["PWD"])
  end
end