knjrbfw 0.0.30 → 0.0.31

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.30
1
+ 0.0.31
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjrbfw}
8
- s.version = "0.0.30"
8
+ s.version = "0.0.31"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-05-03}
12
+ s.date = %q{2012-05-06}
13
13
  s.description = %q{Including stuff for HTTP, SSH and much more.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
@@ -1,4 +1,19 @@
1
+ #This class holds various methods to generate commands for command-line-purpose.
1
2
  class Knj::Cmd_gen
3
+ #Generates rsync commands as strings.
4
+ #===Examples
5
+ # Knj::Cmd_gen.rsync(
6
+ # :bin => "/usr/bin/rsync",
7
+ # :verbose => 2,
8
+ # :ssh => true,
9
+ # :port => 10022,
10
+ # :delete => true,
11
+ # :exclude => "cache",
12
+ # :user => "username",
13
+ # :host => "mydomain.com",
14
+ # :dir_host => "/home/username/sync_path",
15
+ # :dir_local => "/home/otheruser/sync_path"
16
+ # ) #=> <String>
2
17
  def self.rsync(args)
3
18
  cmd = ""
4
19
 
@@ -39,6 +54,18 @@ class Knj::Cmd_gen
39
54
  return cmd
40
55
  end
41
56
 
57
+ #Generates tar commands.
58
+ #===Examples
59
+ # Knj::Cmd_gen.tar(
60
+ # :bin => "/usr/bin/tar",
61
+ # :gzip => true,
62
+ # :extract => false,
63
+ # :file => true,
64
+ # :create => true,
65
+ # :verbose => 1,
66
+ # :archive_path => "~/myarchive.tar.gz",
67
+ # :paths => ["~/mylib1", "~/mylib2", "~/mylib3"]
68
+ # ) #=> <String>
42
69
  def self.tar(args)
43
70
  cmd = ""
44
71
 
@@ -1,4 +1,9 @@
1
+ #This class can help you parse results from command-line commands.
1
2
  class Knj::Cmd_parser
3
+ #Parses the results of "ls -l".
4
+ #===Examples
5
+ # str = %x[ls -l]
6
+ # Knj::Cmd_parser.lsl(str) #=> <Array> holding a lot of info about the various listed files.
2
7
  def self.lsl(str, args = {})
3
8
  ret = []
4
9
 
@@ -1,12 +1,20 @@
1
1
  #This class can manipulate the CPU behavior through "cpufreq".
2
2
  class Knj::Cpufreq
3
+ #Data that is used to identify the CPU controlled by this object.
3
4
  attr_reader :data
4
5
 
6
+ #Useually called through "Knj::Cpufreq.list".
5
7
  def initialize(data)
6
8
  @data = data
7
9
  @allowed_govs = ["performance", "ondemand", "powersafe", "conservative"]
8
10
  end
9
11
 
12
+ #Returns a list of CPUs.
13
+ #===Examples
14
+ # list = Knj::Cpufreq.list
15
+ # list.each do |cpufreq|
16
+ # cpufreq.governor = "performance"
17
+ # end
10
18
  def self.list
11
19
  ret = []
12
20
  cont = File.read("/proc/cpuinfo")
@@ -28,6 +36,9 @@ class Knj::Cpufreq
28
36
  return ret
29
37
  end
30
38
 
39
+ #Sets the governor.
40
+ #===Examples
41
+ # cpufreq.governor = "performance"
31
42
  def governor=(newgov)
32
43
  raise "Governor not found." if @allowed_govs.index(newgov) == nil
33
44
 
@@ -1,4 +1,8 @@
1
+ #Contains various methods for handeling CSV-stuff.
1
2
  class Knj::Csv
3
+ #Converts a given array to a CSV-string.
4
+ #===Examples
5
+ # str = Knj::Csv.arr_to_csv([1, 2, 3], ";", "'") #=> "'1';'2';'3'\n"
2
6
  def self.arr_to_csv(arr, del, encl)
3
7
  raise "No delimiter given." if !del
4
8
  raise "No enclosure given." if !encl
@@ -1,19 +1,52 @@
1
+ #This class helps create models in a framework with Knj::Db and Knj::Objects.
2
+ #===Examples
3
+ # db = Knj::Db.new(:type => "sqlite3", :path => "somepath.sqlite3")
4
+ # ob = Knj::Objects.new(:db => db, :datarow => true, :path => "path_of_model_class_files")
5
+ # user = ob.get(:User, 1) #=> <Models::User> that extends <Knj::Datarow>
1
6
  class Knj::Datarow
2
- attr_reader :data, :ob, :db
7
+ #Returns the data-hash that contains all the data from the database.
8
+ attr_reader :data
9
+
10
+ #Returns the Knj::Objects which handels this model.
11
+ attr_reader :ob
12
+
13
+ #Returns the Knj::Db which handels this model.
14
+ attr_reader :db
3
15
 
4
16
  #This is used by 'Knj::Objects' to find out what data is required for this class. Returns the array that tells about required data.
17
+ #===Examples
18
+ #When adding a new user, this can fail if the ':group_id' is not given, or the ':group_id' doesnt refer to a valid group-row in the db.
19
+ # class Models::User < Knj::Datarow
20
+ # has_one [
21
+ # {:class => :Group, :col => :group_id, :method => :group, :required => true}
22
+ # ]
23
+ # end
5
24
  def self.required_data
6
25
  @required_data = [] if !@required_data
7
26
  return @required_data
8
27
  end
9
28
 
10
29
  #This is used by 'Knj::Objects' to find out what other objects this class depends on. Returns the array that tells about depending data.
30
+ #===Examples
31
+ #This will tell Knj::Objects that files depends on users. It can prevent the user from being deleted, if any files depend on it.
32
+ # class Models::User < Knj::Datarow
33
+ # has_many [
34
+ # {:class => :File, :col => :user_id, :method => :files, :depends => true}
35
+ # ]
36
+ # end
11
37
  def self.depending_data
12
38
  @depending_data = [] if !@depending_data
13
39
  return @depending_data
14
40
  end
15
41
 
16
42
  #This is used by 'Knj::Objects' to find out which other objects should be deleted when an object of this class is deleted automatically. Returns the array that tells about autodelete data.
43
+ #===Examples
44
+ #This will trigger Knj::Objects to automatically delete all the users pictures, when deleting the current user.
45
+ # class Models::User < Knj::Datarow
46
+ # has_many [
47
+ # {:class => :Picture, :col => :user_id, :method => :pictures, :autodelete => true}
48
+ # ]
49
+ # end
17
50
  def self.autodelete_data
18
51
  @autodelete_data = [] if !@autodelete_data
19
52
  return @autodelete_data
@@ -25,17 +58,30 @@ class Knj::Datarow
25
58
  end
26
59
 
27
60
  #This helps various parts of the framework determine if this is a datarow class without requiring it.
61
+ #===Examples
62
+ # print "This is a knj-object." if obj.respond_to?("is_knj?")
28
63
  def is_knj?
29
64
  return true
30
65
  end
31
66
 
32
67
  #This tests if a certain string is a date-null-stamp.
68
+ #===Examples
69
+ # time_str = dbrow[:date]
70
+ # print "No valid date on the row." if Knj::Datarow.is_nullstamp?(time_str)
33
71
  def self.is_nullstamp?(stamp)
34
72
  return true if !stamp or stamp == "0000-00-00 00:00:00" or stamp == "0000-00-00"
35
73
  return false
36
74
  end
37
75
 
38
76
  #This is used to define datarows that this object can have a lot of.
77
+ #===Examples
78
+ #This will define the method "pictures" on 'Models::User' that will return all pictures for the users and take possible Objects-sql-arguments. It will also enabling joining pictures when doing Objects-sql-lookups.
79
+ # class Models::User < Knj::Datarow
80
+ # has_many [
81
+ # [:Picture, :user_id, :pictures],
82
+ # {:class => :File, :col => :user_id, :method => :files}
83
+ # ]
84
+ # end
39
85
  def self.has_many(arr)
40
86
  arr.each do |val|
41
87
  if val.is_a?(Array)
@@ -109,6 +155,16 @@ class Knj::Datarow
109
155
  end
110
156
 
111
157
  #This define is this object has one element of another datarow-class. It define various methods and joins based on that.
158
+ #===Examples
159
+ # class Models::User < Knj::Datarow
160
+ # has_one [
161
+ # #Defines the method 'group', which returns a 'Group'-object by the column 'group_id'.
162
+ # :Group,
163
+ #
164
+ # #Defines the method 'type', which returns a 'Type'-object by the column 'type_id'.
165
+ # {:class => :Type, :col => :type_id, :method => :type}
166
+ # ]
167
+ # end
112
168
  def self.has_one(arr)
113
169
  arr.each do |val|
114
170
  methodname = nil
@@ -162,6 +218,16 @@ class Knj::Datarow
162
218
  end
163
219
 
164
220
  #This method initializes joins, sets methods to update translations and makes the translations automatically be deleted when the object is deleted.
221
+ #===Examples
222
+ # class Models::Article < Knj::Datarow
223
+ # #Defines methods such as: 'title', 'title=', 'content', 'content='. When used with Knjappserver these methods will change what they return and set based on the current language of the session.
224
+ # has_translation [:title, :content]
225
+ # end
226
+ #
227
+ # article = ob.get(:Article, 1)
228
+ # print "The title in the current language is: '#{article.title}'."
229
+ #
230
+ # article.title = 'Title in english if the language is english'
165
231
  def self.has_translation(arr)
166
232
  @translations = [] if !@translations
167
233
 
@@ -194,32 +260,46 @@ class Knj::Datarow
194
260
  return @translations
195
261
  end
196
262
 
263
+ #Returns data about joined tables for this class.
197
264
  def self.joined_tables(hash)
198
265
  @columns_joined_tables = {} if !@columns_joined_tables
199
266
  @columns_joined_tables.merge!(hash)
200
267
  end
201
268
 
202
269
  #Returns the table-name that should be used for this datarow.
270
+ #===Examples
271
+ # db.query("SELECT * FROM `#{Models::User.table}` WHERE username = 'John Doe'") do |data|
272
+ # print data[:id]
273
+ # end
203
274
  def self.table
204
275
  return @table if @table
205
276
  return self.name.split("::").last
206
277
  end
207
278
 
208
279
  #This can be used to manually set the table-name. Useful when meta-programming classes that extends the datarow-class.
280
+ #===Examples
281
+ # Models::User.table = "prefix_User"
209
282
  def self.table=(newtable)
210
283
  @table = newtable
211
284
  @columns_sqlhelper_args[:table] = @table if @columns_sqlhelper_args.is_a?(Hash)
212
285
  end
213
286
 
214
287
  #Returns the class-name but without having to call the class-table-method. To make code look shorter.
288
+ #===Examples
289
+ # user = ob.get_by(:User, {:username => 'John Doe'})
290
+ # db.query("SELECT * FROM `#{user.table}` WHERE username = 'John Doe'") do |data|
291
+ # print data[:id]
292
+ # end
215
293
  def table
216
294
  return self.class.table
217
295
  end
218
296
 
297
+ #Returns various data for the objects-sql-helper. This can be used to view various informations about the columns and more.
219
298
  def self.columns_sqlhelper_args
220
299
  return @columns_sqlhelper_args
221
300
  end
222
301
 
302
+ #Called by Knj::Objects to initialize the model and load column-data on-the-fly.
223
303
  def self.load_columns(d)
224
304
  @ob = d.ob if !@ob
225
305
 
@@ -294,89 +374,14 @@ class Knj::Datarow
294
374
  self.init_class(d) if self.respond_to?(:init_class)
295
375
  end
296
376
 
297
- #Various methods to define methods based on the columns for the datarow.
298
- def self.define_translation_methods(args)
299
- define_method("#{args[:val_dc]}=") do |newtransval|
300
- _kas.trans_set(self, {
301
- args[:val] => newtransval
302
- })
303
- end
304
-
305
- define_method("#{args[:val_dc]}") do
306
- return _kas.trans(self, args[:val])
307
- end
308
-
309
- define_method("#{args[:val_dc]}_html") do
310
- str = _kas.trans(self, args[:val])
311
- if str.to_s.strip.length <= 0
312
- return "[no translation for #{args[:val]}]"
313
- end
314
-
315
- return str
316
- end
317
- end
318
-
319
- def self.define_bool_methods(args)
320
- #Spawns a method on the class which returns true if the data is 1.
321
- method_name = "#{args[:col_name]}?".to_sym
322
-
323
- if args[:inst_methods].index(method_name) == nil
324
- define_method(method_name) do
325
- return true if self[args[:col_name].to_sym].to_s == "1"
326
- return false
327
- end
328
- end
329
- end
330
-
331
- def self.define_date_methods(args)
332
- method_name = "#{args[:col_name]}_str".to_sym
333
- if args[:inst_methods].index(method_name) == nil
334
- define_method(method_name) do |*method_args|
335
- if Knj::Datet.is_nullstamp?(self[args[:col_name].to_sym])
336
- return @ob.events.call(:no_date, self.class.name)
337
- end
338
-
339
- return Knj::Datet.in(self[args[:col_name].to_sym]).out(*method_args)
340
- end
341
- end
342
-
343
- method_name = "#{args[:col_name]}".to_sym
344
- if args[:inst_methods].index(method_name) == nil
345
- define_method(method_name) do |*method_args|
346
- return false if Knj::Datet.is_nullstamp?(self[args[:col_name].to_sym])
347
- return Knj::Datet.in(self[args[:col_name].to_sym])
348
- end
349
- end
350
- end
351
-
352
- def self.define_numeric_methods(args)
353
- method_name = "#{args[:col_name]}_format"
354
- if args[:inst_methods].index(method_name) == nil
355
- define_method(method_name) do |*method_args|
356
- return Knj::Locales.number_out(self[args[:col_name].to_sym], *method_args)
357
- end
358
- end
359
- end
360
-
361
- def self.define_text_methods(args)
362
- method_name = "by_#{args[:col_name]}".to_sym
363
- if args[:inst_methods].index(method_name) == nil and RUBY_VERSION.to_s.slice(0, 3) != "1.8"
364
- define_singleton_method(method_name) do |arg|
365
- return d.ob.get_by(self.table, {args[:col_name].to_s => arg})
366
- end
367
- end
368
- end
369
-
370
- def self.define_time_methods(args)
371
- method_name = "#{args[:col_name]}_dbt"
372
- if args[:inst_methods].index(method_name) == nil
373
- define_method(method_name) do
374
- return Knj::Db::Dbtime.new(self[args[:col_name].to_sym])
375
- end
376
- end
377
- end
378
-
379
377
  #This method helps returning objects and supports various arguments. It should be called by Object#list.
378
+ #===Examples
379
+ # ob.list(:User, {"username_lower" => "john doe"}) do |user|
380
+ # print user.id
381
+ # end
382
+ #
383
+ # array = ob.list(:User, {"id" => 1})
384
+ # print array.length
380
385
  def self.list(d, &block)
381
386
  ec_col = d.db.enc_col
382
387
  ec_table = d.db.enc_table
@@ -460,20 +465,24 @@ class Knj::Datarow
460
465
  return d.ob.list_bysql(self.classname, sql, qargs, &block)
461
466
  end
462
467
 
468
+ #Helps call 'sqlhelper' on Knj::Objects to generate SQL-strings.
463
469
  def self.list_helper(d)
464
470
  self.load_columns(d) if !@columns_sqlhelper_args
465
471
  @columns_sqlhelper_args[:table] = @table if @table
466
472
  return d.ob.sqlhelper(d.args, @columns_sqlhelper_args)
467
473
  end
468
474
 
475
+ #Returns the classname of the object without any subclasses.
469
476
  def self.classname
470
477
  return @classname
471
478
  end
472
479
 
480
+ #Sets the classname to something specific in order to hack the behaviour.
473
481
  def self.classname=(newclassname)
474
482
  @classname = newclassname
475
483
  end
476
484
 
485
+ #Initializes the object. This should be called from Knj::Objects.
477
486
  def initialize(d)
478
487
  @ob = d.ob
479
488
  @db = d.ob.db
@@ -490,6 +499,9 @@ class Knj::Datarow
490
499
  end
491
500
 
492
501
  #Reloads the data from the database.
502
+ # old_username = user[:username]
503
+ # user.reload
504
+ # print "The username changed in the database!" if user[:username] != old_username
493
505
  def reload
494
506
  data = @db.single(self.table, {:id => @data[:id]})
495
507
  if !data
@@ -500,6 +512,8 @@ class Knj::Datarow
500
512
  end
501
513
 
502
514
  #Writes/updates new data for the object.
515
+ #===Examples
516
+ # user.update(:username => 'New username', :date_changed => Time.now)
503
517
  def update(newdata)
504
518
  @db.update(self.table, newdata, {:id => @data[:id]})
505
519
  self.reload
@@ -516,15 +530,11 @@ class Knj::Datarow
516
530
  @data = nil
517
531
  end
518
532
 
519
- #Alias for key?
520
- def has_key?(key)
521
- return @data.key?(key.to_sym)
522
- end
523
-
524
533
  #Returns true if that key exists on the object.
525
534
  def key?(key)
526
535
  return @data.key?(key.to_sym)
527
536
  end
537
+ alias has_key? key?
528
538
 
529
539
  #Returns true if the object has been deleted.
530
540
  def deleted?
@@ -533,6 +543,9 @@ class Knj::Datarow
533
543
  end
534
544
 
535
545
  #Returns a specific data from the object by key.
546
+ # print "Username: #{user[:username]}\n"
547
+ # print "ID: #{user[:id]}\n"
548
+ # print "ID again: #{user.id}\n"
536
549
  def [](key)
537
550
  raise "Key was not a symbol: '#{key.class.name}'." if !key.is_a?(Symbol)
538
551
  raise "No data was loaded on the object? Maybe you are trying to call a deleted object?" if !@data
@@ -541,6 +554,8 @@ class Knj::Datarow
541
554
  end
542
555
 
543
556
  #Writes/updates a keys value on the object.
557
+ # user = ob.get_by(:User, {"username" => "John Doe"})
558
+ # user[:username] = 'New username'
544
559
  def []=(key, value)
545
560
  self.update(key.to_sym => value)
546
561
  self.reload
@@ -582,10 +597,107 @@ class Knj::Datarow
582
597
  return name_str
583
598
  end
584
599
 
585
- alias :title :name
600
+ alias title name
586
601
 
587
602
  #Loops through the data on the object.
603
+ #===Examples
604
+ # user = ob.get(:User, 1)
605
+ # user.each do |key, val|
606
+ # print "#{key}: #{val}\n" #=> username: John Doe
607
+ # end
588
608
  def each(&args)
589
609
  return @data.each(&args)
590
610
  end
611
+
612
+ private
613
+
614
+ #Various methods to define methods based on the columns for the datarow.
615
+ def self.define_translation_methods(args)
616
+ define_method("#{args[:val_dc]}=") do |newtransval|
617
+ _kas.trans_set(self, {
618
+ args[:val] => newtransval
619
+ })
620
+ end
621
+
622
+ define_method("#{args[:val_dc]}") do
623
+ return _kas.trans(self, args[:val])
624
+ end
625
+
626
+ define_method("#{args[:val_dc]}_html") do
627
+ str = _kas.trans(self, args[:val])
628
+ if str.to_s.strip.length <= 0
629
+ return "[no translation for #{args[:val]}]"
630
+ end
631
+
632
+ return str
633
+ end
634
+ end
635
+
636
+ #Defines the boolean-methods based on enum-columns.
637
+ def self.define_bool_methods(args)
638
+ #Spawns a method on the class which returns true if the data is 1.
639
+ method_name = "#{args[:col_name]}?".to_sym
640
+
641
+ if args[:inst_methods].index(method_name) == nil
642
+ define_method(method_name) do
643
+ return true if self[args[:col_name].to_sym].to_s == "1"
644
+ return false
645
+ end
646
+ end
647
+ end
648
+
649
+ #Defines date- and time-columns based on datetime- and date-columns.
650
+ def self.define_date_methods(args)
651
+ method_name = "#{args[:col_name]}_str".to_sym
652
+ if args[:inst_methods].index(method_name) == nil
653
+ define_method(method_name) do |*method_args|
654
+ if Knj::Datet.is_nullstamp?(self[args[:col_name].to_sym])
655
+ return @ob.events.call(:no_date, self.class.name)
656
+ end
657
+
658
+ return Knj::Datet.in(self[args[:col_name].to_sym]).out(*method_args)
659
+ end
660
+ end
661
+
662
+ method_name = "#{args[:col_name]}".to_sym
663
+ if args[:inst_methods].index(method_name) == nil
664
+ define_method(method_name) do |*method_args|
665
+ return false if Knj::Datet.is_nullstamp?(self[args[:col_name].to_sym])
666
+ return Knj::Datet.in(self[args[:col_name].to_sym])
667
+ end
668
+ end
669
+ end
670
+
671
+ #Define various methods based on integer-columns.
672
+ def self.define_numeric_methods(args)
673
+ method_name = "#{args[:col_name]}_format"
674
+ if args[:inst_methods].index(method_name) == nil
675
+ define_method(method_name) do |*method_args|
676
+ return Knj::Locales.number_out(self[args[:col_name].to_sym], *method_args)
677
+ end
678
+ end
679
+ end
680
+
681
+ #Define methods to look up objects directly.
682
+ #===Examples
683
+ # user = Models::User.by_username('John Doe')
684
+ # print user.id
685
+ def self.define_text_methods(args)
686
+ method_name = "by_#{args[:col_name]}".to_sym
687
+ if args[:inst_methods].index(method_name) == nil and RUBY_VERSION.to_s.slice(0, 3) != "1.8"
688
+ define_singleton_method(method_name) do |arg|
689
+ return d.ob.get_by(self.table, {args[:col_name].to_s => arg})
690
+ end
691
+ end
692
+ end
693
+
694
+ #Defines dbtime-methods based on time-columns.
695
+ def self.define_time_methods(args)
696
+ method_name = "#{args[:col_name]}_dbt"
697
+ if args[:inst_methods].index(method_name) == nil
698
+ define_method(method_name) do
699
+ return Knj::Db::Dbtime.new(self[args[:col_name].to_sym])
700
+ end
701
+ end
702
+ end
591
703
  end