surfer 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.
@@ -0,0 +1,289 @@
1
+ module Surfer
2
+ require ::File.expand_path('../connection',__FILE__)
3
+ require ::File.expand_path('../support',__FILE__)
4
+
5
+ class Operation
6
+ # @@columns = nil
7
+ CONNECTION = Connection.new
8
+ SUPPORT = Support.new
9
+ @conn = nil
10
+
11
+ # initializer method.
12
+
13
+ def initialize(arg={})
14
+ puts self.class
15
+ arg.each do |a, v|
16
+ puts "a = #{a} p = #{v}"
17
+ self.send("#{a}=", v)
18
+ #eval("self.#{a} = '#{v}'")
19
+ end
20
+ end
21
+
22
+ # Drop commnad.
23
+
24
+
25
+ def self.drop
26
+ begin
27
+ @conn = CONNECTION.create_connection
28
+ table_name = SUPPORT.get_pluralize( "#{self.name}" )
29
+ @conn.do( "DROP TABLE IF EXISTS #{table_name}" )
30
+ @conn.commit
31
+ rescue DBI::DatabaseError => e
32
+ puts "Error code : #{e.err}"
33
+ puts "Error message : #{e.errstr}"
34
+ @conn.rollback
35
+ else
36
+ puts "Table droped successfully."
37
+ ensure
38
+ CONNECTION.close_connection
39
+ end
40
+ end
41
+
42
+ # select All with attribute and without attribute command.
43
+
44
+ def self.all attribute = {}
45
+ objects = []
46
+ table_name = SUPPORT.get_pluralize( "#{self.name}" )
47
+ begin
48
+ @conn = CONNECTION.create_connection
49
+ if attribute.empty?
50
+ record = @conn.prepare( " SELECT * FROM #{table_name} " )
51
+ else
52
+ record = @conn.prepare( " SELECT #{attribute} FROM #{table_name} " )
53
+ end
54
+ record.execute()
55
+ @columns = record.column_names
56
+ record.fetch do |e|
57
+ hash = {}
58
+ e.entries.each_with_index do |v,i|
59
+ @columns[i].to_sym
60
+ hash[@columns[i].to_sym] = v
61
+ end
62
+ obj = self.new({})
63
+ hash.each{|k,v| obj.send("#{k}=",v)}
64
+ objects << obj
65
+ end
66
+ return objects
67
+ rescue DBI::DatabaseError => e
68
+ puts "Error code : #{e.err}"
69
+ puts "Error message : #{e.errstr}"
70
+ else
71
+ puts " Records fetched successfully. "
72
+ ensure
73
+ if @conn
74
+ CONNECTION.close_connection
75
+ end
76
+ end
77
+ end
78
+
79
+ # Save command.
80
+
81
+ def save
82
+ hash = {}
83
+ self.class.columns.each do |c|
84
+ if c != "id"
85
+ hash[c] = eval "self.#{c}"
86
+ end #@args[0]
87
+ end
88
+ table_name = SUPPORT.get_pluralize( "#{self.class}" )
89
+ query = SUPPORT.generate_insert( table_name, hash )
90
+ puts query
91
+ begin
92
+ @conn = CONNECTION.create_connection
93
+ @conn.do(query)
94
+ @conn.commit
95
+ self.id = @conn.select_one("SELECT LAST_INSERT_ID()").last
96
+ rescue DBI::DatabaseError => e
97
+ puts "Error code : #{e.err}"
98
+ puts "Error message : #{e.errstr}"
99
+ @conn.rollback
100
+ else
101
+ puts " Record inserted successfully. "
102
+ ensure
103
+ CONNECTION.close_connection
104
+ end
105
+ end
106
+
107
+ # Update Command.
108
+
109
+ def update argv
110
+ table_name = SUPPORT.get_pluralize( "#{self.class}" )
111
+ if self.id != nil
112
+ query = SUPPORT.generate_update( table_name, argv, "id" => "#{self.id}" )
113
+ else
114
+ puts " Record does not persist in database. "
115
+ return
116
+ end
117
+ begin
118
+ @conn = CONNECTION.create_connection
119
+ @conn.do(query)
120
+ @conn.commit
121
+ rescue DBI::DatabaseError => e
122
+ puts "Error code : #{e.err}"
123
+ puts "Error message : #{e.errstr}"
124
+ @conn.rollback
125
+ else
126
+ puts " Record updated successfully. "
127
+ ensure
128
+ CONNECTION.close_connection
129
+ end
130
+ end
131
+
132
+ # Remove Command
133
+
134
+ def remove
135
+ table_name = SUPPORT.get_pluralize( "#{self.class}" )
136
+ if self.id != nil
137
+ query = SUPPORT.generate_remove( table_name, "id" => "#{self.id}" )
138
+ else
139
+ puts " Record does not persist in database. "
140
+ return
141
+ end
142
+ puts query
143
+ begin
144
+ @conn = CONNECTION.create_connection
145
+ if @conn.do(query) == 1
146
+ @conn.commit
147
+ self.id = nil
148
+ puts " Record remove successfully. "
149
+ else
150
+ puts " Record does not exist in database. "
151
+ end
152
+ rescue DBI::DatabaseError => e
153
+ puts "Error code : #{e.err}"
154
+ puts "Error message : #{e.errstr}"
155
+ @conn.rollback
156
+ ensure
157
+ puts "Iam Here :) "
158
+ CONNECTION.close_connection
159
+ end
160
+ end
161
+
162
+ # Where clouse.
163
+
164
+ def self.where argv
165
+ table_name = SUPPORT.get_pluralize( "#{self.name}" )
166
+ query = SUPPORT.generate_where( table_name, argv )
167
+ begin
168
+ @conn = CONNECTION.create_connection
169
+ objects = []
170
+ record = @conn.prepare( "#{query}" )
171
+ record.execute()
172
+ @columns = record.column_names
173
+ record.fetch do |e|
174
+ hash = {}
175
+ e.entries.each_with_index do |v,i|
176
+ @columns[i].to_sym
177
+ hash[@columns[i].to_sym] = v
178
+ end
179
+ obj = self.new({})
180
+ puts obj.class
181
+ puts "Iam Here"
182
+ puts hash.inspect
183
+ hash.each{|k,v| obj.send("#{k}=",v)}
184
+ puts "Iam Not Here"
185
+ # objects << hash
186
+ objects << obj
187
+ end
188
+
189
+ rescue DBI::DatabaseError => e
190
+ puts "Error code : #{e.err}"
191
+ puts "Error message : #{e.errstr}"
192
+ else
193
+ puts " Records fetched successfully. "
194
+ ensure
195
+ if @conn
196
+ CONNECTION.close_connection
197
+ end
198
+ end
199
+
200
+ objects
201
+ end
202
+
203
+ # Index creation.
204
+
205
+ def self.index argv
206
+ table_name = SUPPORT.get_pluralize( "#{self.name}" )
207
+ query = SUPPORT.generate_index( table_name, argv )
208
+
209
+ begin
210
+ @conn = CONNECTION.create_connection
211
+ @conn.do(query)
212
+ @conn.commit
213
+ rescue DBI::DatabaseError => e
214
+ puts "Error code : #{e.err}"
215
+ puts "Error message : #{e.errstr}"
216
+ @conn.rollback
217
+ else
218
+ puts " Index created successfully. "
219
+ ensure
220
+ CONNECTION.close_connection
221
+ end
222
+ end
223
+
224
+ # Index distroy.
225
+
226
+ def self.dindex argv
227
+ table_name = SUPPORT.get_pluralize( "#{self.name}" )
228
+ query = SUPPORT.generate_dindex( table_name, argv )
229
+
230
+ begin
231
+ @conn = CONNECTION.create_connection
232
+ @conn.do( query )
233
+ @conn.commit
234
+ rescue DBI::DatabaseError => e
235
+ puts "Error code : #{e.err}"
236
+ puts "Error message : #{e.errstr}"
237
+ @conn.rollback
238
+ else
239
+ puts " Index removes successfully. "
240
+ ensure
241
+ CONNECTION.close_connection
242
+ end
243
+ end
244
+
245
+ # Loading Table column from database.
246
+
247
+ def self.set_columns
248
+ puts "*" * 100
249
+ puts self.inspect
250
+ unless columns
251
+ table_name = SUPPORT.get_pluralize(self.name)
252
+ begin
253
+ if @conn == nil
254
+ @conn = CONNECTION.create_connection
255
+ record = @conn.prepare( " select * from #{table_name} " )
256
+ record.execute()
257
+ puts "inside setcoloums #{self.name}"
258
+ self.class_variable_set(:@@columns,record.column_names)
259
+ set_accessor(self, columns )
260
+ end
261
+ rescue DBI::DatabaseError => e
262
+ puts "Error code : #{e.err}"
263
+ puts "Error message : #{e.errstr}"
264
+ ensure
265
+ if @conn
266
+ CONNECTION.close_connection
267
+ end
268
+ end
269
+ end
270
+ columns
271
+ end
272
+
273
+ # Setting Attribute accessor.
274
+
275
+ def self.set_accessor(base,argv)
276
+ argv.each do |c|
277
+ base.class_eval do
278
+ attr_accessor c
279
+ end
280
+ end
281
+ end
282
+
283
+ # setting column accessor.
284
+ def self.columns
285
+ self.class_variable_get(:@@columns) rescue nil
286
+ end
287
+
288
+ end
289
+ end
@@ -0,0 +1,32 @@
1
+ module Surfer
2
+ class Options
3
+ def self.all_opitons
4
+ puts "Available Options"
5
+ z="replace [whatever] with your requirements"
6
+ puts "\033[34m#{z}\033[0m"
7
+ puts "surfer new [app_name]"
8
+ puts "surfer generate model [model_name] [fields]"
9
+ puts "surfer generate contoller [controller_name]"
10
+ end
11
+ end
12
+ class Custom_Colors
13
+ def black; "\033[30m#{str}\033[0m" end
14
+ def red; "\033[31m#{str}\033[0m" end
15
+ def green; "\033[32m#{str}\033[0m" end
16
+ def brown; "\033[33m#{str}\033[0m" end
17
+ def blue; "\033[34m#{str}\033[0m" end
18
+ def magenta; "\033[35m#{str}\033[0m" end
19
+ def cyan; "\033[36m#{str}\033[0m" end
20
+ def gray; "\033[37m#{str}\033[0m" end
21
+ def bg_black; "\033[40m#{str}\0330m" end
22
+ def bg_red; "\033[41m#{str}\033[0m" end
23
+ def bg_green; "\033[42m#{str}\033[0m" end
24
+ def bg_brown; "\033[43m#{str}\033[0m" end
25
+ def bg_blue; "\033[44m#{str}\033[0m" end
26
+ def bg_magenta; "\033[45m#{str}\033[0m" end
27
+ def bg_cyan; "\033[46m#{str}\033[0m" end
28
+ def bg_gray; "\033[47m#{str}\033[0m" end
29
+ def bold; "\033[1m#{str}\033[22m" end
30
+ def reverse_color; "\033[7m#{str}\033[27m" end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module Surfer
2
+ require ::File.expand_path('../config',__FILE__)
3
+ class ReadDbFile
4
+ require 'yaml'
5
+ require 'fileutils'
6
+ def read_file
7
+ db_path= Config.root_path+"/config/database.yml"
8
+ puts "DataBase Configuration Path #{db_path}"
9
+ begin
10
+ docs = YAML::load(File.read(db_path) )
11
+ rescue
12
+ abort ("Your Not Inside any Application\n Please change your directory to Application Directory")
13
+ end
14
+ return docs
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ module Surfer
2
+ class Application
3
+ @@routes_controller=[]
4
+ def get_controller_and_action(env)
5
+ unwanted, cont, action, after = env["PATH_INFO"].split('/', 4)
6
+ puts "Inside"
7
+ puts cont
8
+ route = @@routes_controller.select{|f| f[:path] == "#{cont}"}
9
+ puts route.empty?
10
+ if(route.empty?)
11
+ return ["no_such_path","0","0"]
12
+ else
13
+ cn = route[0]
14
+ puts "Requested Path = #{cn[:path]}"
15
+ puts "Controller Called #{cn[:controller]}"
16
+ puts "Action called #{cn[:action]}"
17
+ # autoload="#{cn[:controller]}_controller"
18
+ # puts "Controller FIle #{autoload}"
19
+ # require "#{autoload}"
20
+ cont = cn[:controller].capitalize # Capitalize Controller eg : Webonise
21
+ if(action.nil?)
22
+ action=cn[:action]
23
+ end
24
+ if(action!=cn[:action])
25
+ return ["0","no_such_action","0"]
26
+ end
27
+
28
+ # Append Controller eg : WeboniseController
29
+ [Object.const_get(cont+"Controller"), action, cont]
30
+ end
31
+ end
32
+
33
+ def self.get args
34
+ @@routes_controller<<args
35
+ end
36
+
37
+ def self.check_route (cont)
38
+ puts "Inside"
39
+ route = @@routes_controller.select{|f| f[:controller] == "#{cont}"}
40
+ puts route
41
+ cn = route[0]
42
+ puts cn[:path]
43
+ puts cn[:controller]
44
+ puts cn[:action]
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,217 @@
1
+ module Surfer
2
+ class Support
3
+
4
+ # Plural method for pluralization purpose.
5
+
6
+ def get_pluralize name
7
+ until name.empty?
8
+ str = name.downcase.concat('s')
9
+ return str
10
+ end
11
+ end
12
+
13
+ # Getting sql specific datatype.
14
+
15
+ def get_data_type key
16
+ data_type = Hash.new
17
+ data_type["int"] = "INTEGER"
18
+ data_type["mobile"] = "BIGINT(20)"
19
+ data_type["string"] = "VARCHAR(50)"
20
+ data_type["float"] = "INTEGER(20,5)"
21
+ data_type["integer"] = "INTEGER"
22
+ data_type["text"] = "TEXT"
23
+ data_type["date"] = "DATE"
24
+
25
+ tdata = data_type[key]
26
+ end
27
+
28
+ # Parsing command line argument for table creating.
29
+
30
+ def create_field argv
31
+ i = 3
32
+ str = ""
33
+ str = " id INTEGER AUTO_INCREMENT PRIMARY KEY, "
34
+ begin
35
+ att = argv[i]
36
+ str << att
37
+ str << " "
38
+ i = i.to_i + 2
39
+ att1 = get_data_type(argv[i])
40
+ str << att1
41
+ if i.to_i != argv.length.to_i-1
42
+ str << ","
43
+ str << "\n"
44
+ i = i.to_i + 2
45
+ else
46
+ return str
47
+ end
48
+ end while i < argv.length.to_i
49
+ end
50
+
51
+ # Table creation query.
52
+
53
+ def create_table argv
54
+ query = "CREATE TABLE #{get_pluralize(argv[2])}( #{create_field(argv)} )"
55
+ puts query
56
+ return query
57
+ end
58
+
59
+ # Parsing insert command attribute.
60
+
61
+ def generate_insert tab_name, argv
62
+
63
+ # Attribute of table.
64
+ attribute = ""
65
+ attribute << "id"
66
+ i = 0
67
+ argv.each do |k,v|
68
+ if i == 0 and argv.length != 0
69
+ attribute << ","
70
+ end
71
+ attribute << k
72
+ i = i.to_i + 1
73
+ if i != argv.length
74
+ attribute << ","
75
+ end
76
+ end
77
+ # Values of the Attributes.
78
+ val = []
79
+ val << "NULL"
80
+ i = 0
81
+ if argv.length != 0
82
+ begin
83
+ val << "\"#{argv[argv.keys[i]]}\""
84
+ i = i.to_i + 1
85
+ end while i < argv.length.to_i
86
+ end
87
+
88
+ if argv.length == 0
89
+ query = " INSERT INTO #{tab_name}(#{attribute}) VALUES (#{val * ""}) "
90
+ return query
91
+ else
92
+ query = " INSERT INTO #{tab_name}(#{attribute}) VALUES (#{val * ","}) "
93
+ return query
94
+ end
95
+ end
96
+
97
+ # Parsing coditional parameter for delete command.
98
+
99
+ def generate_remove tab_name, argv
100
+ condition = ""
101
+ condition << "( "
102
+ i = 0
103
+ begin
104
+ if "condition" != argv.keys[i]
105
+ condition << argv.keys[i]
106
+ condition << "="
107
+ condition << "\"#{argv[argv.keys[i]]}\""
108
+ condition << " "
109
+ i = i.to_i + 1
110
+ else
111
+ condition << "\"#{argv[argv.keys[i]]}\""
112
+ condition << " "
113
+ i = i.to_i + 1
114
+ end
115
+ end while i < argv.length
116
+ condition << ")"
117
+ query = " DELETE FROM #{tab_name} WHERE#{condition} "
118
+ #puts query
119
+ return query
120
+ end
121
+
122
+ # Parsing attributes and values for update command.
123
+
124
+ def generate_update tab_name, argv, cond_argv
125
+
126
+ # Parsing updatable attribute
127
+ attribute = ""
128
+ i = 0
129
+ begin
130
+ attribute << argv.keys[i]
131
+ attribute << " = "
132
+ attribute << "\"#{argv[argv.keys[i]]}\""
133
+ i = i.to_i + 1
134
+ if i != argv.length
135
+ attribute << ","
136
+ end
137
+ end while i < argv.length
138
+
139
+
140
+ # Parsing conditional hashing argument.
141
+ condition = ""
142
+ condition << "( "
143
+ i = 0
144
+ begin
145
+ if "condition" != cond_argv.keys[i]
146
+ condition << cond_argv.keys[i]
147
+ condition << "="
148
+ condition << "\"#{cond_argv[cond_argv.keys[i]]}\""
149
+ condition << " "
150
+ i = i.to_i + 1
151
+ else
152
+ condition << "\"#{cond_argv[cond_argv.keys[i]]}\""
153
+ condition << " "
154
+ i = i.to_i + 1
155
+ end
156
+ end while i < cond_argv.length
157
+ condition << ")"
158
+
159
+ query = " UPDATE #{tab_name} SET #{attribute} WHERE #{condition} "
160
+ puts query
161
+ return query
162
+ end
163
+
164
+ # Parsing attribute for where clouse.
165
+
166
+ def generate_where tab_name, argv #, att_argv
167
+
168
+
169
+ # Parsing conditional hashing argument.
170
+ condition = ""
171
+ condition << "( "
172
+ i = 0
173
+ begin
174
+ if "condition" != argv.keys[i]
175
+ condition << argv.keys[i]
176
+ condition << "="
177
+ condition << "\"#{argv[argv.keys[i]]}\""
178
+ condition << " "
179
+ i = i.to_i + 1
180
+ else
181
+ condition << "\"#{argv[argv.keys[i]]}\""
182
+ condition << " "
183
+ i = i.to_i + 1
184
+ end
185
+ end while i < argv.length
186
+ condition << ")"
187
+
188
+
189
+ #if att_argv.length == 0
190
+ query = " SELECT * FROM #{tab_name} WHERE #{condition} "
191
+ puts query
192
+ return query
193
+ #else
194
+ #query = " SELECT #{att_argv * ","} FROM #{tab_name} WHERE #{condition} "
195
+ #puts query
196
+ #return query
197
+ #end
198
+ end
199
+
200
+ # Generating the index command.
201
+
202
+ def generate_index tab_name, argv
203
+ query = " CREATE INDEX #{argv[argv.keys[0]]} ON #{tab_name} (#{argv[argv.keys[1]]}) "
204
+ puts query
205
+ return query
206
+ end
207
+
208
+ # # Generating the index deletion command.
209
+ def generate_dindex tab_name,argv
210
+ query = " ALTER TABLE #{tab_name} DROP INDEX #{argv[argv.keys[0]]} "
211
+ puts query
212
+ return query
213
+ end
214
+
215
+ end
216
+
217
+ end