Class: MongoMapper::Mongration

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

Constant Summary collapse

@@verbose =
true

Class Method Summary collapse

Class Method Details

.announce(message) ⇒ Object



58
59
60
61
62
# File 'lib/mongrations/mongration.rb', line 58

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

.connectionObject



84
85
86
# File 'lib/mongrations/mongration.rb', line 84

def connection
  MongoMapper.connection
end

.down_with_benchmarksObject

:nodoc:



11
12
13
# File 'lib/mongrations/mongration.rb', line 11

def down_with_benchmarks #:nodoc:
  migrate(:down)
end

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



88
89
90
91
92
93
94
95
96
97
# File 'lib/mongrations/mongration.rb', line 88

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongrations/mongration.rb', line 16

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



64
65
66
# File 'lib/mongrations/mongration.rb', line 64

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

.say_with_time(message) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/mongrations/mongration.rb', line 68

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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mongrations/mongration.rb', line 38

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



77
78
79
80
81
82
# File 'lib/mongrations/mongration.rb', line 77

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

.up_with_benchmarksObject

:nodoc:



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

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

.write(text = "") ⇒ Object



54
55
56
# File 'lib/mongrations/mongration.rb', line 54

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