rmvc 3.0.1 → 3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rmvc.rb +159 -4
  3. metadata +22 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d2118910a18dd14b21ae51754610cfdc651a6bb
4
- data.tar.gz: a7ace3176411ba4f8d6bd7a8fae094bc89246ee9
3
+ metadata.gz: 393e77134ec51cd78ed409bed303004982a5558d
4
+ data.tar.gz: bf14aa7970a289bc9a57e077ea6372ba6a333c1c
5
5
  SHA512:
6
- metadata.gz: d7808633b2ba7194f4955d487e5ede3366a9b10c7ebca232228aef3a6b8b42ccab9b4918a6b86cbd2647f551d221ffb30413510b373351a20ab1db6aa396420e
7
- data.tar.gz: f76546e066fbd760fbbee382e6b90197eedea144aee5f5202ea4ad051f2612a7947c2f955be5bc594fd49f62a80228a1d3736450248cb77f423b386f0338c531
6
+ metadata.gz: f5087966162ca40aee0af65eb8a2ef3fa095a4c5519f2b4419671c8dc9b9bf89c279e6b9eda0ee41bd9520eac2bc6209664d63c00e341b1a81ef8c442ff3598c
7
+ data.tar.gz: cf52ce88b51e915be3caf4ffe5b87c3cb7e24cc9c4491f574492a572a570760c7f82793cf321f6bed67fe959faf9466b34d754fb1955eb0479fac04a1b96810c
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'colorize'
3
+ require 'sqlite3'
3
4
  module RMVC
4
5
  # Interface
5
6
  class Interface
@@ -25,6 +26,13 @@ module RMVC
25
26
  puts "Wrong syntax for 'new'!"
26
27
  RMVC::Interface.showHelp
27
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
28
36
  elsif args[0] == "generate"
29
37
  # Generate code
30
38
  if (args.length < 3) then
@@ -33,6 +41,9 @@ module RMVC
33
41
  elsif (args[1] == "view" && args.length < 4)
34
42
  puts "Wrong syntax for 'generate view'!"
35
43
  RMVC::Interface.showHelp
44
+ elsif (args[1] == "migration" && args.length < 4)
45
+ puts "Wrong syntax for 'generate migration'!"
46
+ RMVC::Interface.showHelp
36
47
  else
37
48
  RMVC::Core.generate(args)
38
49
  end
@@ -48,23 +59,52 @@ module RMVC
48
59
  puts "Syntax: rmvc [command] [arguments ...]"
49
60
  puts "The following commands are available: "
50
61
  puts ""
51
- puts " new [ProjectName]"
52
- puts " where [ProjectName] is the name of your new project"
62
+ puts " new [ProjectName]".yellow
63
+ puts " where [ProjectName] is the name of your new project".green
53
64
  puts " Creates a new project using the RMVC architecture."
54
65
  puts ""
55
- puts " generate [model|controller|view] [name] [controllerName]"
56
- puts " where [name] is the name of the model/controller/view"
66
+ puts " generate [model|controller|view|migration] [name] [controllerName|dbName]".yellow
67
+ puts " where [name] is the name of the model/controller/view".green
57
68
  puts " Creates a new model/controller/view with the desired name."
58
69
  puts " If a controller is created, it will automatically be linked (through require) to a model "
59
70
  puts " and a view of the same name than the controller."
60
71
  puts " If a model is created, it will be linked (through require) to a controller of the same name."
61
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."
62
79
  puts ""
63
80
  end
64
81
  end
65
82
 
66
83
  # Core (commands)
67
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
+
68
108
  def self.generate(args)
69
109
  # Check if we're in a RMVC project
70
110
  if (!File.directory? ".rmvc") then
@@ -105,6 +145,17 @@ module RMVC
105
145
  f.write(RMVC::Helpers.createView(args[2], args[3]))
106
146
  end
107
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"
108
159
  else
109
160
  puts "Wrong argument for generate: #{args[1]}"
110
161
  RMVC::Interface.showHelp
@@ -182,6 +233,103 @@ module RMVC
182
233
  f.write("end\n")
183
234
  end
184
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
185
333
  end
186
334
  end
187
335
 
@@ -220,5 +368,12 @@ end\n"
220
368
  class #{modelName.slice(0,1).capitalize + modelName.slice(1..-1)}Model
221
369
  end\n"
222
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"
223
377
  end
224
378
  end
379
+ end
metadata CHANGED
@@ -1,27 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmvc
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: '3.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Catbuntu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-01 00:00:00.000000000 Z
11
+ date: 2014-09-20 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
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  description: Use MVC in your Ruby apps!
@@ -31,8 +45,8 @@ executables:
31
45
  extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
34
- - lib/rmvc.rb
35
48
  - bin/rmvc
49
+ - lib/rmvc.rb
36
50
  homepage: http://unrar.github.io/rmvc
37
51
  licenses:
38
52
  - MIT
@@ -43,17 +57,17 @@ require_paths:
43
57
  - lib
44
58
  required_ruby_version: !ruby/object:Gem::Requirement
45
59
  requirements:
46
- - - '>='
60
+ - - ">="
47
61
  - !ruby/object:Gem::Version
48
62
  version: '0'
49
63
  required_rubygems_version: !ruby/object:Gem::Requirement
50
64
  requirements:
51
- - - '>='
65
+ - - ">="
52
66
  - !ruby/object:Gem::Version
53
67
  version: '0'
54
68
  requirements: []
55
69
  rubyforge_project:
56
- rubygems_version: 2.0.6
70
+ rubygems_version: 2.2.2
57
71
  signing_key:
58
72
  specification_version: 4
59
73
  summary: Ruby MVC