net_mate 1.0.0
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 +7 -0
- data/bin/net_mate +41 -0
- data/lib/net_mate.rb +6 -0
- data/lib/net_mate/connection.rb +25 -0
- data/lib/net_mate/controller.rb +35 -0
- data/lib/net_mate/controller_generator.rb +87 -0
- data/lib/net_mate/create_application.rb +142 -0
- data/lib/net_mate/database_generator.rb +78 -0
- data/lib/net_mate/dispatcher.rb +21 -0
- data/lib/net_mate/model.rb +109 -0
- data/lib/net_mate/model_generator.rb +100 -0
- data/lib/net_mate/request.rb +25 -0
- data/lib/net_mate/response.rb +25 -0
- data/lib/net_mate/routing.rb +30 -0
- data/lib/net_mate/usage +77 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 87f072157118dbbb4466859513ac4e660d40cc00
|
4
|
+
data.tar.gz: 661772be6dbda528db090e5243cb28d4325e80f6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6e6c377d1e9974b98aa203d86670119e39ca992301c97129ecbf7c831e3a1efcb07fc5f2f65488199e1647dd0899dd39f65d0fcee819ac826b2877b26f5b2717
|
7
|
+
data.tar.gz: 6e547c0223449c46015f61a548eb35dc742405da39054d89b338e62ffbd6e215c3f5c829fadaf04a4e9e946c21d5ae5705cabcbf4873fdc9c7542dfaab03c343
|
data/bin/net_mate
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'fileutils'
|
3
|
+
require File.expand_path('../../lib/net_mate/create_application', __FILE__)
|
4
|
+
require File.expand_path('../../lib/net_mate/controller_generator', __FILE__)
|
5
|
+
require File.expand_path('../../lib/net_mate/model_generator', __FILE__)
|
6
|
+
require File.expand_path('../../lib/net_mate/database_generator', __FILE__)
|
7
|
+
|
8
|
+
abort 'Please supply an argument' if ARGV.empty?
|
9
|
+
|
10
|
+
def show_usage
|
11
|
+
usage = IO.read(File.expand_path('../../lib/net_mate/usage', __FILE__))
|
12
|
+
puts usage
|
13
|
+
end
|
14
|
+
|
15
|
+
if ARGV[0].downcase == 'new'
|
16
|
+
NetMate::Generators::CreateApplication.new(ARGV[1]).create_app
|
17
|
+
elsif ARGV[0].downcase == 'generate' || ARGV[0].downcase == 'g'
|
18
|
+
if ARGV[1].downcase == 'controller'
|
19
|
+
unless ARGV[2]
|
20
|
+
show_usage
|
21
|
+
else
|
22
|
+
NetMate::Generators::ControllerGenerator.new(ARGV[2], ARGV[3..(ARGV.size)]).generate_controller
|
23
|
+
end
|
24
|
+
elsif ARGV[1].downcase == 'model'
|
25
|
+
unless ARGV[2]
|
26
|
+
show_usage
|
27
|
+
else
|
28
|
+
NetMate::Generators::ModelGenerator.new(ARGV[2], ARGV[3..(ARGV.size)]).generate_model
|
29
|
+
end
|
30
|
+
elsif ARGV[1].downcase == 'database'
|
31
|
+
NetMate::Generators::DatabaseGenerator.new.check_and_create_database
|
32
|
+
else
|
33
|
+
show_usage
|
34
|
+
end
|
35
|
+
elsif ARGV[0].downcase == 'start' || ARGV[0].downcase == 's'
|
36
|
+
port = ARGV[1] ? ARGV[1] : 9000
|
37
|
+
`rackup -p #{port}`
|
38
|
+
else
|
39
|
+
show_usage
|
40
|
+
end
|
41
|
+
|
data/lib/net_mate.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'mysql2'
|
3
|
+
|
4
|
+
module NetMate
|
5
|
+
|
6
|
+
class Connection
|
7
|
+
|
8
|
+
attr_reader :db_config
|
9
|
+
def initialize path = nil
|
10
|
+
path ||= "#{ROOT_PATH}/config/database.yml"
|
11
|
+
@db_config = YAML.load_file(path)['development']
|
12
|
+
rescue
|
13
|
+
abort 'Please check if you are in the proper Application directory!'
|
14
|
+
end
|
15
|
+
|
16
|
+
def connect
|
17
|
+
Mysql2::Client.new @db_config
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.disconnect dbh
|
21
|
+
dbh.close if dbh
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module NetMate
|
2
|
+
class Controller
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@params = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def render erb_name
|
9
|
+
erb_name = erb_name.dup
|
10
|
+
erb_name << '.html.erb'
|
11
|
+
unless erb_name.start_with? '/'
|
12
|
+
class_name = self.class.name
|
13
|
+
erb_dir = "#{(class_name[0..(class_name.rindex('Controller').pred)]).downcase}"
|
14
|
+
erb_path = "#{ROOT_PATH}/app/views/#{erb_dir}/#{erb_name}"
|
15
|
+
else
|
16
|
+
erb_path = erb_name
|
17
|
+
end
|
18
|
+
body = Erubis::Eruby.new(IO.read(erb_path)).result(binding())
|
19
|
+
Response.new(body).respond
|
20
|
+
end
|
21
|
+
=begin
|
22
|
+
def redirect_to action
|
23
|
+
action = action.dup
|
24
|
+
if action.include? '/'
|
25
|
+
controller, action = action.split('/')
|
26
|
+
controller = Object.const_get("#{controller.capitalize}Controller").new
|
27
|
+
else
|
28
|
+
controller = self.class.new
|
29
|
+
end
|
30
|
+
controller.instance_variable_set(:@params, @params)
|
31
|
+
controller.send(action.to_sym)
|
32
|
+
end
|
33
|
+
=end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
module NetMate
|
5
|
+
module Generators
|
6
|
+
class ControllerGenerator
|
7
|
+
|
8
|
+
def initialize controller, actions
|
9
|
+
@controller = controller.dup
|
10
|
+
@actions = actions
|
11
|
+
end
|
12
|
+
|
13
|
+
def generate_controller
|
14
|
+
cur_dir = Dir.pwd
|
15
|
+
flag = false
|
16
|
+
while cur_dir != '/' do
|
17
|
+
if Dir.exist? 'app'
|
18
|
+
FileUtils.cd 'app'
|
19
|
+
if Dir.exist? 'controllers'
|
20
|
+
FileUtils.cd 'controllers'
|
21
|
+
controller_rb = "#{ActiveSupport::Inflector.tableize(@controller)}_controller.rb"
|
22
|
+
File.open(controller_rb, 'w') do |file|
|
23
|
+
puts "Created app/controllers/#{controller_rb}"
|
24
|
+
@controller = ActiveSupport::Inflector.pluralize(@controller)
|
25
|
+
file.puts "class #{ActiveSupport::Inflector.camelize(@controller)}Controller < NetMate::Controller\n\n"
|
26
|
+
@actions.each do |action|
|
27
|
+
file.puts " def #{action}\n end\n\n"
|
28
|
+
end
|
29
|
+
file.puts 'end'
|
30
|
+
end
|
31
|
+
flag = true
|
32
|
+
break
|
33
|
+
else
|
34
|
+
abort 'ERROR: app/controllers folder does not exist'
|
35
|
+
end
|
36
|
+
else
|
37
|
+
cur_dir = File.expand_path '..', Dir.pwd
|
38
|
+
FileUtils.cd cur_dir
|
39
|
+
end
|
40
|
+
end
|
41
|
+
abort 'ERROR: please move to the application folder' unless flag
|
42
|
+
unless @actions.empty?
|
43
|
+
create_views
|
44
|
+
create_routes
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def create_views
|
51
|
+
FileUtils.cd File.expand_path '..', Dir.pwd
|
52
|
+
if Dir.exist? 'views'
|
53
|
+
FileUtils.cd 'views'
|
54
|
+
@controller = ActiveSupport::Inflector.tableize(@controller)
|
55
|
+
FileUtils.mkpath @controller
|
56
|
+
FileUtils.cd @controller
|
57
|
+
@actions.each do |action|
|
58
|
+
action_html_erb = action.dup << '.html.erb'
|
59
|
+
IO.write action_html_erb, "This is #{action_html_erb} file."
|
60
|
+
puts "Created app/views/#{@controller}/#{action_html_erb}"
|
61
|
+
end
|
62
|
+
else
|
63
|
+
abort 'ERROR app/views folder does not exist '
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
#move to config/routes.rb and make entries in routes.rb for generated actions
|
68
|
+
def create_routes
|
69
|
+
FileUtils.cd File.expand_path '../../..', Dir.pwd
|
70
|
+
FileUtils.cd 'config'
|
71
|
+
temp = Tempfile.new 'temp_routes'
|
72
|
+
File.open('routes.rb', 'r').each_line do |line|
|
73
|
+
temp.puts line
|
74
|
+
if line =~ /def\s+create_routes/
|
75
|
+
@actions.each do |action|
|
76
|
+
route_entry = "#{ActiveSupport::Inflector.tableize(@controller)}/#{action}"
|
77
|
+
temp.puts "#{' ' * 4}get '#{route_entry}'"
|
78
|
+
puts "Created route #{route_entry}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
temp.close
|
83
|
+
FileUtils.mv temp.path, 'routes.rb'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
##
|
2
|
+
## DO NOT AUTO INDENT
|
3
|
+
##
|
4
|
+
|
5
|
+
module NetMate
|
6
|
+
module Generators
|
7
|
+
|
8
|
+
class CreateApplication
|
9
|
+
|
10
|
+
def initialize path
|
11
|
+
if path.include? '/'
|
12
|
+
@app_path = File.expand_path path[0..(path.rindex('/'))]
|
13
|
+
FileUtils.mkpath @app_path
|
14
|
+
else
|
15
|
+
@app_path = Dir.pwd
|
16
|
+
end
|
17
|
+
@app_name = path.split('/').last
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_app
|
21
|
+
create_skeleton
|
22
|
+
write_config_files
|
23
|
+
write_html_pages
|
24
|
+
display_output
|
25
|
+
puts 'Installing required gems'
|
26
|
+
command = "cd #{@app_path}/#{@app_name} && bundle install"
|
27
|
+
exec command
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def write_html_pages
|
32
|
+
output = ["<!DOCTYPE html>",
|
33
|
+
"<html>",
|
34
|
+
"<head><title>Error</title></head>",
|
35
|
+
"<body>The address you entered does not exist</body>",
|
36
|
+
"</html>"].join("\n")
|
37
|
+
IO.write('public/404.html', output)
|
38
|
+
end
|
39
|
+
|
40
|
+
def display_output
|
41
|
+
puts "Full App path: #{@app_path}/#{@app_name}"
|
42
|
+
puts "App name: #{@app_name}"
|
43
|
+
puts "Created the following files:"
|
44
|
+
Dir['**/*'].each { |file| puts " #{file}"}
|
45
|
+
end
|
46
|
+
|
47
|
+
def write_config_files
|
48
|
+
write_database_config
|
49
|
+
write_routes_file
|
50
|
+
write_gem_file
|
51
|
+
write_rackup_file
|
52
|
+
write_application_file
|
53
|
+
end
|
54
|
+
|
55
|
+
def write_application_file
|
56
|
+
output = ["require 'net_mate'",
|
57
|
+
"require File.expand_path('../routes', __FILE__)",
|
58
|
+
"",
|
59
|
+
'Dir["#{File.expand_path(\'../../app\', __FILE__ )}/{controllers,models}/*.rb"].each do |file|',
|
60
|
+
" require file",
|
61
|
+
"end",
|
62
|
+
"",
|
63
|
+
"ROOT_PATH = File.expand_path('../../', __FILE__)",
|
64
|
+
"",
|
65
|
+
"class Application < NetMate::Request",
|
66
|
+
"end"].join("\n")
|
67
|
+
IO.write('config/application.rb', output)
|
68
|
+
end
|
69
|
+
|
70
|
+
def write_rackup_file
|
71
|
+
output = ["# This file is used by Rack-based servers to start the application.\n",
|
72
|
+
"require File.expand_path('../config/application', __FILE__)",
|
73
|
+
"run Application.new"].join("\n")
|
74
|
+
IO.write('config.ru', output)
|
75
|
+
end
|
76
|
+
|
77
|
+
def write_database_config
|
78
|
+
output = ["# MySQL. Versions 4.1 and 5.0 are recommended.",
|
79
|
+
"#",
|
80
|
+
"# Install the MYSQL driver",
|
81
|
+
"# gem install mysql2",
|
82
|
+
"#",
|
83
|
+
"# Ensure the MySQL gem is defined in your Gemfile",
|
84
|
+
"# gem 'mysql2'\n",
|
85
|
+
"development:",
|
86
|
+
" adapter: mysql2",
|
87
|
+
" encoding: utf8",
|
88
|
+
" database: #{@app_name}",
|
89
|
+
" pool: 5",
|
90
|
+
" username: root",
|
91
|
+
" password:",
|
92
|
+
" url: localhost"].join("\n")
|
93
|
+
IO.write('config/database.yml', output)
|
94
|
+
end
|
95
|
+
|
96
|
+
def write_routes_file
|
97
|
+
output = ["class Routes < NetMate::Routing",
|
98
|
+
" def create_routes",
|
99
|
+
" #examples:",
|
100
|
+
" #get 'students/show'",
|
101
|
+
" #post 'students/create",
|
102
|
+
" #match 'signup', to: 'users#signup', via: 'get'",
|
103
|
+
" #resources :sessions, only: [:new, :create]",
|
104
|
+
" end",
|
105
|
+
"end"].join("\n")
|
106
|
+
IO.write('config/routes.rb', output)
|
107
|
+
end
|
108
|
+
|
109
|
+
def write_gem_file
|
110
|
+
output = ["source 'https://rubygems.org'\n",
|
111
|
+
"gem 'rack'",
|
112
|
+
"gem 'net_mate'",
|
113
|
+
"gem 'mysql2'"].join("\n")
|
114
|
+
IO.write('Gemfile', output)
|
115
|
+
end
|
116
|
+
|
117
|
+
def create_skeleton
|
118
|
+
app_full_path = File.join(@app_path, @app_name)
|
119
|
+
|
120
|
+
FileUtils.mkpath app_full_path
|
121
|
+
FileUtils.cd app_full_path
|
122
|
+
FileUtils.mkdir %w( app bin config public )
|
123
|
+
|
124
|
+
FileUtils.cd 'app'
|
125
|
+
FileUtils.mkdir %w( assets controllers models views )
|
126
|
+
FileUtils.cd 'assets'
|
127
|
+
FileUtils.mkdir %w( images javascripts stylesheets )
|
128
|
+
FileUtils.cd app_full_path
|
129
|
+
|
130
|
+
FileUtils.cd 'config'
|
131
|
+
%w( database.yml routes.rb application.rb ).each do |file|
|
132
|
+
FileUtils.touch file
|
133
|
+
end
|
134
|
+
FileUtils.cd app_full_path
|
135
|
+
|
136
|
+
%w( Gemfile README.md config.ru public/404.html ).each do |file|
|
137
|
+
FileUtils.touch file
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.expand_path('../connection', __FILE__)
|
2
|
+
|
3
|
+
module NetMate
|
4
|
+
module Generators
|
5
|
+
class DatabaseGenerator
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
iterate
|
9
|
+
@db_config = Connection.new("#{Dir.pwd}/database.yml").db_config
|
10
|
+
end
|
11
|
+
|
12
|
+
def in_cur_dir? dir
|
13
|
+
in_cur_dir = []
|
14
|
+
Dir.entries('.').each do |entry|
|
15
|
+
if File.directory? File.join('.', entry) and !(entry =='.' || entry == '..')
|
16
|
+
in_cur_dir << entry
|
17
|
+
end
|
18
|
+
end
|
19
|
+
in_cur_dir.include? dir
|
20
|
+
end
|
21
|
+
|
22
|
+
def check_and_create_database
|
23
|
+
dbh = Connection.new("#{Dir.pwd}/database.yml").connect
|
24
|
+
# connect to the MySQL server
|
25
|
+
# Create Table in database
|
26
|
+
if dbh.query "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '#{@db_config["database"]}'"
|
27
|
+
puts "USING EXISTING DATABASE #{@db_config['database']}"
|
28
|
+
end
|
29
|
+
rescue
|
30
|
+
puts "Creating new Database #{@db_config['database']}"
|
31
|
+
create_database()
|
32
|
+
ensure
|
33
|
+
# disconnect from server
|
34
|
+
Connection.disconnect dbh
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def iterate
|
39
|
+
cur_dir = Dir.pwd
|
40
|
+
while cur_dir != '/' do
|
41
|
+
if in_cur_dir? 'config'
|
42
|
+
FileUtils.cd 'config'
|
43
|
+
if !File.exist? 'database.yml'
|
44
|
+
abort "ERROR: app/config folder does not exist"
|
45
|
+
else
|
46
|
+
return
|
47
|
+
end
|
48
|
+
else
|
49
|
+
cur_dir = File.expand_path '..' , Dir.pwd
|
50
|
+
FileUtils.cd cur_dir
|
51
|
+
end
|
52
|
+
end
|
53
|
+
abort 'Could not find database.yml'
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def create_database()
|
58
|
+
dbh = Mysql2::Client.new(database: 'INFORMATION_SCHEMA',
|
59
|
+
username: @db_config['username'],
|
60
|
+
password: @db_config['password'],
|
61
|
+
host: @db_config['url'])
|
62
|
+
dbh.query "CREATE DATABASE #{@db_config['database']}"
|
63
|
+
puts "Database #{@db_config['database']} is created successfully."
|
64
|
+
Connection.disconnect dbh
|
65
|
+
|
66
|
+
#-------------------modified-------------#
|
67
|
+
rescue
|
68
|
+
puts "\n######################################################################"
|
69
|
+
puts "# There was an error while connecting to the Mysql. #"
|
70
|
+
puts "# Please check if you have configured the database.yml #"
|
71
|
+
puts "# File can be found in config/ of your application directory. #"
|
72
|
+
puts "# Make sure you setup proper Username and Password for database. #"
|
73
|
+
puts "######################################################################\n\n"
|
74
|
+
#----------------------------------------#
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module NetMate
|
2
|
+
class Dispatcher
|
3
|
+
|
4
|
+
def initialize(url, method, params)
|
5
|
+
@url, @method, @params = url, method.downcase, params
|
6
|
+
end
|
7
|
+
|
8
|
+
def dispatch
|
9
|
+
controller, action = NetMate::routes[[@url, @method]]
|
10
|
+
if controller
|
11
|
+
controller = Object.const_get("#{controller.capitalize}Controller").new
|
12
|
+
controller.instance_variable_set(:@params, @params)
|
13
|
+
controller.send(action)
|
14
|
+
#controller.send(:render, action.to_s)
|
15
|
+
else
|
16
|
+
Response.new(IO.read("#{ROOT_PATH}/public/404.html")).error
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'mysql2'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
module NetMate
|
5
|
+
class Model
|
6
|
+
|
7
|
+
@@columns = []
|
8
|
+
|
9
|
+
def make_accessors
|
10
|
+
self.class.class_eval do
|
11
|
+
@@columns.each do |col|
|
12
|
+
if col != 'id'
|
13
|
+
attr_accessor col.to_sym
|
14
|
+
else
|
15
|
+
attr_reader :id
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def update
|
22
|
+
dbh = Connection.new.connect
|
23
|
+
cols = @@columns.reject { |e| e == 'id' }
|
24
|
+
attributes = cols.each_with_index.map { |c, i| "#{c} = '#{self.send(c.to_sym)}'" }.join(', ')
|
25
|
+
dbh.query("UPDATE #{find_name} SET #{attributes} WHERE id = #{@id} ")
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_columns
|
29
|
+
dbh = Connection.new.connect
|
30
|
+
dbh.query("desc #{find_name}").each do |row|
|
31
|
+
@@columns << row.values[0]
|
32
|
+
end
|
33
|
+
Connection.disconnect dbh
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize args = {}
|
37
|
+
@@columns = []
|
38
|
+
get_columns
|
39
|
+
make_accessors
|
40
|
+
args.each { |k ,v| instance_variable_set("@#{k}".to_sym, v) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def save
|
44
|
+
table = find_name
|
45
|
+
dbh = Connection.new.connect
|
46
|
+
values = []
|
47
|
+
cols = @@columns.reject { |e| e == 'id' }
|
48
|
+
cols.each do |col|
|
49
|
+
values << self.send(col.to_sym)
|
50
|
+
end
|
51
|
+
values = values.map { |v| "'#{v}'" }.join(', ')
|
52
|
+
dbh.query "INSERT INTO #{table}(#{cols.join(',')}) VALUES (#{values})"
|
53
|
+
rescue
|
54
|
+
puts "An error occurred while saving into the database."
|
55
|
+
else
|
56
|
+
puts "New record was saved/added successfully."
|
57
|
+
ensure
|
58
|
+
Connection.disconnect dbh
|
59
|
+
end
|
60
|
+
|
61
|
+
def find_name
|
62
|
+
ActiveSupport::Inflector.tableize(self.class.name)
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.all
|
66
|
+
return find_by_sql("SELECT * FROM #{ActiveSupport::Inflector.tableize(self.name)}")
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.find(id)
|
70
|
+
id = id.to_i
|
71
|
+
dbh = Connection.new.connect
|
72
|
+
res = self.new
|
73
|
+
dbh.query("SELECT * FROM #{ActiveSupport::Inflector.tableize(self.name)} WHERE ID = #{id}").each do |row|
|
74
|
+
row.each { |k ,v| res.instance_variable_set("@#{k}".to_sym, v) }
|
75
|
+
end
|
76
|
+
rescue
|
77
|
+
ensure
|
78
|
+
Connection.disconnect dbh
|
79
|
+
return res
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.find_by_sql(sql_query)
|
83
|
+
dbh = Connection.new.connect
|
84
|
+
res = []
|
85
|
+
dbh.query(sql_query).each_with_index do |row, i|
|
86
|
+
res[i] = self.new
|
87
|
+
row.each { |k, v| res[i].instance_variable_set("@#{k}".to_sym, v) }
|
88
|
+
end
|
89
|
+
rescue
|
90
|
+
puts "An error occurred"
|
91
|
+
ensure
|
92
|
+
Connection.disconnect dbh
|
93
|
+
return res
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.find_by(col_name, value)
|
97
|
+
return find_by_sql("SELECT * FROM #{ActiveSupport::Inflector.tableize(self.name)} WHERE #{col_name} = '#{value}'")
|
98
|
+
end
|
99
|
+
|
100
|
+
def destroy
|
101
|
+
dbh = Connection.new.connect
|
102
|
+
dbh.query "DELETE FROM #{find_name} WHERE id = #{instance_variable_get(:@id)}"
|
103
|
+
rescue
|
104
|
+
puts "An error occurred"
|
105
|
+
ensure
|
106
|
+
Connection.disconnect dbh
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
module NetMate
|
5
|
+
module Generators
|
6
|
+
class ModelGenerator
|
7
|
+
|
8
|
+
def initialize model, columns
|
9
|
+
@model = model.dup
|
10
|
+
@columns = columns
|
11
|
+
end
|
12
|
+
|
13
|
+
def generate_model
|
14
|
+
flag = false
|
15
|
+
cur_dir = Dir.pwd
|
16
|
+
while cur_dir != "/" do
|
17
|
+
if Dir.exist? 'app'
|
18
|
+
FileUtils.cd 'app'
|
19
|
+
if Dir.exist? 'models'
|
20
|
+
call_create_table
|
21
|
+
FileUtils.cd 'models'
|
22
|
+
model_rb = ActiveSupport::Inflector.underscore "#{@model}.rb"
|
23
|
+
output = "class #{ActiveSupport::Inflector.camelize(@model)} < NetMate::Model\nend"
|
24
|
+
IO.write model_rb, output
|
25
|
+
puts "Created app/models/#{model_rb}"
|
26
|
+
flag = true
|
27
|
+
break
|
28
|
+
else
|
29
|
+
abort 'ERROR: app/models folder does not exist'
|
30
|
+
end
|
31
|
+
else
|
32
|
+
cur_dir = File.expand_path '..', Dir.pwd
|
33
|
+
FileUtils.cd cur_dir
|
34
|
+
end
|
35
|
+
end
|
36
|
+
abort "ERROR: please move to the application folder" unless flag
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def create_table table_data
|
42
|
+
attributes = table_data[:columns].map { |k, v| "#{k.to_s} #{convert(v)}" }.join(', ')
|
43
|
+
table_attributes = "id INT AUTO_INCREMENT ," << attributes << ", PRIMARY KEY (id)"
|
44
|
+
dbh = Connection.new("#{Dir.pwd}/../config/database.yml").connect
|
45
|
+
# Create Table in database
|
46
|
+
dbh.query "CREATE TABLE #{table_data[:table_name]}(#{table_attributes})"
|
47
|
+
rescue Exception => e
|
48
|
+
puts e
|
49
|
+
puts e.backtrace.join("\n")
|
50
|
+
else
|
51
|
+
puts "Table #{table_data[:table_name]} was created successfully."
|
52
|
+
ensure
|
53
|
+
# disconnect from server
|
54
|
+
Connection.disconnect dbh
|
55
|
+
end
|
56
|
+
|
57
|
+
def convert val
|
58
|
+
case val
|
59
|
+
when 'string'
|
60
|
+
'VARCHAR(80)'
|
61
|
+
when 'integer'
|
62
|
+
'INT'
|
63
|
+
when 'date'
|
64
|
+
'DATE'
|
65
|
+
when 'float'
|
66
|
+
'DECIMAL(20,10)'
|
67
|
+
when 'datetime'
|
68
|
+
'DATETIME'
|
69
|
+
when 'text'
|
70
|
+
'TEXT'
|
71
|
+
else 'boolean'
|
72
|
+
'BOOLEAN'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def supported_datatype? given_type
|
77
|
+
supported_datatypes = ['boolean', 'string', 'integer', 'date',
|
78
|
+
'datetime', 'text', 'float']
|
79
|
+
if !supported_datatypes.include? given_type
|
80
|
+
abort "Unsupported datatype: #{given_type}\nSupported_datatypes are:\n#{supported_datatypes}"
|
81
|
+
end
|
82
|
+
true
|
83
|
+
end
|
84
|
+
|
85
|
+
def call_create_table
|
86
|
+
table_data = {}
|
87
|
+
table_data[:columns] = {}
|
88
|
+
@columns.each do |column|
|
89
|
+
colm_name, colm_type = column.downcase.split ':'
|
90
|
+
if supported_datatype? colm_type
|
91
|
+
table_data[:columns][colm_name.to_sym] = colm_type
|
92
|
+
end
|
93
|
+
end
|
94
|
+
table_data[:table_name] = ActiveSupport::Inflector.tableize(@model)
|
95
|
+
create_table table_data
|
96
|
+
true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module NetMate
|
2
|
+
|
3
|
+
@@routes = {}
|
4
|
+
def self.routes= r
|
5
|
+
@@routes = r
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.routes
|
9
|
+
@@routes
|
10
|
+
end
|
11
|
+
|
12
|
+
class Request
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
Routes.new.create_routes
|
16
|
+
end
|
17
|
+
|
18
|
+
def call env
|
19
|
+
req = Rack::Request.new(env)
|
20
|
+
dispatcher = Dispatcher.new(env['PATH_INFO'], env['REQUEST_METHOD'], req.params)
|
21
|
+
dispatcher.dispatch
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module NetMate
|
2
|
+
class Response
|
3
|
+
|
4
|
+
def initialize body
|
5
|
+
@body = body
|
6
|
+
end
|
7
|
+
|
8
|
+
def respond
|
9
|
+
Rack::Response.new.finish do |res|
|
10
|
+
res['Content-Type'] = 'text/html'
|
11
|
+
res.status = 200
|
12
|
+
res.write @body
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def error
|
17
|
+
Rack::Response.new.finish do |res|
|
18
|
+
res['Content-Type'] = 'text/html'
|
19
|
+
res.status = 404
|
20
|
+
res.write @body
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module NetMate
|
2
|
+
|
3
|
+
class Routing
|
4
|
+
|
5
|
+
def get path
|
6
|
+
match path, to: path.tr('/','#'), via: 'get'
|
7
|
+
end
|
8
|
+
|
9
|
+
def post path
|
10
|
+
match path, to: path.tr('/','#'), via: 'post'
|
11
|
+
end
|
12
|
+
|
13
|
+
def match path, options
|
14
|
+
path = path == '/' ? path : "/#{path}"
|
15
|
+
controller, action = options[:to].split('#')
|
16
|
+
NetMate::routes[[path, options[:via]]] = [controller, action]
|
17
|
+
end
|
18
|
+
|
19
|
+
def resources controller, options = { only: [:new, :create, :edit, :update, :show, :index] }
|
20
|
+
options[:only].each do |action|
|
21
|
+
action = action.to_s
|
22
|
+
controller = controller.to_s
|
23
|
+
path = "/#{controller}/#{action}"
|
24
|
+
method = ['create', 'update'].include?(action) ? 'post' : 'get'
|
25
|
+
NetMate::routes[[path, method]] = [controller, action]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/net_mate/usage
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
Usage: net_mate COMMAND [ARGS]
|
2
|
+
|
3
|
+
Description:
|
4
|
+
COMMAND:
|
5
|
+
new Create a new net_mate application. "net_mate new my_app" creates
|
6
|
+
a new application called MyApp in "./my_app"
|
7
|
+
generate Generate controller/database/model (short-cut alias: "g")
|
8
|
+
server Start the net_mate server (short-cut alias: "s")
|
9
|
+
|
10
|
+
-----------------------------------------------------------------------------
|
11
|
+
Usage:
|
12
|
+
net_mate generate database
|
13
|
+
|
14
|
+
|
15
|
+
Description:
|
16
|
+
Creates a new database for the current application. Need to run this
|
17
|
+
command before generating a new model.
|
18
|
+
|
19
|
+
-----------------------------------------------------------------------------
|
20
|
+
Usage:
|
21
|
+
net_mate generate model NAME field:type field:type
|
22
|
+
|
23
|
+
|
24
|
+
Description:
|
25
|
+
Creates a new model. Pass the model name, either CamelCased or
|
26
|
+
under_scored, and an optional list of attribute pairs as arguments.
|
27
|
+
|
28
|
+
Attribute pairs are field:type arguments specifying the
|
29
|
+
model's attributes.
|
30
|
+
|
31
|
+
Available field types:
|
32
|
+
|
33
|
+
Just after the field name you can specify a type like text or boolean.
|
34
|
+
It will generate the column with the associated SQL type. For example:
|
35
|
+
|
36
|
+
`net_mate generate model post title:string body:text`
|
37
|
+
|
38
|
+
Will generate a title column with a varchar type and a body column with a
|
39
|
+
text type. You can use the following types:
|
40
|
+
|
41
|
+
boolean
|
42
|
+
string
|
43
|
+
integer
|
44
|
+
date
|
45
|
+
datetime
|
46
|
+
text
|
47
|
+
float
|
48
|
+
|
49
|
+
Examples:
|
50
|
+
`net_mate generate model account`
|
51
|
+
|
52
|
+
For Model it creates:
|
53
|
+
|
54
|
+
Model: app/models/account.rb
|
55
|
+
|
56
|
+
`net_mate generate model post title:string body:text published:boolean`
|
57
|
+
|
58
|
+
Creates a Post model with a string title, text body, and published
|
59
|
+
flag.
|
60
|
+
|
61
|
+
-----------------------------------------------------------------------------
|
62
|
+
Usage:
|
63
|
+
net_mate generate controller NAME [action action]
|
64
|
+
|
65
|
+
|
66
|
+
Description:
|
67
|
+
Creates out a new controller and its views. Pass the controller name,
|
68
|
+
either CamelCased or under_scored, and a list of views as arguments.
|
69
|
+
|
70
|
+
This generates a controller class in app/controllers.
|
71
|
+
|
72
|
+
Example:
|
73
|
+
`net_mate generate controller CreditCards open debit credit close`
|
74
|
+
|
75
|
+
CreditCards controller with URLs like /credit_cards/debit.
|
76
|
+
Controller: app/controllers/credit_cards_controller.rb
|
77
|
+
Views: app/views/credit_cards/debit.html.erb
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net_mate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Suyog Nerkar
|
8
|
+
- Sharang Dashputre
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-06-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: erubis
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: activesupport
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: mysql2
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
description: NetMate is a framework created to facilitate Web Development using Ruby.
|
71
|
+
The framework is an MVC framework which provides basic functionality to create a
|
72
|
+
web application.
|
73
|
+
email:
|
74
|
+
- suyog.nerkar@weboniselab.com
|
75
|
+
- sharang.dashputre@weboniselab.com
|
76
|
+
executables:
|
77
|
+
- net_mate
|
78
|
+
extensions: []
|
79
|
+
extra_rdoc_files: []
|
80
|
+
files:
|
81
|
+
- bin/net_mate
|
82
|
+
- lib/net_mate.rb
|
83
|
+
- lib/net_mate/connection.rb
|
84
|
+
- lib/net_mate/controller.rb
|
85
|
+
- lib/net_mate/controller_generator.rb
|
86
|
+
- lib/net_mate/create_application.rb
|
87
|
+
- lib/net_mate/database_generator.rb
|
88
|
+
- lib/net_mate/dispatcher.rb
|
89
|
+
- lib/net_mate/model.rb
|
90
|
+
- lib/net_mate/model_generator.rb
|
91
|
+
- lib/net_mate/request.rb
|
92
|
+
- lib/net_mate/response.rb
|
93
|
+
- lib/net_mate/routing.rb
|
94
|
+
- lib/net_mate/usage
|
95
|
+
homepage: ''
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 2.0.0
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 1.8.11
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.6.11
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: MVC Web application framework
|
119
|
+
test_files: []
|