tgauge 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/TestProj/Gemfile +3 -0
- data/TestProj/Gemfile.lock +44 -0
- data/TestProj/app/controllers/application_controller.rb +3 -0
- data/TestProj/app/controllers/trains_controller.rb +7 -0
- data/TestProj/app/models/train.rb +8 -0
- data/TestProj/config/database.yml +1 -0
- data/TestProj/config/routes.rb +9 -0
- data/TestProj/db/migrations/1476832713_create_train}.sql +4 -0
- data/TestProj/db/seeds.rb +10 -0
- data/bin/cli.rb +11 -11
- data/lib/app/models/routes.rb +7 -0
- data/lib/app/models/trecord_base.rb +44 -28
- data/lib/db/db_connection.rb +31 -26
- data/lib/templates/app/controllers/application_controller.rb +3 -0
- data/lib/templates/config/routes.rb +1 -1
- data/lib/version.rb +1 -1
- metadata +12 -2
- data/lib/templates/app/application_controller.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07619a69df37ace4f1529a5b897ca6353382cce2
|
4
|
+
data.tar.gz: 26768915f63cc53cd2ea4dc2f50c0eebf4ff568d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe5386f187073c27138a395b63dca860726ec1ad987ffa3679563eb8d25402be0ae0db573bb17904ff1134df880f5e61d52e0522b6b65a01bf3586cd9774c47b
|
7
|
+
data.tar.gz: de1aa7a3a56fdb41a4c8901236944aecfd2db72354eaf476071fce7940bd63dac70ebd1c4dff3c4de264f926da1714885a1bdf9a8562f92e1bcfe058a969a738
|
data/TestProj/Gemfile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (4.2.7.1)
|
5
|
+
i18n (~> 0.7)
|
6
|
+
json (~> 1.7, >= 1.7.7)
|
7
|
+
minitest (~> 5.1)
|
8
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
9
|
+
tzinfo (~> 1.1)
|
10
|
+
coderay (1.1.1)
|
11
|
+
fileutils (0.7)
|
12
|
+
rmagick (>= 2.13.1)
|
13
|
+
i18n (0.7.0)
|
14
|
+
json (1.8.3)
|
15
|
+
method_source (0.8.2)
|
16
|
+
minitest (5.9.1)
|
17
|
+
pg (0.19.0)
|
18
|
+
pry (0.10.4)
|
19
|
+
coderay (~> 1.1.0)
|
20
|
+
method_source (~> 0.8.1)
|
21
|
+
slop (~> 3.4)
|
22
|
+
rack (1.6.4)
|
23
|
+
rmagick (2.16.0)
|
24
|
+
slop (3.6.0)
|
25
|
+
tgauge (0.1.7)
|
26
|
+
activesupport (~> 4.2, >= 4.2.5.1)
|
27
|
+
fileutils (~> 0.7)
|
28
|
+
pg (~> 0.18)
|
29
|
+
pry (~> 0.10.3)
|
30
|
+
rack (~> 1.6, >= 1.6.4)
|
31
|
+
thor (~> 0.19)
|
32
|
+
thor (0.19.1)
|
33
|
+
thread_safe (0.3.5)
|
34
|
+
tzinfo (1.2.2)
|
35
|
+
thread_safe (~> 0.1)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
tgauge
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
1.13.0
|
@@ -0,0 +1 @@
|
|
1
|
+
database: TestProj
|
data/bin/cli.rb
CHANGED
@@ -6,19 +6,19 @@ module TGauge
|
|
6
6
|
class Generate < Thor
|
7
7
|
desc 'model <name>', 'generate a model.'
|
8
8
|
def model(name)
|
9
|
-
File.open(
|
9
|
+
File.open("./app/models/#{name.downcase}.rb", 'w') do |f|
|
10
10
|
f.write("class #{name.capitalize} < TGauge::TRecordBase\n\n")
|
11
11
|
f.write("end\n")
|
12
12
|
f.write("#{name.capitalize}.finalize!")
|
13
13
|
end
|
14
14
|
|
15
|
-
migration("
|
15
|
+
migration("#{name.capitalize}")
|
16
16
|
puts "#{name} model created"
|
17
17
|
end
|
18
18
|
|
19
19
|
desc 'controller <name>', 'generate a controller.'
|
20
20
|
def controller(name)
|
21
|
-
File.open(
|
21
|
+
File.open("./app/controllers/#{name.downcase}_controller.rb", 'w') do |f|
|
22
22
|
f.write("class #{name.capitalize}Controller < TGauge::TControllerBase\n\n")
|
23
23
|
f.write("end")
|
24
24
|
end
|
@@ -33,7 +33,7 @@ module TGauge
|
|
33
33
|
ts = Time.now.to_i
|
34
34
|
filename = "#{ts}_#{name.underscore.downcase}"
|
35
35
|
|
36
|
-
File.open(
|
36
|
+
File.open("./db/migrations/#{filename}.sql", 'w') do |f|
|
37
37
|
f.write "CREATE TABLE IF NOT EXISTS #{name} (\n"
|
38
38
|
f.write "\tid SERIAL PRIMARY KEY,\n"
|
39
39
|
f.write ')'
|
@@ -44,7 +44,7 @@ module TGauge
|
|
44
44
|
class Db < Thor
|
45
45
|
desc 'create', 'creates the DB'
|
46
46
|
def create
|
47
|
-
require_relative '../lib/db_connection'
|
47
|
+
require_relative '../lib/db/db_connection'
|
48
48
|
TGauge::DBConnection.reset
|
49
49
|
puts 'db created!'
|
50
50
|
end
|
@@ -53,14 +53,14 @@ module TGauge
|
|
53
53
|
def migrate
|
54
54
|
# Creates Version table if necessary,
|
55
55
|
# then runs needed migrations in order.
|
56
|
-
require_relative '../lib/db_connection'
|
56
|
+
require_relative '../lib/db/db_connection'
|
57
57
|
TGauge::DBConnection.migrate
|
58
58
|
puts 'migrated!'
|
59
59
|
end
|
60
60
|
|
61
61
|
desc 'seed', 'seeds the DB'
|
62
62
|
def seed
|
63
|
-
require_relative '../lib/
|
63
|
+
require_relative '../lib/tgauge'
|
64
64
|
TGauge::Seed.populate
|
65
65
|
puts 'db seeded!'
|
66
66
|
end
|
@@ -102,18 +102,18 @@ module TGauge
|
|
102
102
|
Dir.mkdir "./#{name}/app/views"
|
103
103
|
Dir.mkdir "./#{name}/app/controllers"
|
104
104
|
File.open("./#{name}/app/controllers/application_controller.rb", 'w') do |f|
|
105
|
-
f.write File.read(File.expand_path('../../lib/
|
105
|
+
f.write File.read(File.expand_path('../../lib/templates/app/controllers/application_controller.rb', __FILE__))
|
106
106
|
end
|
107
107
|
File.open("./#{name}/config/routes.rb", 'w') do |f|
|
108
|
-
f.write File.read(File.expand_path('../../lib/
|
108
|
+
f.write File.read(File.expand_path('../../lib/templates/config/routes.rb', __FILE__))
|
109
109
|
end
|
110
110
|
Dir.mkdir "./#{name}/db"
|
111
111
|
Dir.mkdir "./#{name}/db/migrations"
|
112
112
|
File.open("./#{name}/db/seeds.rb", 'w') do |f|
|
113
|
-
f.write File.read(File.expand_path('../../lib/
|
113
|
+
f.write File.read(File.expand_path('../../lib/templates/db/seeds.rb', __FILE__))
|
114
114
|
end
|
115
115
|
File.open("./#{name}/Gemfile", 'w') do |f|
|
116
|
-
f.write File.read(File.expand_path('../../lib/
|
116
|
+
f.write File.read(File.expand_path('../../lib/templates/Gemfile', __FILE__))
|
117
117
|
end
|
118
118
|
end
|
119
119
|
end
|
@@ -28,17 +28,20 @@ module TGauge
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
def self.destroy_all
|
32
|
+
all.each(&:destroy!)
|
33
|
+
end
|
34
|
+
|
31
35
|
def self.columns
|
32
36
|
# ...
|
33
37
|
return @columns if @columns
|
34
38
|
|
35
39
|
arr = DBConnection.execute(<<-SQL)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
SELECT
|
41
|
+
*
|
42
|
+
FROM
|
43
|
+
#{self.table_name}
|
40
44
|
SQL
|
41
|
-
|
42
45
|
@columns = []
|
43
46
|
arr.nfields.times do |i|
|
44
47
|
@columns << arr.fname(i)
|
@@ -49,7 +52,7 @@ module TGauge
|
|
49
52
|
|
50
53
|
def self.finalize!
|
51
54
|
columns.each do |column|
|
52
|
-
|
55
|
+
inst_var = "@" + column.to_s
|
53
56
|
define_method(column) do
|
54
57
|
attributes[column]
|
55
58
|
end
|
@@ -69,10 +72,10 @@ module TGauge
|
|
69
72
|
|
70
73
|
def self.all
|
71
74
|
objs_arr = DBConnection.execute(<<-SQL)
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
75
|
+
SELECT
|
76
|
+
#{table_name}.*
|
77
|
+
FROM
|
78
|
+
#{table_name}
|
76
79
|
SQL
|
77
80
|
|
78
81
|
parse_all(objs_arr)
|
@@ -85,12 +88,12 @@ module TGauge
|
|
85
88
|
|
86
89
|
def self.find(id)
|
87
90
|
obj = DBConnection.execute(<<-SQL, id)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
91
|
+
SELECT
|
92
|
+
#{table_name}.*
|
93
|
+
FROM
|
94
|
+
#{table_name}
|
95
|
+
WHERE
|
96
|
+
#{table_name}.id = ?
|
94
97
|
SQL
|
95
98
|
|
96
99
|
parse_all(obj).first
|
@@ -118,26 +121,39 @@ module TGauge
|
|
118
121
|
attr_count = cols.count
|
119
122
|
column_str = cols.join(", ")
|
120
123
|
quest_str = Array.new(attr_count) {"?"}.join(", ")
|
121
|
-
|
124
|
+
|
122
125
|
DBConnection.execute(<<-SQL, attribute_values)
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
126
|
+
INSERT INTO
|
127
|
+
#{table_name} (#{column_str})
|
128
|
+
VALUES
|
129
|
+
(#{quest_str})
|
127
130
|
SQL
|
128
131
|
end
|
129
132
|
|
133
|
+
def destroy!
|
134
|
+
if self.class.find(id)
|
135
|
+
Puffs::DBConnection.execute(<<-SQL)
|
136
|
+
DELETE
|
137
|
+
FROM
|
138
|
+
#{self.class.table_name}
|
139
|
+
WHERE
|
140
|
+
id = #{id}
|
141
|
+
SQL
|
142
|
+
return self
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
130
146
|
def update
|
131
|
-
|
147
|
+
attr_count = columns.count - 1
|
132
148
|
column_str = columns[1..-1].map { |col| "#{col} = ?" }.join(", ")
|
133
149
|
|
134
150
|
DBConnection.execute(<<-SQL, attribute_values)
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
151
|
+
UPDATE
|
152
|
+
#{table_name}
|
153
|
+
SET
|
154
|
+
#{column_str}
|
155
|
+
WHERE
|
156
|
+
id = ?
|
141
157
|
SQL
|
142
158
|
end
|
143
159
|
|
data/lib/db/db_connection.rb
CHANGED
@@ -28,60 +28,65 @@ module TGauge
|
|
28
28
|
instance.exec(File.read(file))
|
29
29
|
instance.exec(<<-SQL)
|
30
30
|
INSERT INTO
|
31
|
-
|
31
|
+
migrations (filename)
|
32
32
|
VALUES
|
33
|
-
|
34
|
-
|
33
|
+
('#{filename}')
|
34
|
+
SQL
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.instance
|
40
|
-
open if @
|
40
|
+
open if @postgres.nil?
|
41
41
|
|
42
|
-
@
|
42
|
+
@postgres
|
43
43
|
end
|
44
44
|
|
45
45
|
def self.execute(*args)
|
46
|
+
args.flatten!
|
47
|
+
|
46
48
|
print_query(*args)
|
47
|
-
|
49
|
+
query = self.number_placeholders(args.unshift[0])
|
50
|
+
params = args[1..-1]
|
51
|
+
params.flatten!
|
52
|
+
instance.exec(query, params)
|
48
53
|
end
|
49
54
|
|
50
|
-
|
51
|
-
instance.last_insert_row_id
|
52
|
-
end
|
55
|
+
private
|
53
56
|
|
54
|
-
def self.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
def self.number_placeholders(query_string)
|
58
|
+
count = 0
|
59
|
+
query_string.chars.map do |char|
|
60
|
+
if char == "?"
|
61
|
+
count += 1
|
62
|
+
"$#{count}"
|
63
|
+
else
|
64
|
+
char
|
65
|
+
end
|
66
|
+
end.join("")
|
60
67
|
end
|
61
68
|
|
62
|
-
private
|
63
|
-
|
64
69
|
def self.ensure_migrations_table
|
65
70
|
table = instance.exec(<<-SQL)
|
66
|
-
|
71
|
+
SELECT to_regclass('migrations') AS exists
|
67
72
|
SQL
|
68
73
|
|
69
74
|
unless table[0]['exists']
|
70
75
|
instance.exec(<<-SQL)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
76
|
+
CREATE TABLE migrations(
|
77
|
+
id SERIAL PRIMARY KEY,
|
78
|
+
filename VARCHAR(255) NOT NULL
|
79
|
+
)
|
75
80
|
SQL
|
76
81
|
end
|
77
82
|
end
|
78
83
|
|
79
84
|
def self.migrated_files
|
80
85
|
Set.new instance.exec(<<-SQL).values.flatten
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
86
|
+
SELECT
|
87
|
+
filename
|
88
|
+
FROM
|
89
|
+
migrations
|
85
90
|
SQL
|
86
91
|
end
|
87
92
|
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tgauge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nam Kim
|
@@ -147,6 +147,15 @@ files:
|
|
147
147
|
- README.md
|
148
148
|
- Rakefile
|
149
149
|
- TGauge.gemspec
|
150
|
+
- TestProj/Gemfile
|
151
|
+
- TestProj/Gemfile.lock
|
152
|
+
- TestProj/app/controllers/application_controller.rb
|
153
|
+
- TestProj/app/controllers/trains_controller.rb
|
154
|
+
- TestProj/app/models/train.rb
|
155
|
+
- TestProj/config/database.yml
|
156
|
+
- TestProj/config/routes.rb
|
157
|
+
- TestProj/db/migrations/1476832713_create_train}.sql
|
158
|
+
- TestProj/db/seeds.rb
|
150
159
|
- bin/cli.rb
|
151
160
|
- bin/console
|
152
161
|
- bin/setup
|
@@ -155,6 +164,7 @@ files:
|
|
155
164
|
- lib/app/controllers/session.rb
|
156
165
|
- lib/app/controllers/tcontroller_base.rb
|
157
166
|
- lib/app/models/associatable.rb
|
167
|
+
- lib/app/models/routes.rb
|
158
168
|
- lib/app/models/searchable.rb
|
159
169
|
- lib/app/models/trecord_base.rb
|
160
170
|
- lib/db/db_connection.rb
|
@@ -164,7 +174,7 @@ files:
|
|
164
174
|
- lib/db/static_viewer.rb
|
165
175
|
- lib/db/templates/error_page.html.erb
|
166
176
|
- lib/templates/Gemfile
|
167
|
-
- lib/templates/app/application_controller.rb
|
177
|
+
- lib/templates/app/controllers/application_controller.rb
|
168
178
|
- lib/templates/config/routes.rb
|
169
179
|
- lib/templates/db/seeds.rb
|
170
180
|
- lib/tgauge.rb
|