Class: DatabaseCleaner::Base

Inherits:
Object
  • Object
show all
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.



4
5
6
7
8
9
10
11
12
# File 'lib/database_cleaner/base.rb', line 4

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
end

Instance Method Details

#==(other) ⇒ Object

TODO make strategies directly comparable



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

def ==(other)
  self.orm == other.orm && self.db == other.db
end

#auto_detected?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/database_cleaner/base.rb', line 95

def auto_detected?
  !!@autodetected
end

#autodetect_ormObject



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

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!



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

def clean
  strategy.clean
end

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



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

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

  strategy.clean
  strategy
end

#cleaning(&block) ⇒ Object



91
92
93
# File 'lib/database_cleaner/base.rb', line 91

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

#create_strategy(*args) ⇒ Object



31
32
33
34
# File 'lib/database_cleaner/base.rb', line 31

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

#dbObject



27
28
29
# File 'lib/database_cleaner/base.rb', line 27

def db
  @db ||= :default
end

#db=(desired_db) ⇒ Object



14
15
16
17
# File 'lib/database_cleaner/base.rb', line 14

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

#ormObject



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

def orm
  @orm || autodetect
end

#orm=(desired_orm) ⇒ Object



73
74
75
# File 'lib/database_cleaner/base.rb', line 73

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

#set_strategy_db(strategy, desired_db) ⇒ Object



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

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



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

def start
  strategy.start
end

#strategyObject



69
70
71
# File 'lib/database_cleaner/base.rb', line 69

def strategy
  @strategy ||= NullStrategy
end

#strategy=(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/database_cleaner/base.rb', line 54

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



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

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