GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Added --sandbox (-S) option for IRB console

When specified, the console will call open_sandbox! which in turn calls 
any
ORM module's method of the same name (the modules Merb::Orm::* are
considered). It's up to the ORM implementation to start a transaction. 
When
you exit the IRB session, close_sandbox! will rollback any open 
transactions,
again by calling the appropriate ORM implementation of that method. See
Merb::Orms::ActiveRecord for an example.
fabien (author)
Wed Mar 12 13:28:08 -0700 2008
commit  0a15099a76e50b6d3419a81c2acdb6b9214e0a71
tree    1c1a87d1fcec5b1ebcf56effd2eec4e095730b1c
parent  adad648d6c700c7ff450ae7a6f1abf3ca136264a
...
168
169
170
 
 
 
 
171
172
173
...
168
169
170
171
172
173
174
175
176
177
0
@@ -168,6 +168,10 @@ module Merb
0
           opts.on("-i", "--irb-console", "This flag will start merb in irb console mode. All your models and other classes will be available for you in an irb session.") do |console|
0
             options[:adapter] = 'irb'
0
           end
0
+
0
+ opts.on("-S", "--sandbox", "This flag will enable a sandboxed irb console. If your ORM supports transactions, all edits will be rolled back on exit.") do |sandbox|
0
+ options[:sandbox] = true
0
+ end
0
 
0
           opts.on("-l", "--log-level LEVEL", "Log levels can be set to any of these options: debug < info < warn < error < fatal") do |log_level|
0
             options[:log_level] = log_level.to_sym
...
35
36
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
39
40
...
49
50
51
 
52
53
54
55
56
57
 
58
59
 
 
 
 
 
 
60
61
62
63
...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
0
@@ -35,6 +35,38 @@ module Merb
0
         end
0
         nil
0
       end
0
+
0
+ # Starts a sandboxed session (delegates to any Merb::Orms::* modules).
0
+ #
0
+ # An ORM should implement Merb::Orms::MyOrm#open_sandbox! to support this.
0
+ # Usually this involves starting a transaction.
0
+ def open_sandbox!
0
+ puts "Loading #{Merb.environment} environment in sandbox (Merb #{Merb::VERSION})"
0
+ puts "Any modifications you make will be rolled back on exit"
0
+ orm_modules.each { |orm| orm.open_sandbox! if orm.respond_to?(:open_sandbox!) }
0
+ end
0
+
0
+ # Ends a sandboxed session (delegates to any Merb::Orms::* modules).
0
+ #
0
+ # An ORM should implement Merb::Orms::MyOrm#close_sandbox! to support this.
0
+ # Usually this involves rolling back a transaction.
0
+ def close_sandbox!
0
+ orm_modules.each { |orm| orm.close_sandbox! if orm.respond_to?(:close_sandbox!) }
0
+ puts "Modifications have been rolled back"
0
+ end
0
+
0
+ private
0
+
0
+ # ==== Returns
0
+ # Array:: All Merb::Orms::* modules.
0
+ def orm_modules
0
+ if Merb.const_defined?('Orms')
0
+ Merb::Orms.constants.map { |c| Merb::Orms::const_get(c) }
0
+ else
0
+ []
0
+ end
0
+ end
0
+
0
     end
0
 
0
     class Irb
0
@@ -49,14 +81,22 @@ module Merb
0
         m = Merb::Rack::Console.new
0
         Object.send(:define_method, :merb) { m }
0
         ARGV.clear # Avoid passing args to IRB
0
+ m.open_sandbox! if sandboxed?
0
         require 'irb'
0
         require 'irb/completion'
0
         if File.exists? ".irbrc"
0
           ENV['IRBRC'] = ".irbrc"
0
         end
0
         IRB.start
0
+ at_exit do merb.close_sandbox! if sandboxed? end
0
         exit
0
       end
0
+
0
+ private
0
+
0
+ def self.sandboxed?
0
+ Merb::Config[:sandbox]
0
+ end
0
     end
0
   end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.