Class: MongoMapper::Mongration

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper/mongration.rb

Constant Summary collapse

@@verbose =
true

Class Method Summary collapse

Class Method Details

.announce(message) ⇒ Object



54
55
56
57
58
# File 'lib/mongo_mapper/mongration.rb', line 54

def announce(message)
  text = "#{@version} #{name}: #{message}"
  length = [0, 75 - text.length].max
  write "== %s %s" % [text, "=" * length]
end

.connectionObject



80
81
82
# File 'lib/mongo_mapper/mongration.rb', line 80

def connection
  MongoMapper.connection
end

.method_missing(method, *arguments, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/mongo_mapper/mongration.rb', line 84

def method_missing(method, *arguments, &block)
  arg_list = arguments.map(&:inspect) * ', '

  say_with_time "#{method}(#{arg_list})" do
    unless arguments.empty? || method == :execute
      arguments[0] = Migrator.proper_table_name(arguments.first)
    end
    connection.send(method, *arguments, &block)
  end
end

.migrate(direction) ⇒ Object

Execute this migration in the named direction



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mongo_mapper/mongration.rb', line 12

def migrate(direction)
  return unless respond_to?(direction)

  case direction
    when :up   then announce "migrating"
    when :down then announce "reverting"
  end

  result = nil
  time = Benchmark.measure { result = send("#{direction}_without_benchmarks") }

  case direction
    when :up   then announce "migrated (%.4fs)" % time.real; write
    when :down then announce "reverted (%.4fs)" % time.real; write
  end

  result
end

.say(message, subitem = false) ⇒ Object



60
61
62
# File 'lib/mongo_mapper/mongration.rb', line 60

def say(message, subitem=false)
  write "#{subitem ? "   ->" : "--"} #{message}"
end

.say_with_time(message) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/mongo_mapper/mongration.rb', line 64

def say_with_time(message)
  say(message)
  result = nil
  time = Benchmark.measure { result = yield }
  say "%.4fs" % time.real, :subitem
  say("#{result} rows", :subitem) if result.is_a?(Integer)
  result
end

.singleton_method_added(sym) ⇒ Object

Because the method added may do an alias_method, it can be invoked recursively. We use @ignore_new_methods as a guard to indicate whether it is safe for the call to proceed.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mongo_mapper/mongration.rb', line 34

def singleton_method_added(sym) #:nodoc:
  return if defined?(@ignore_new_methods) && @ignore_new_methods

  begin
    @ignore_new_methods = true

    case sym
      when :up, :down
        klass = (class << self; self; end)
        klass.send(:alias_method_chain, sym, "benchmarks")
    end
  ensure
    @ignore_new_methods = false
  end
end

.suppress_messagesObject



73
74
75
76
77
78
# File 'lib/mongo_mapper/mongration.rb', line 73

def suppress_messages
  save, self.verbose = verbose, false
  yield
ensure
  self.verbose = save
end

.up_with_benchmarksObject

:nodoc:



7
8
9
# File 'lib/mongo_mapper/mongration.rb', line 7

def up_with_benchmarks #:nodoc:
  migrate(:up)
end

.write(text = "") ⇒ Object



50
51
52
# File 'lib/mongo_mapper/mongration.rb', line 50

def write(text="")
  puts(text) if verbose
end