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



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

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



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

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

#auto_detected?Boolean

Returns:

  • (Boolean)


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

def auto_detected?
  !!@autodetected
end

#autodetect_ormObject



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

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!



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

def clean
  strategy.clean
end

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



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

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

  strategy.clean
  strategy
end

#cleaning(&block) ⇒ Object



97
98
99
# File 'lib/database_cleaner/base.rb', line 97

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

#create_strategy(*args) ⇒ Object



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

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

#dbObject



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

def db
  @db ||= :default
end

#db=(desired_db) ⇒ Object



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

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

#ormObject



83
84
85
# File 'lib/database_cleaner/base.rb', line 83

def orm
  @orm || autodetect
end

#orm=(desired_orm) ⇒ Object



79
80
81
# File 'lib/database_cleaner/base.rb', line 79

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

#set_strategy_db(strategy, desired_db) ⇒ Object



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

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



87
88
89
# File 'lib/database_cleaner/base.rb', line 87

def start
  strategy.start
end

#strategyObject



75
76
77
# File 'lib/database_cleaner/base.rb', line 75

def strategy
  @strategy ||= NullStrategy
end

#strategy=(args) ⇒ Object



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

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



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

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