Class: Merb

Inherits:
Object show all
Defined in:
lib/xiki/merb.rb

Class Method Summary collapse

Class Method Details

.all(*args) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/xiki/merb.rb', line 203

def self.all *args
  # If 2 args, it's where, model
  where = (args.size == 2) ? "(#{args.shift})" : ''
  model = args.shift
  model.sub! /\/$/, ''
  puts RubyConsole.at(:m, "y #{model}.all#{where}")
end

.console(dir = nil, port = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/xiki/merb.rb', line 129

def self.console dir=nil, port=nil
  b = "*merb console #{self.name_from_dir(dir)}"
  if View.buffer_open?(b)
    View.handle_bar
    View.to_buffer b
  else
    Console.run "merb -i", :dir => dir, :buffer => b
  end
  nil
end

.count(*args) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/xiki/merb.rb', line 211

def self.count *args
  # If 2 args, it's where, model
  where = (args.size == 2) ? "(#{args.shift})" : ''
  model = args.shift
  model.sub! /\/$/, ''
  puts RubyConsole.at(:m, "puts #{model}.count#{where}")
end

.create(dir, port = nil) ⇒ Object



91
92
93
94
95
96
# File 'lib/xiki/merb.rb', line 91

def self.create dir, port=nil
  # Split into dir and name
  dir = self.path_from_dir(dir)
  path, name = dir.match(/(.+)\/(.+)/)[1..2]
  Console.run "merb-gen app -f --no-color #{name}", :dir=>path, :buffer=>"*merb-gen app #{name}" #, :sync=>true
end

.db(dir, port) ⇒ Object



75
76
77
78
79
80
# File 'lib/xiki/merb.rb', line 75

def self.db dir, port
  puts "
    - .db_create
    - .migrate
    "
end

.db_create(dir, port) ⇒ Object



82
83
84
# File 'lib/xiki/merb.rb', line 82

def self.db_create dir, port
  Mysql.create_db self.name_from_dir(dir) + "_development"
end

.files(dir, port = nil) ⇒ Object



37
38
39
# File 'lib/xiki/merb.rb', line 37

def self.files dir, port=nil
  puts "- #{self.path_from_dir(dir)}/"
end

.first(*args) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/xiki/merb.rb', line 195

def self.first *args
  # If 2 args, it's where, model
  where = (args.size == 2) ? "(#{args.shift})" : ''
  model = args.shift
  model.sub! /\/$/, ''
  puts RubyConsole.at(:m, "y #{model}.first#{where}")
end

.generate(dir, port = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/xiki/merb.rb', line 41

def self.generate dir, port=nil
  puts "
    - .merb_gen 'controller posts'
    - .merb_gen 'model post body:string'
    - .merb_gen 'resource post body:string'
    "
end

.initObject



249
250
251
252
253
254
# File 'lib/xiki/merb.rb', line 249

def self.init
  # Jump to controller from line in merb log
  Launcher.add(/^.* ~ Routed to: \{/) do |line|
    Merb.launch_merb_log_line line
  end
end

.launch_merb_log_line(line) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/xiki/merb.rb', line 235

def self.launch_merb_log_line line
  #     if Keys.prefix_u   # If U, just go there
  # Pull out action and controller
  action = line[/"action"=>"(.+?)"/, 1]
  controller = line[/"controller"=>"(.+?)"/, 1]
  # Open controller
  View.open "$co/#{controller}.rb"
  # Jump to method
  View.to_highest
  Search.forward "^\\s-+def #{action}[^a-z_]"
  Line.to_beginning

end


68
69
70
71
72
73
# File 'lib/xiki/merb.rb', line 68

def self.links
  puts "
    - home: http://www.merbivore.com/
    - open-source book: http://book.merbist.com/
    "
end


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xiki/merb.rb', line 5

def self.menu name=nil, port=nil
  unless name  # Print menu
    r = "+ current dir: #{View.dir}, 4000\n"
    r << "+ upper: #{View.dir_of_after_bar}, 4000\n" if View.in_bar?
    r + "
    + tmp: /tmp/merb1, 4000
    + .links/
    - .snippets/
    + .version/
    ".unindent
  else  # Print options
    "
    - .create
    - .start
    - .url '/'
    + .shells/
    + .generate/
    + .rake/
    + .models/
    + .db/
    + .files/
    "
  end
end

.merb_gen(command, dir, port = nil) ⇒ Object



64
65
66
# File 'lib/xiki/merb.rb', line 64

def self.merb_gen command, dir, port=nil
  Console.run "merb-gen #{command} --no-color", :dir => self.path_from_dir(dir), :buffer => "*merb-gen #{dir}"
end

.migrate(dir = nil, port = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/xiki/merb.rb', line 140

def self.migrate dir=nil, port=nil
  b = "*merb console #{self.name_from_dir(dir)}"

  if View.buffer_open?(b)
    View.handle_bar
    View.to_buffer b
  else  # If console not open, open it
    self.console dir
  end

  View.to_buffer(b)
  $el.insert("repository.auto_migrate!")
  #$el.insert("DataMapper::Base.auto_migrate!")
  Console.enter

  # TODO: item for migrating test
  #   - always take env as param?
  #     - default to 'development'
  #   - auto_migrate MERB_ENV=test
end

.models(model = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/xiki/merb.rb', line 172

def self.models model=nil
  # If no model specified, show all
  unless model
    Dir.foreach(Bookmarks['$mo']) { |m|
      next unless m =~ /(.+)\.rb$/
      puts "+ #{TextUtil.camel_case($1)}/"
    }
    return
  end

  # Show options
  puts "
  + .first/
  + .recent 1/
  + .all/
  + .count/
  + .first \":name => 'foo'\"/
  + .recent 1, \":name => 'foo'\"/
  + .all \":name => 'foo'\"/
  + .count \":name => 'foo'\"/
  "
end

.name_from_dir(dir) ⇒ Object



161
162
163
164
# File 'lib/xiki/merb.rb', line 161

def self.name_from_dir dir
  dir = View.dir_of_after_bar if dir == "."
  dir[/(\w+)\/?$/, 1]
end

.path_from_dir(dir) ⇒ Object



166
167
168
169
170
# File 'lib/xiki/merb.rb', line 166

def self.path_from_dir dir
  # If ., grab path of window after bar
  dir = View.dir_of_after_bar if dir == "."
  Bookmarks.expand(dir, :absolute => true)
end

.rake(dir, port, task = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xiki/merb.rb', line 49

def self.rake dir, port, task=nil
  unless task
    #puts self.path_from_dir(dir)
    out = Console.run "rake -T", :dir => self.path_from_dir(dir), :sync => true
    # Pull out tasks
    out.scan(/^rake ([\w:]+)/) do |m|
      puts "- #{m[0]}"
    end
    return
  end

  Console.run "rake #{task}", :dir => self.path_from_dir(dir), :buffer => "*rake #{dir}"

end

.recent(*args) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/xiki/merb.rb', line 219

def self.recent *args
  number = args.shift
  where = (args.size == 2) ? "#{args.shift}, " : ''
  model = args.shift
  model.sub! /\/$/, ''

  number ||= 1

  model.sub! /\/$/, ''
  puts RubyConsole.at(:m, "y #{model}.all(#{where}:order => [:updated_at.desc], :limit => #{number})")
end

.shell(dir, port) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/xiki/merb.rb', line 103

def self.shell dir, port
  Console.run "", :dir=>self.path_from_dir(dir)#, :buffer=>"*merb #{dir}"

  #     buffer = "*" + self.name_from_dir(dir) + " merb shell"

  #     path = self.path_from_dir(dir)
  #     View.to_after_bar

  #     # Just go there if already open
  #     if View.buffer_open? buffer
  #       View.to_buffer buffer
  #       return
  #     end

  #     Console.run nil, :path => path, :buffer => buffer
end

.shells(dir, port = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/xiki/merb.rb', line 30

def self.shells dir, port=nil
  puts "
    - .console
    - .shell
    "
end

.snippetsObject



86
87
88
89
# File 'lib/xiki/merb.rb', line 86

def self.snippets
  puts "- create (o): Foo.create(:a => 'aa')"
  puts "- find first (o): y Foo.first"
end

.start(dir, port) ⇒ Object



98
99
100
101
# File 'lib/xiki/merb.rb', line 98

def self.start dir, port
  Console.run "merb -a thin -p #{port}", :dir=>self.path_from_dir(dir), :buffer=>"*merb #{dir}"
  #     Console.run "merb -p #{port}", :dir=>self.path_from_dir(dir), :buffer=>"*merb #{dir}"
end

.url(path, dir, port) ⇒ Object

def self.url path=‘/’, dir=nil, port

  $el.browse_url "http://localhost:#{port}#{path}"
end


124
125
126
127
# File 'lib/xiki/merb.rb', line 124

def self.url path, dir, port
  $el.browse_url "http://localhost:#{port}#{path}"
  nil
end

.versionObject



231
232
233
# File 'lib/xiki/merb.rb', line 231

def self.version
  Console.run('merb --version', :sync=>true)
end