rmvc 3.2 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a074178961a40fa312e5f40179a283ee06294539
4
- data.tar.gz: a3b776a10817041bf42d545adcbf1cdcceddf54f
3
+ metadata.gz: 92a2ddb01efc0130d4abb6d5fbf829de610e62b7
4
+ data.tar.gz: d3dc0ce1e8990e7f4cfee0601a206510d45d8601
5
5
  SHA512:
6
- metadata.gz: b606ac40eca61031bb0c0114914bf2c519209de88cd05a5cf83ddf882d1f76847f1324f28fde031f31546efe9d51c169218a758a6aa61daaf736969fcbfc51fe
7
- data.tar.gz: 925c0b6e0a0cb92e906f444a611d40546c534b104d0447fdd2c002b75088bac9f1d594782b3094e08e9713a9d36c447ebfaad73b47666f7ae9d976aabd277ee6
6
+ metadata.gz: c44bf1e269eec605bfda5bce1032eba05a2c72528abd5b31816fc89b400eac75b2efa1e90ace4f1ac20d31beeee5032a1c7ec14b1e3a4c139f5e68ada6c2d914
7
+ data.tar.gz: f94d7c041601ded15057a52210ca2aeafe1bf5aafa5e05dc8e2651db3aabaf64616b0be9f570508d3b83c8db806e344c558f50ce3d46fa62301b64dcce2151d4
data/lib/rmvc.rb CHANGED
@@ -1,379 +1,4 @@
1
- #!/usr/bin/env ruby
2
- require 'colorize'
3
- require 'sqlite3'
4
- module RMVC
5
- # Interface
6
- class Interface
7
- # Startup
8
- def self.startup(args)
9
- if (args != "none") then
10
- RMVC::Interface.run(args)
11
- else
12
- # No ARGS
13
- RMVC::Interface.showHelp
14
- end
15
- end
16
-
17
- # Run a command
18
- def self.run(args)
19
- fullArgs = args.join(" ")
20
- # Validate first argument
21
- if args[0] == "new" then
22
- # Create a new project
23
- if (args.length >= 2) then
24
- RMVC::Core.create(args[1])
25
- else
26
- puts "Wrong syntax for 'new'!"
27
- RMVC::Interface.showHelp
28
- end
29
- elsif args[0] == "migrate"
30
- if args.length < 2
31
- puts "Wrong syntax for 'migrate'!"
32
- RMVC::Interface.showHelp
33
- else
34
- RMVC::Core.migrate(args[1])
35
- end
36
- elsif args[0] == "generate"
37
- # Generate code
38
- if (args.length < 3) then
39
- puts "Wrong syntax for 'generate'!"
40
- RMVC::Interface.showHelp
41
- elsif (args[1] == "view" && args.length < 4)
42
- puts "Wrong syntax for 'generate view'!"
43
- RMVC::Interface.showHelp
44
- elsif (args[1] == "migration" && args.length < 4)
45
- puts "Wrong syntax for 'generate migration'!"
46
- RMVC::Interface.showHelp
47
- else
48
- RMVC::Core.generate(args)
49
- end
50
- else
51
- # Unknown command
52
- RMVC::Interface.showHelp
53
- end
54
- end
55
-
56
- # Show help
57
- def self.showHelp
58
- puts "You introduced an invalid command, or you've ran rmvc without arguments."
59
- puts "Syntax: rmvc [command] [arguments ...]"
60
- puts "The following commands are available: "
61
- puts ""
62
- puts " new [ProjectName]".yellow
63
- puts " where [ProjectName] is the name of your new project".green
64
- puts " Creates a new project using the RMVC architecture."
65
- puts ""
66
- puts " generate [model|controller|view|migration] [name] [controllerName|dbName]".yellow
67
- puts " where [name] is the name of the model/controller/view".green
68
- puts " Creates a new model/controller/view with the desired name."
69
- puts " If a controller is created, it will automatically be linked (through require) to a model "
70
- puts " and a view of the same name than the controller."
71
- puts " If a model is created, it will be linked (through require) to a controller of the same name."
72
- puts " If a view is created, you must specify [controllerName]. It will be linked (through require) to it."
73
- puts " If a migration is created, a file in /app/migrations will be created. A database must be specified."
74
- puts ""
75
- puts " migrate [migration]".yellow
76
- puts " where [migration] is the name of the migration previously created.".green
77
- puts " Runs the queries written in a migration file to a database."
78
- puts " If the database doesn't exist, it will be created."
79
- puts ""
80
- end
81
- end
82
-
83
- # Core (commands)
84
- class Core
85
- def self.migrate(mName)
86
- # Check if we're in a RMVC project
87
- if (!File.directory? ".rmvc") then
88
- puts "error".red + " Not a RMVC project! (missing directory .rmvc)"
89
- exit
90
- end
91
-
92
- # Check if the migration exists
93
- unless File.exists? "app/migrations/#{mName}.rb"
94
- puts "error".red + " The migration #{mName} doesn't exist!"
95
- exit
96
- end
97
-
98
- # Run the file
99
- puts "attempting to migrate #{mName}...".yellow
100
- sta = system ("ruby app/migrations/#{mName}.rb")
101
- if sta
102
- puts "#{mName} migrated correctly!".green
103
- else
104
- puts "error while migrating #{mName}".red
105
- end
106
- end
107
-
108
- def self.generate(args)
109
- # Check if we're in a RMVC project
110
- if (!File.directory? ".rmvc") then
111
- puts "error".red + " Not a RMVC project! (missing directory .rmvc)"
112
- exit
113
- end
114
- if (args[1] == "model") then
115
- puts "Generating model #{args[2]}..."
116
- # Create file /app/models/args[2].rb
117
- if (File.exists? "app/models/#{args[2].downcase}.rb") then
118
- puts "error".red + " File /app/models/#{args[2].downcase}.rb already exists."
119
- exit
120
- end
121
- File.open("app/models/#{args[2].downcase}.rb", "w") do |f|
122
- f.write(RMVC::Helpers.createModel(args[2]))
123
- end
124
- puts "create".green + " File /app/models/#{args[2].downcase}.rb"
125
- elsif (args[1] == "controller")
126
- puts "Generating controller #{args[2]}..."
127
- # Create file /app/controllers/args[2]_controller.rb
128
- if (File.exists? "app/controllers/#{args[2].downcase}_controller.rb") then
129
- puts "error".red + " File /app/controller/#{args[2].downcase}_controller.rb already exists."
130
- exit
131
- end
132
- File.open("app/controllers/#{args[2].downcase}_controller.rb", "w") do |f|
133
- f.write(RMVC::Helpers.createController(args[2]))
134
- end
135
- puts "create".green + " File /app/controllers/#{args[2].downcase}_controller.rb"
136
- puts "notice".yellow + " Remember to add a require statement to the main file of the project, to include app/controllers/#{args[2].downcase}_controller."
137
- elsif (args[1] == "view")
138
- puts "Generating view #{args[2]}..."
139
- # Create file /app/controllers/args[2]_controller.rb
140
- if (File.exists? "app/views/#{args[2].downcase}.rb") then
141
- puts "error".red + " File /app/views/#{args[2].downcase}.rb already exists."
142
- exit
143
- end
144
- File.open("app/views/#{args[2].downcase}.rb", "w") do |f|
145
- f.write(RMVC::Helpers.createView(args[2], args[3]))
146
- end
147
- puts "create".green + " File /app/views/#{args[2].downcase}.rb"
148
- elsif (args[1] == "migration")
149
- puts "Generating migration #{args[2]}..."
150
- # Create file /app/migrations/args[2].rb
151
- if (File.exists? "app/migrations/#{args[2].downcase}.rb") then
152
- puts "error".red + " File /app/migrations/#{args[2].downcase}.rb already exists."
153
- exit
154
- end
155
- File.open("app/migrations/#{args[2].downcase}.rb", "w") do |f|
156
- f.write(RMVC::Helpers.createMigration(args[2], args[3]))
157
- end
158
- puts "create".green + " File /app/migrations/#{args[2].downcase}.rb"
159
- else
160
- puts "Wrong argument for generate: #{args[1]}"
161
- RMVC::Interface.showHelp
162
- end
163
- end
164
- # CMD: new [ProjectName]
165
- def self.create(projectname)
166
- puts "Creating new project: #{projectname}"
167
- puts ""
168
- # Check if directory exists
169
- if (File.directory? projectname) then
170
- puts "error".red + " Directory #{projectname} already exists."
171
- exit
172
- end
173
- # Try to create it
174
- if (!Dir.mkdir(projectname)) then
175
- puts "error".red + " Error while creating the directory #{projectname}"
176
- exit
177
- else
178
- puts "create".green + " Directory #{projectname}"
179
- end
180
- # Create directory .rmvc
181
- Dir.mkdir(projectname + "/.rmvc")
182
- puts "create".green + " Directory #{projectname}/.rmvc"
183
- # Create file projectname.rb
184
- File.open(projectname + "/" + projectname + ".rb", "w") do |f|
185
- f.write("require './app/controllers/default_controller'\n")
186
- f.write("DefaultController.main\n")
187
- end
188
- puts "create".green + " File #{projectname}.rb"
189
-
190
- # Create directory app
191
- Dir.mkdir(projectname + "/app")
192
- puts "create".green + " Directory /app"
193
- # Create directory app/controllers
194
- Dir.mkdir(projectname + "/app/controllers")
195
- puts "create".green + " Directory /app/controllers"
196
- # Create default controller
197
- File.open(projectname + "/app/controllers/default_controller.rb", "w") do |f|
198
- f.write("require './app/models/default'\n")
199
- f.write("require './app/views/main'\n")
200
- f.write("class DefaultController\n")
201
- f.write(" class << self\n")
202
- f.write(" #Add your variables here!\n")
203
- f.write(" attr_accessor :name\n")
204
- f.write(" end\n")
205
- f.write(" @name = \"James!\"\n")
206
- f.write(" def self.main\n")
207
- f.write(" MainView.load\n")
208
- f.write(" end\n")
209
- f.write("end\n")
210
- end
211
- puts "create".green + " File /app/controllers/default_controller.rb"
212
- # Create directory app/views
213
- Dir.mkdir(projectname + "/app/views")
214
- puts "create".green + " Directory /app/views"
215
- # Create /app/views/main.rb
216
- File.open(projectname + "/app/views/main.rb", "w") do |f|
217
- f.write("require './app/controllers/default_controller'\n")
218
- f.write("class MainView\n")
219
- f.write(" def self.load\n")
220
- f.write(" puts \"Hello, hello \#{DefaultController.name}!\"\n")
221
- f.write(" end\n")
222
- f.write("end\n")
223
- end
224
- puts "create".green + " File /app/views/main.rb"
225
-
226
- # Create /app/models
227
- Dir.mkdir(projectname + "/app/models")
228
- puts "create".green + " Directory /app/models"
229
- # Create /app/models/default.rb
230
- File.open(projectname + "/app/models/default.rb", "w") do |f|
231
- f.write("require './app/controllers/default_controller'\n")
232
- f.write("class DefaultModel\n")
233
- f.write("end\n")
234
- end
235
- puts "create".green + " File /app/models/default.rb"
236
-
237
- # Create /app/migrations
238
- Dir.mkdir(projectname + "/app/migrations")
239
- puts "create".green + " Directory /app/migrations"
240
- puts "create".green + " Directory /app/db"
241
- Dir.mkdir(projectName + "/app/db")
242
- puts "reminder".yellow + " You have to generate a new migration if you're using a database."
243
- end
244
- end
245
-
246
- # Migration: an object to assist database creation
247
- class Migration
248
- # Init (create or open database)
249
- attr_accessor :dbr
250
- def initialize(database)
251
- if File.exists?("./db/" + database + ".db")
252
- puts "database #{database} already exists. opening...".green
253
- @dbr = SQLite3::Database.open("./db/" + database + ".db")
254
- else
255
- puts "database #{database} not found. creating database...".yellow
256
- begin
257
- @dbr = SQLite3::Database.new("./db/" + database + ".db")
258
- puts "database #{database} successfully created.".green
259
- rescue
260
- puts "couldn't create database. aborting...".red
261
- exit
262
- end
263
- end
264
- end
265
-
266
- # create table
267
- def create_table(table)
268
- puts "attempting to create table #{table}..."
269
- begin
270
- tt = @dbr.prepare("CREATE TABLE #{table}(id int auto_increment);")
271
- ee = tt.execute
272
- puts "table created correctly.".green
273
- rescue
274
- puts "error while creating table #{table}".red
275
- exit
276
- end
277
- end
278
-
279
- # add column
280
- def add_column(table, cname, type)
281
- if type.to_s == "text"
282
- puts "attempting to create text column #{cname} at #{table}..."
283
- begin
284
- tt = @dbr.prepare("ALTER TABLE #{table} ADD #{cname} text;")
285
- ee = tt.execute
286
- puts "column correctly added!".green
287
- rescue
288
- puts "error while adding column #{cname} to #{table}".red
289
- exit
290
- end
291
- elsif type.to_s == "num"
292
- puts "attempting to create numerical column #{cname} at #{table}..."
293
- begin
294
- tt = @dbr.prepare("ALTER TABLE #{table} ADD #{cname} int;")
295
- ee = tt.execute
296
- puts "column correctly added!".green
297
- rescue
298
- puts "error while adding column #{cname} to #{table}".red
299
- exit
300
- end
301
- else
302
- puts "attempting to create custom (#{type.to_s}) column #{cname} at #{table}..."
303
- begin
304
- tt = @dbr.prepare("ALTER TABLE #{table} ADD #{cname} #{type.to_s};")
305
- ee = tt.execute
306
- puts "column correctly added!".green
307
- rescue
308
- puts "error while adding column #{cname} to #{table}".red
309
- exit
310
- end
311
- end
312
- end
313
-
314
- # Insert values into table WHERE columns = ["column1", "column2"]
315
- # AND values = ["value1", "value2"]
316
- def insert(table, columns, values)
317
- puts "attempting to insert values into #{table}..."
318
- begin
319
- nvalues = []
320
- values.each do |v|
321
- if v.is_a? String
322
- nvalues << "\"" + v + "\""
323
- else
324
- nvalues << v
325
- end
326
- end
327
- tt = @dbr.prepare("INSERT INTO #{table}(" + columns.join(", ") + ") VALUES(" + nvalues.join(", ") + ");")
328
- ee = tt.execute
329
- puts "values correctly added!".green
330
- rescue
331
- puts "error while inserting values".red
332
- end
333
- end
334
- end
335
-
336
- # Helpers
337
- class Helpers
338
- # Helper for creating a controller
339
- def self.createController(controllerName)
340
- controllerCap = controllerName.slice(0,1).capitalize + controllerName.slice(1..-1)
341
- "require './app/models/#{controllerName.downcase}'
342
- require './app/views/#{controllerName.downcase}'
343
- class #{controllerCap}Controller
344
- class << self
345
- #Add your variables here!
346
- attr_accessor :name
347
- end
348
- @name = \"James!\"
349
- def self.main
350
- #{controllerCap}View.load
351
- end
352
- end\n"
353
- end
354
- # Helper for creating a view
355
- def self.createView(viewName, controllerName)
356
- viewCap = viewName.slice(0,1).capitalize + viewName.slice(1..-1)
357
- "require './app/controllers/#{controllerName.downcase}_controller'
358
- class #{viewCap}View
359
- def self.load
360
- puts \"Hello, hello!\"
361
- end
362
- end\n"
363
- end
364
-
365
- # Helper for creating a model
366
- def self.createModel(modelName)
367
- "require './app/controllers/#{modelName.downcase}_controller'
368
- class #{modelName.slice(0,1).capitalize + modelName.slice(1..-1)}Model
369
- end\n"
370
- end
371
-
372
- # Helper for creating a migration
373
- def self.createMigration(migrationName, dbName)
374
- "require 'rmvc'
375
- m = RMVC::Migration.new(\"#{dbName}\")
376
- # Insert migration commands here\n"
377
- end
378
- end
379
- end
1
+ require 'rmvc/core'
2
+ require 'rmvc/helpers'
3
+ require 'rmvc/interface'
4
+ require 'rmvc/migrations'
data/lib/rmvc/core.rb ADDED
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/env ruby
2
+ require 'colorize'
3
+ require 'sqlite3'
4
+ module RMVC
5
+ # Core (commands)
6
+ class Core
7
+ def self.migrate(mName)
8
+ # Check if we're in a RMVC project
9
+ if (!File.directory? ".rmvc") then
10
+ puts "error".red + " Not a RMVC project! (missing directory .rmvc)"
11
+ exit
12
+ end
13
+
14
+ # Check if the migration exists
15
+ unless File.exists? "app/migrations/#{mName}.rb"
16
+ puts "error".red + " The migration #{mName} doesn't exist!"
17
+ exit
18
+ end
19
+
20
+ # Run the file
21
+ puts "attempting to migrate #{mName}...".yellow
22
+ sta = system ("ruby app/migrations/#{mName}.rb")
23
+ if sta
24
+ puts "#{mName} migrated correctly!".green
25
+ else
26
+ puts "error while migrating #{mName}".red
27
+ end
28
+ end
29
+
30
+ def self.generate(args)
31
+ # Check if we're in a RMVC project
32
+ if (!File.directory? ".rmvc") then
33
+ puts "error".red + " Not a RMVC project! (missing directory .rmvc)"
34
+ exit
35
+ end
36
+ if (args[1] == "model") then
37
+ puts "Generating model #{args[2]}..."
38
+ # Create file /app/models/args[2].rb
39
+ if (File.exists? "app/models/#{args[2].downcase}.rb") then
40
+ puts "error".red + " File /app/models/#{args[2].downcase}.rb already exists."
41
+ exit
42
+ end
43
+ File.open("app/models/#{args[2].downcase}.rb", "w") do |f|
44
+ f.write(RMVC::Helpers.createModel(args[2]))
45
+ end
46
+ puts "create".green + " File /app/models/#{args[2].downcase}.rb"
47
+ elsif (args[1] == "controller")
48
+ puts "Generating controller #{args[2]}..."
49
+ # Create file /app/controllers/args[2]_controller.rb
50
+ if (File.exists? "app/controllers/#{args[2].downcase}_controller.rb") then
51
+ puts "error".red + " File /app/controller/#{args[2].downcase}_controller.rb already exists."
52
+ exit
53
+ end
54
+ File.open("app/controllers/#{args[2].downcase}_controller.rb", "w") do |f|
55
+ f.write(RMVC::Helpers.createController(args[2]))
56
+ end
57
+ puts "create".green + " File /app/controllers/#{args[2].downcase}_controller.rb"
58
+ puts "notice".yellow + " Remember to add a require statement to the main file of the project, to include app/controllers/#{args[2].downcase}_controller."
59
+ elsif (args[1] == "view")
60
+ puts "Generating view #{args[2]}..."
61
+ # Create file /app/controllers/args[2]_controller.rb
62
+ if (File.exists? "app/views/#{args[2].downcase}.rb") then
63
+ puts "error".red + " File /app/views/#{args[2].downcase}.rb already exists."
64
+ exit
65
+ end
66
+ File.open("app/views/#{args[2].downcase}.rb", "w") do |f|
67
+ f.write(RMVC::Helpers.createView(args[2], args[3]))
68
+ end
69
+ puts "create".green + " File /app/views/#{args[2].downcase}.rb"
70
+ elsif (args[1] == "migration")
71
+ puts "Generating migration #{args[2]}..."
72
+ # Create file /app/migrations/args[2].rb
73
+ if (File.exists? "app/migrations/#{args[2].downcase}.rb") then
74
+ puts "error".red + " File /app/migrations/#{args[2].downcase}.rb already exists."
75
+ exit
76
+ end
77
+ File.open("app/migrations/#{args[2].downcase}.rb", "w") do |f|
78
+ f.write(RMVC::Helpers.createMigration(args[2], args[3]))
79
+ end
80
+ puts "create".green + " File /app/migrations/#{args[2].downcase}.rb"
81
+ else
82
+ puts "Wrong argument for generate: #{args[1]}"
83
+ RMVC::Interface.showHelp
84
+ end
85
+ end
86
+ # CMD: new [ProjectName]
87
+ def self.create(projectname)
88
+ puts "Creating new project: #{projectname}"
89
+ puts ""
90
+ # Check if directory exists
91
+ if (File.directory? projectname) then
92
+ puts "error".red + " Directory #{projectname} already exists."
93
+ exit
94
+ end
95
+ # Try to create it
96
+ if (!Dir.mkdir(projectname)) then
97
+ puts "error".red + " Error while creating the directory #{projectname}"
98
+ exit
99
+ else
100
+ puts "create".green + " Directory #{projectname}"
101
+ end
102
+ # Create directory .rmvc
103
+ Dir.mkdir(projectname + "/.rmvc")
104
+ puts "create".green + " Directory #{projectname}/.rmvc"
105
+ # Create file projectname.rb
106
+ File.open(projectname + "/" + projectname + ".rb", "w") do |f|
107
+ f.write("require './app/controllers/default_controller'\n")
108
+ f.write("DefaultController.main\n")
109
+ end
110
+ puts "create".green + " File #{projectname}.rb"
111
+
112
+ # Create directory app
113
+ Dir.mkdir(projectname + "/app")
114
+ puts "create".green + " Directory /app"
115
+ # Create directory app/controllers
116
+ Dir.mkdir(projectname + "/app/controllers")
117
+ puts "create".green + " Directory /app/controllers"
118
+ # Create default controller
119
+ File.open(projectname + "/app/controllers/default_controller.rb", "w") do |f|
120
+ f.write("require './app/models/default'\n")
121
+ f.write("require './app/views/main'\n")
122
+ f.write("class DefaultController\n")
123
+ f.write(" class << self\n")
124
+ f.write(" #Add your variables here!\n")
125
+ f.write(" attr_accessor :name\n")
126
+ f.write(" end\n")
127
+ f.write(" @name = \"James!\"\n")
128
+ f.write(" def self.main\n")
129
+ f.write(" MainView.load\n")
130
+ f.write(" end\n")
131
+ f.write("end\n")
132
+ end
133
+ puts "create".green + " File /app/controllers/default_controller.rb"
134
+ # Create directory app/views
135
+ Dir.mkdir(projectname + "/app/views")
136
+ puts "create".green + " Directory /app/views"
137
+ # Create /app/views/main.rb
138
+ File.open(projectname + "/app/views/main.rb", "w") do |f|
139
+ f.write("require './app/controllers/default_controller'\n")
140
+ f.write("class MainView\n")
141
+ f.write(" def self.load\n")
142
+ f.write(" puts \"Hello, hello \#{DefaultController.name}!\"\n")
143
+ f.write(" end\n")
144
+ f.write("end\n")
145
+ end
146
+ puts "create".green + " File /app/views/main.rb"
147
+
148
+ # Create /app/models
149
+ Dir.mkdir(projectname + "/app/models")
150
+ puts "create".green + " Directory /app/models"
151
+ # Create /app/models/default.rb
152
+ File.open(projectname + "/app/models/default.rb", "w") do |f|
153
+ f.write("require './app/controllers/default_controller'\n")
154
+ f.write("class DefaultModel\n")
155
+ f.write("end\n")
156
+ end
157
+ puts "create".green + " File /app/models/default.rb"
158
+
159
+ # Create /app/migrations
160
+ Dir.mkdir(projectname + "/app/migrations")
161
+ puts "create".green + " Directory /app/migrations"
162
+ puts "create".green + " Directory /db"
163
+ Dir.mkdir(projectname + "/db")
164
+ puts "reminder".yellow + " You have to generate a new migration if you're using a database."
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+ require 'colorize'
3
+ require 'sqlite3'
4
+ module RMVC
5
+ # Helpers
6
+ class Helpers
7
+ # Helper for creating a controller
8
+ def self.createController(controllerName)
9
+ controllerCap = controllerName.slice(0,1).capitalize + controllerName.slice(1..-1)
10
+ "require './app/models/#{controllerName.downcase}'
11
+ require './app/views/#{controllerName.downcase}'
12
+ class #{controllerCap}Controller
13
+ class << self
14
+ #Add your variables here!
15
+ attr_accessor :name
16
+ end
17
+ @name = \"James!\"
18
+ def self.main
19
+ #{controllerCap}View.load
20
+ end
21
+ end\n"
22
+ end
23
+ # Helper for creating a view
24
+ def self.createView(viewName, controllerName)
25
+ viewCap = viewName.slice(0,1).capitalize + viewName.slice(1..-1)
26
+ "require './app/controllers/#{controllerName.downcase}_controller'
27
+ class #{viewCap}View
28
+ def self.load
29
+ puts \"Hello, hello!\"
30
+ end
31
+ end\n"
32
+ end
33
+
34
+ # Helper for creating a model
35
+ def self.createModel(modelName)
36
+ "require './app/controllers/#{modelName.downcase}_controller'
37
+ class #{modelName.slice(0,1).capitalize + modelName.slice(1..-1)}Model
38
+ end\n"
39
+ end
40
+
41
+ # Helper for creating a migration
42
+ def self.createMigration(migrationName, dbName)
43
+ "require 'rmvc'
44
+ m = RMVC::Migration.new(\"#{dbName}\")
45
+ # Insert migration commands here\n"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+ require 'colorize'
3
+ require 'sqlite3'
4
+ module RMVC
5
+ # Interface
6
+ class Interface
7
+ # Startup
8
+ def self.startup(args)
9
+ if (args != "none") then
10
+ RMVC::Interface.run(args)
11
+ else
12
+ # No ARGS
13
+ RMVC::Interface.showHelp
14
+ end
15
+ end
16
+
17
+ # Run a command
18
+ def self.run(args)
19
+ fullArgs = args.join(" ")
20
+ # Validate first argument
21
+ if args[0] == "new" then
22
+ # Create a new project
23
+ if (args.length >= 2) then
24
+ RMVC::Core.create(args[1])
25
+ else
26
+ puts "Wrong syntax for 'new'!"
27
+ RMVC::Interface.showHelp
28
+ end
29
+ elsif args[0] == "migrate"
30
+ if args.length < 2
31
+ puts "Wrong syntax for 'migrate'!"
32
+ RMVC::Interface.showHelp
33
+ else
34
+ RMVC::Core.migrate(args[1])
35
+ end
36
+ elsif args[0] == "generate"
37
+ # Generate code
38
+ if (args.length < 3) then
39
+ puts "Wrong syntax for 'generate'!"
40
+ RMVC::Interface.showHelp
41
+ elsif (args[1] == "view" && args.length < 4)
42
+ puts "Wrong syntax for 'generate view'!"
43
+ RMVC::Interface.showHelp
44
+ elsif (args[1] == "migration" && args.length < 4)
45
+ puts "Wrong syntax for 'generate migration'!"
46
+ RMVC::Interface.showHelp
47
+ else
48
+ RMVC::Core.generate(args)
49
+ end
50
+ else
51
+ # Unknown command
52
+ RMVC::Interface.showHelp
53
+ end
54
+ end
55
+
56
+ # Show help
57
+ def self.showHelp
58
+ puts "You introduced an invalid command, or you've ran rmvc without arguments."
59
+ puts "Syntax: rmvc [command] [arguments ...]"
60
+ puts "The following commands are available: "
61
+ puts ""
62
+ puts " new [ProjectName]".yellow
63
+ puts " where [ProjectName] is the name of your new project".green
64
+ puts " Creates a new project using the RMVC architecture."
65
+ puts ""
66
+ puts " generate [model|controller|view|migration] [name] [controllerName|dbName]".yellow
67
+ puts " where [name] is the name of the model/controller/view".green
68
+ puts " Creates a new model/controller/view with the desired name."
69
+ puts " If a controller is created, it will automatically be linked (through require) to a model "
70
+ puts " and a view of the same name than the controller."
71
+ puts " If a model is created, it will be linked (through require) to a controller of the same name."
72
+ puts " If a view is created, you must specify [controllerName]. It will be linked (through require) to it."
73
+ puts " If a migration is created, a file in /app/migrations will be created. A database must be specified."
74
+ puts ""
75
+ puts " migrate [migration]".yellow
76
+ puts " where [migration] is the name of the migration previously created.".green
77
+ puts " Runs the queries written in a migration file to a database."
78
+ puts " If the database doesn't exist, it will be created."
79
+ puts ""
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+ require 'colorize'
3
+ require 'sqlite3'
4
+ module RMVC
5
+ # Migration: an object to assist database creation
6
+ class Migration
7
+ # Init (create or open database)
8
+ attr_accessor :dbr
9
+ def initialize(database)
10
+ if File.exists?("./db/" + database + ".db")
11
+ puts "database #{database} already exists. opening...".green
12
+ @dbr = SQLite3::Database.open("./db/" + database + ".db")
13
+ else
14
+ puts "database #{database} not found. creating database...".yellow
15
+ begin
16
+ @dbr = SQLite3::Database.new("./db/" + database + ".db")
17
+ puts "database #{database} successfully created.".green
18
+ rescue
19
+ puts "couldn't create database. aborting...".red
20
+ exit
21
+ end
22
+ end
23
+ end
24
+
25
+ # create table
26
+ def create_table(table)
27
+ puts "attempting to create table #{table}..."
28
+ begin
29
+ tt = @dbr.prepare("CREATE TABLE #{table}(id int auto_increment);")
30
+ ee = tt.execute
31
+ puts "table created correctly.".green
32
+ rescue
33
+ puts "error while creating table #{table}".red
34
+ exit
35
+ end
36
+ end
37
+
38
+ # add column
39
+ def add_column(table, cname, type)
40
+ if type.to_s == "text"
41
+ puts "attempting to create text column #{cname} at #{table}..."
42
+ begin
43
+ tt = @dbr.prepare("ALTER TABLE #{table} ADD #{cname} text;")
44
+ ee = tt.execute
45
+ puts "column correctly added!".green
46
+ rescue
47
+ puts "error while adding column #{cname} to #{table}".red
48
+ exit
49
+ end
50
+ elsif type.to_s == "num"
51
+ puts "attempting to create numerical column #{cname} at #{table}..."
52
+ begin
53
+ tt = @dbr.prepare("ALTER TABLE #{table} ADD #{cname} int;")
54
+ ee = tt.execute
55
+ puts "column correctly added!".green
56
+ rescue
57
+ puts "error while adding column #{cname} to #{table}".red
58
+ exit
59
+ end
60
+ else
61
+ puts "attempting to create custom (#{type.to_s}) column #{cname} at #{table}..."
62
+ begin
63
+ tt = @dbr.prepare("ALTER TABLE #{table} ADD #{cname} #{type.to_s};")
64
+ ee = tt.execute
65
+ puts "column correctly added!".green
66
+ rescue
67
+ puts "error while adding column #{cname} to #{table}".red
68
+ exit
69
+ end
70
+ end
71
+ end
72
+
73
+ # Insert values into table WHERE columns = ["column1", "column2"]
74
+ # AND values = ["value1", "value2"]
75
+ def insert(table, columns, values)
76
+ puts "attempting to insert values into #{table}..."
77
+ begin
78
+ nvalues = []
79
+ values.each do |v|
80
+ if v.is_a? String
81
+ nvalues << "\"" + v + "\""
82
+ else
83
+ nvalues << v
84
+ end
85
+ end
86
+ tt = @dbr.prepare("INSERT INTO #{table}(" + columns.join(", ") + ") VALUES(" + nvalues.join(", ") + ");")
87
+ ee = tt.execute
88
+ puts "values correctly added!".green
89
+ rescue
90
+ puts "error while inserting values".red
91
+ end
92
+ end
93
+ end
94
+ end
metadata CHANGED
@@ -1,45 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmvc
3
3
  version: !ruby/object:Gem::Version
4
- version: '3.2'
4
+ version: 3.2.1
5
5
  platform: ruby
6
6
  authors:
7
- - Catbuntu
7
+ - unrar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-20 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Use MVC in your Ruby apps!
42
- email: catbuntu@catbuntu.me
42
+ email: joszaynka@gmail.com
43
43
  executables:
44
44
  - rmvc
45
45
  extensions: []
@@ -47,6 +47,10 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - bin/rmvc
49
49
  - lib/rmvc.rb
50
+ - lib/rmvc/core.rb
51
+ - lib/rmvc/helpers.rb
52
+ - lib/rmvc/interface.rb
53
+ - lib/rmvc/migrations.rb
50
54
  homepage: http://unrar.github.io/rmvc
51
55
  licenses:
52
56
  - MIT