tgauge 0.1.7 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9e8bc7c2dc687cd9ad7bcf149d6871f0b583df4
4
- data.tar.gz: fede56a08aedad5dffb5eb7f8eeb5c12580e3019
3
+ metadata.gz: 07619a69df37ace4f1529a5b897ca6353382cce2
4
+ data.tar.gz: 26768915f63cc53cd2ea4dc2f50c0eebf4ff568d
5
5
  SHA512:
6
- metadata.gz: 5827a7e47d6f5dcaaca9b36e05dad7d92b0a7e2f8db291458ef04260861c0f4cad7c9ce40adb4e0ce60482f17d0f53c7600b32cee1fa5685bf5b5cb625b8ed2a
7
- data.tar.gz: 198685c673d66bc80f57f24beb1aa56a03066ee7d509f819ba1fed55ce11f23f842c11a80ed863ea289d9c9ef46900eaa76695c95a22adabf6bb91922f805758
6
+ metadata.gz: fe5386f187073c27138a395b63dca860726ec1ad987ffa3679563eb8d25402be0ae0db573bb17904ff1134df880f5e61d52e0522b6b65a01bf3586cd9774c47b
7
+ data.tar.gz: de1aa7a3a56fdb41a4c8901236944aecfd2db72354eaf476071fce7940bd63dac70ebd1c4dff3c4de264f926da1714885a1bdf9a8562f92e1bcfe058a969a738
data/TestProj/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "tgauge"
@@ -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,3 @@
1
+ class ApplicationController < TGauge::TControllerBase
2
+ #Prevent CSRF attacks by raising an exception.
3
+ end
@@ -0,0 +1,7 @@
1
+ class TrainsController < TGauge::TControllerBase
2
+ def index
3
+ @trains = Train.all
4
+
5
+ render_content(@trains, "application.json")
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class Train < TGauge::TRecordBase
2
+ def index
3
+ @trains = Train.all
4
+
5
+ render_content(JSON.generate(@users), "application.json")
6
+ end
7
+ end
8
+ Train.finalize!
@@ -0,0 +1 @@
1
+ database: TestProj
@@ -0,0 +1,9 @@
1
+ ROUTER = TGauge::Router.new
2
+
3
+ ROUTER.draw do
4
+ #ADD ROUTES HERE
5
+ #EXAMPLE:
6
+ #get Regexp.new("^/MODEL/"), CONTROLLER, :index
7
+
8
+ get Regexp.new("^/$"), TrainsController, :index
9
+ end
@@ -0,0 +1,4 @@
1
+ CREATE TABLE IF NOT EXISTS trains (
2
+ id SERIAL PRIMARY KEY,
3
+ sound VARCHAR(255)
4
+ )
@@ -0,0 +1,10 @@
1
+ module TGauge
2
+ class Seed
3
+ def self.populate
4
+ #PUT IN SEEDS HERE
5
+ Train.destroy_all
6
+
7
+ Train.new({sound: "choo choo"}).save
8
+ end
9
+ end
10
+ end
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('./app/models/#{name.downcase}.rb', 'w') do |f|
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("Create#{name.capitalize}}")
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('./app/controllers/#{name.downcase}.rb', 'w') do |f|
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('./db/migrations/#{filename}.sql', 'w') do |f|
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/puffs'
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/template/app/controllers/application_controller.rb', __FILE__))
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/template/config/routes.rb', __FILE__))
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/template/db/seeds.rb', __FILE__))
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/template/Gemfile', __FILE__))
116
+ f.write File.read(File.expand_path('../../lib/templates/Gemfile', __FILE__))
117
117
  end
118
118
  end
119
119
  end
@@ -0,0 +1,7 @@
1
+ ROUTER = TGauge::Router.new
2
+
3
+ ROUTER.draw do
4
+ #ADD ROUTES HERE
5
+ #EXAMPLE:
6
+ #get Regexp.new("^/MODEL/$"), CONTROLLER, :index
7
+ 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
- SELECT
37
- *
38
- FROM
39
- #{self.table_name}
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
- # inst_var = "@" + column.to_s
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
- SELECT
73
- #{table_name}.*
74
- FROM
75
- #{table_name}
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
- SELECT
89
- #{table_name}.*
90
- FROM
91
- #{table_name}
92
- WHERE
93
- #{table_name}.id = ?
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
- debugger
124
+
122
125
  DBConnection.execute(<<-SQL, attribute_values)
123
- INSERT INTO
124
- #{table_name} (#{column_str})
125
- VALUES
126
- (#{quest_str})
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
- # attr_count = columns.count - 1
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
- UPDATE
136
- #{table_name}
137
- SET
138
- #{column_str}
139
- WHERE
140
- id = ?
151
+ UPDATE
152
+ #{table_name}
153
+ SET
154
+ #{column_str}
155
+ WHERE
156
+ id = ?
141
157
  SQL
142
158
  end
143
159
 
@@ -28,60 +28,65 @@ module TGauge
28
28
  instance.exec(File.read(file))
29
29
  instance.exec(<<-SQL)
30
30
  INSERT INTO
31
- migrations (filename)
31
+ migrations (filename)
32
32
  VALUES
33
- ('#{filename}')
34
- SQL
33
+ ('#{filename}')
34
+ SQL
35
35
  end
36
36
  end
37
37
  end
38
38
 
39
39
  def self.instance
40
- open if @db.nil?
40
+ open if @postgres.nil?
41
41
 
42
- @db
42
+ @postgres
43
43
  end
44
44
 
45
45
  def self.execute(*args)
46
+ args.flatten!
47
+
46
48
  print_query(*args)
47
- instance.execute(*args)
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
- def self.last_insert_row_id
51
- instance.last_insert_row_id
52
- end
55
+ private
53
56
 
54
- def self.reset
55
- commands = [
56
- "dropdb #{app_name}",
57
- "createdb #{app_name}"
58
- ]
59
- commands.each { |command| `#{command}` }
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
- SELECT to_regclass('migrations') AS exists
71
+ SELECT to_regclass('migrations') AS exists
67
72
  SQL
68
73
 
69
74
  unless table[0]['exists']
70
75
  instance.exec(<<-SQL)
71
- CREATE_TABLE migrations(
72
- id SERIAL PRIMARY KEY,
73
- filename VARCHAR(255) NOT NULL
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
- SELECT
82
- filename
83
- FROM
84
- migrations
86
+ SELECT
87
+ filename
88
+ FROM
89
+ migrations
85
90
  SQL
86
91
  end
87
92
 
@@ -0,0 +1,3 @@
1
+ class ApplicationController < TGauge::TControllerBase
2
+ #Prevent CSRF attacks by raising an exception.
3
+ end
@@ -3,5 +3,5 @@ ROUTER = TGauge::Router.new
3
3
  ROUTER.draw do
4
4
  #ADD ROUTES HERE
5
5
  #EXAMPLE:
6
- #get Regexp.new("^/MODEL/"), CONTROLLER, :index
6
+ #get Regexp.new("^/MODEL/$"), CONTROLLER, :index
7
7
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module TGauge
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.7
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
@@ -1,3 +0,0 @@
1
- class ApplicationController < Puffs::ControllerBase
2
- #Prevent CSRF attacks by raising an exception.
3
- end