Class: DatabaseCleaner::Base

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/database_cleaner/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(desired_orm = nil, opts = {}) ⇒ Base

Returns a new instance of Base



11
12
13
14
15
16
17
18
19
20
# File 'lib/database_cleaner/base.rb', line 11

def initialize(desired_orm = nil,opts = {})
  if [:autodetect, nil, "autodetect"].include?(desired_orm)
    autodetect
  else
    self.orm = desired_orm
  end
  self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
  set_default_orm_strategy
  Safeguard.new.run
end

Instance Method Details

#<=>(other) ⇒ Object



7
8
9
# File 'lib/database_cleaner/base.rb', line 7

def <=>(other)
  (self.orm <=> other.orm) == 0 ? self.db <=> other.db : self.orm <=> other.orm
end

#auto_detected?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/database_cleaner/base.rb', line 103

def auto_detected?
  !!@autodetected
end

#autodetect_ormObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/database_cleaner/base.rb', line 107

def autodetect_orm
  if defined? ::ActiveRecord
    :active_record
  elsif defined? ::DataMapper
    :data_mapper
  elsif defined? ::MongoMapper
    :mongo_mapper
  elsif defined? ::Mongoid
    :mongoid
  elsif defined? ::CouchPotato
    :couch_potato
  elsif defined? ::Sequel
    :sequel
  elsif defined? ::Moped
    :moped
  elsif defined? ::Ohm
    :ohm
  elsif defined? ::Redis
    :redis
  elsif defined? ::Neo4j
    :neo4j
  end
end

#cleanObject Also known as: clean!



93
94
95
# File 'lib/database_cleaner/base.rb', line 93

def clean
  strategy.clean
end

#clean_with(*args) ⇒ Object Also known as: clean_with!



44
45
46
47
48
49
50
# File 'lib/database_cleaner/base.rb', line 44

def clean_with(*args)
  strategy = create_strategy(*args)
  set_strategy_db strategy, self.db

  strategy.clean
  strategy
end

#cleaning(&block) ⇒ Object



99
100
101
# File 'lib/database_cleaner/base.rb', line 99

def cleaning(&block)
  strategy.cleaning(&block)
end

#create_strategy(*args) ⇒ Object



39
40
41
42
# File 'lib/database_cleaner/base.rb', line 39

def create_strategy(*args)
  strategy, *strategy_args = args
  orm_strategy(strategy).new(*strategy_args)
end

#dbObject



35
36
37
# File 'lib/database_cleaner/base.rb', line 35

def db
  @db ||= :default
end

#db=(desired_db) ⇒ Object



22
23
24
25
# File 'lib/database_cleaner/base.rb', line 22

def db=(desired_db)
   self.strategy_db = desired_db
   @db = desired_db
end

#ormObject



85
86
87
# File 'lib/database_cleaner/base.rb', line 85

def orm
  @orm || autodetect
end

#orm=(desired_orm) ⇒ Object



81
82
83
# File 'lib/database_cleaner/base.rb', line 81

def orm=(desired_orm)
  @orm = desired_orm.to_sym
end

#set_strategy_db(strategy, desired_db) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/database_cleaner/base.rb', line 54

def set_strategy_db(strategy, desired_db)
  if strategy.respond_to? :db=
    strategy.db = desired_db
  elsif desired_db != :default
    raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
  end
end

#startObject



89
90
91
# File 'lib/database_cleaner/base.rb', line 89

def start
  strategy.start
end

#strategyObject



77
78
79
# File 'lib/database_cleaner/base.rb', line 77

def strategy
  @strategy ||= NullStrategy
end

#strategy=(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/database_cleaner/base.rb', line 62

def strategy=(args)
  strategy, *strategy_args = args
   if strategy.is_a?(Symbol)
      @strategy = create_strategy(*args)
   elsif strategy_args.empty?
     @strategy = strategy
   else
     raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params."
   end

   set_strategy_db @strategy, self.db

   @strategy
end

#strategy_db=(desired_db) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/database_cleaner/base.rb', line 27

def strategy_db=(desired_db)
  if strategy.respond_to? :db=
    strategy.db = desired_db
  elsif desired_db!= :default
    raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
  end
end