knjrbfw 0.0.49 → 0.0.50

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.49
1
+ 0.0.50
data/knjrbfw.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjrbfw}
8
- s.version = "0.0.49"
8
+ s.version = "0.0.50"
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-06-18}
12
+ s.date = %q{2012-06-25}
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 = [
data/lib/knj/arrayext.rb CHANGED
@@ -246,4 +246,84 @@ module Knj::ArrayExt
246
246
 
247
247
  return nil
248
248
  end
249
+
250
+ #Returns a powerset of the given array. Copied from 'http://mikeburnscoder.wordpress.com/2009/05/30/powerset-in-ruby-using-the-list-monad/'.
251
+ #===Examples
252
+ # ps = Knj::ArrayExt.powerset(:arr => [1, 2 , 3, 4])
253
+ # ps.length #=> 16
254
+ def self.powerset(args)
255
+ arr = args[:arr]
256
+ raise "No array was given." if !arr
257
+
258
+ if block_given?
259
+ if arr.length == 0
260
+ yield []
261
+ else
262
+ Knj::ArrayExt.powerset(:arr => arr[0..-2]) do |set|
263
+ yield set
264
+ yield set + [arr[-1]]
265
+ end
266
+ end
267
+
268
+ arr
269
+ else
270
+ Enumerator.new do |sets|
271
+ Knj::ArrayExt.powerset(:arr => arr) do |set|
272
+ sets << set
273
+ end
274
+ end
275
+ end
276
+ end
277
+
278
+ #Divides an array based on callback.
279
+ #===Examples
280
+ # arr = [1, 2, 3, 4, 6, 7, 8, 9, 15, 16, 17, 18]
281
+ # res = Knj::ArrayExt.divide(:arr => arr) do |a, b|
282
+ # if (b - a) > 1
283
+ # false
284
+ # else
285
+ # true
286
+ # end
287
+ # end
288
+ #
289
+ # res.length #=> 3
290
+ def self.divide(args)
291
+ prev_ele = args[:arr].shift
292
+ chunk = [prev_ele]
293
+ ret = [] if !args[:callback]
294
+
295
+ args[:arr].each do |ele|
296
+ if !chunk
297
+ chunk = [ele]
298
+ prev_ele = ele
299
+ next
300
+ end
301
+
302
+ if yield(prev_ele, ele)
303
+ chunk << ele
304
+ elsif callback = args[:callback]
305
+ callback.call(chunk)
306
+ chunk = nil
307
+ else
308
+ ret << chunk
309
+ chunk = nil
310
+ end
311
+
312
+ prev_ele = ele
313
+ end
314
+
315
+ if chunk and !chunk.empty?
316
+ if callback = args[:callback]
317
+ callback.call(chunk)
318
+ else
319
+ ret << chunk
320
+ end
321
+ end
322
+
323
+ if args[:callback]
324
+ return nil
325
+ else
326
+ return ret
327
+ end
328
+ end
249
329
  end
data/lib/knj/gtk2_tv.rb CHANGED
@@ -1,4 +1,8 @@
1
+ #This module contains various helper-methods for handeling stuff regarding treeviews.
1
2
  module Knj::Gtk2::Tv
3
+ #Initializes a treeview with a model and a number of columns. Returns a hash containing various data like the renderers.
4
+ #===Examples
5
+ # Knj::Gtk2::Tv.init(treeview, ["ID", "Name"])
2
6
  def self.init(tv, columns)
3
7
  ret = {
4
8
  :renderers => []
@@ -62,6 +66,9 @@ module Knj::Gtk2::Tv
62
66
  return ret
63
67
  end
64
68
 
69
+ #Appends data to the treeview.
70
+ #===Examples
71
+ # Knj::Gtk2::Tv.append(treeview, [1, "Kasper"])
65
72
  def self.append(tv, data)
66
73
  iter = tv.model.append
67
74
 
@@ -86,6 +93,9 @@ module Knj::Gtk2::Tv
86
93
  return {:iter => iter}
87
94
  end
88
95
 
96
+ #Gets the selected data from the treeview.
97
+ #===Examples
98
+ # Knj::Gtk2::Tv.sel(treeview) #=> [1, "Kasper"]
89
99
  def self.sel(tv)
90
100
  selected = tv.selection.selected_rows
91
101
 
@@ -218,15 +228,19 @@ module Knj::Gtk2::Tv
218
228
  end
219
229
  end
220
230
 
231
+ #Shortcuts on the actual treeview-objects.
221
232
  class Gtk::TreeView
233
+ #Shortcut to do Knj::Gtk2::Tv.sel(treeview)
222
234
  def sel
223
235
  return Knj::Gtk2::Tv.sel(self)
224
236
  end
225
237
 
238
+ #Shortcut to do Knj::Gtk2.append(treeview, [data1, data2])
226
239
  def append(data)
227
240
  return Knj::Gtk2::Tv.append(self, data)
228
241
  end
229
242
 
243
+ #Shortcut to do Knj::Gtk2.init(treeview, columns_array)
230
244
  def init(cols)
231
245
  return Knj::Gtk2::Tv.init(self, cols)
232
246
  end
@@ -310,7 +310,8 @@ class Knj::Objects
310
310
  end
311
311
 
312
312
  found = true
313
- elsif args.key?(:cols_date) and match = key.match(/^(.+)_(day|week|month|year|from|to|below|above)$/) and args[:cols_date].index(match[1]) != nil
313
+ elsif args.key?(:cols_date) and match = key.match(/^(.+)_(day|week|month|year|from|to|below|above)(|_(not))$/) and args[:cols_date].index(match[1]) != nil
314
+ not_v = match[4]
314
315
  val = Knj::Datet.in(val) if val.is_a?(Time)
315
316
 
316
317
  if match[2] == "day"
@@ -325,19 +326,19 @@ class Knj::Objects
325
326
  sql_where << " OR "
326
327
  end
327
328
 
328
- sql_where << "DATE_FORMAT(#{table}`#{db.esc_col(match[1])}`, '%d %m %Y') = DATE_FORMAT('#{db.esc(realval.dbstr)}', '%d %m %Y')"
329
+ sql_where << " AND #{db.sqlspecs.strftime("%d %m %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%d %m %Y", "'#{db.esc(realval.dbstr)}'")}"
329
330
  end
330
331
 
331
332
  sql_where << ")"
332
333
  else
333
- sql_where << " AND DATE_FORMAT(#{table}`#{db.esc_col(match[1])}`, '%d %m %Y') = DATE_FORMAT('#{db.esc(val.dbstr)}', '%d %m %Y')"
334
+ sql_where << " AND #{db.sqlspecs.strftime("%d %m %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%d %m %Y", "'#{db.esc(val.dbstr)}'")}"
334
335
  end
335
336
  elsif match[2] == "week"
336
- sql_where << " AND #{db.sqlspecs.strftime("%W %Y", "#{table}`#{db.esc_col(match[1])}`")} = #{db.sqlspecs.strftime("%W %Y", "'#{db.esc(val.dbstr)}'")}"
337
+ sql_where << " AND #{db.sqlspecs.strftime("%W %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%W %Y", "'#{db.esc(val.dbstr)}'")}"
337
338
  elsif match[2] == "month"
338
- sql_where << " AND #{db.sqlspecs.strftime("%m %Y", "#{table}`#{db.esc_col(match[1])}`")} = #{db.sqlspecs.strftime("%m %Y", "'#{db.esc(val.dbstr)}'")}"
339
+ sql_where << " AND #{db.sqlspecs.strftime("%m %Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%m %Y", "'#{db.esc(val.dbstr)}'")}"
339
340
  elsif match[2] == "year"
340
- sql_where << " AND DATE_FORMAT(#{table}`#{db.esc_col(match[1])}`, '%Y') = DATE_FORMAT('#{db.esc(val.dbstr)}', '%Y')"
341
+ sql_where << " AND #{db.sqlspecs.strftime("%Y", "#{table}`#{db.esc_col(match[1])}`")} #{self.not(not_v, "!")}= #{db.sqlspecs.strftime("%Y", "'#{db.esc(val.dbstr)}'")}"
341
342
  elsif match[2] == "from" or match[2] == "above"
342
343
  sql_where << " AND #{table}`#{db.esc_col(match[1])}` >= '#{db.esc(val.dbstr)}'"
343
344
  elsif match[2] == "to" or match[2] == "below"
@@ -481,4 +482,12 @@ class Knj::Objects
481
482
 
482
483
  return datarow_argument
483
484
  end
485
+
486
+ def not(not_v, val)
487
+ if not_v == "not" or not_v == "not_"
488
+ return val
489
+ end
490
+
491
+ return ""
492
+ end
484
493
  end
@@ -1,6 +1,34 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "ArrayExt" do
4
+ it "should be able to do powersets" do
5
+ ps = Knj::ArrayExt.powerset(:arr => [1, 2, 3, 4]).to_a
6
+ raise "Expected length of 16 but it wasnt: #{ps.length}" if ps.length != 16
7
+
8
+ ite = 0
9
+ Knj::ArrayExt.powerset(:arr => [1, 2, 3, 4]) do |arr|
10
+ ite += 1
11
+ end
12
+
13
+ raise "Expected block to be executed 16 times but it wasnt: #{ite}" if ite != 16
14
+ end
15
+
16
+ it "should be able to divide arrays" do
17
+ arr = [1, 2, 3, 4, 6, 7, 8, 9, 15, 16, 17, 18]
18
+ res = Knj::ArrayExt.divide(:arr => arr) do |a, b|
19
+ if (b - a) > 1
20
+ false
21
+ else
22
+ true
23
+ end
24
+ end
25
+
26
+ raise "Expected length of 3 but it wasnt: #{res.length}" if res.length != 3
27
+ raise "Expected length of 4 but it wasnt: #{res[0].length}" if res[0].length != 4
28
+ raise "Expected length of 3 but it wasnt: #{res[1].length}" if res[1].length != 3
29
+ raise "Expected length of 3 but it wasnt: #{res[2].length}" if res[2].length != 3
30
+ end
31
+
4
32
  it "should be able to make ago-strings" do
5
33
  arr = [1, 2]
6
34
  Knj::ArrayExt.force_no_cols(:arr => arr, :no => 1)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: knjrbfw
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.49
5
+ version: 0.0.50
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-06-18 00:00:00 +02:00
13
+ date: 2012-06-25 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -379,7 +379,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
379
379
  requirements:
380
380
  - - ">="
381
381
  - !ruby/object:Gem::Version
382
- hash: 4237675880439105833
382
+ hash: 499014487481488360
383
383
  segments:
384
384
  - 0
385
385
  version: "0"