knjrbfw 0.0.103 → 0.0.104

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -18,4 +18,5 @@ group :development do
18
18
  gem "sqlite3" if RUBY_ENGINE != "jruby"
19
19
  gem "rmagick" if RUBY_ENGINE != "jruby"
20
20
  gem "rmagick4j" if RUBY_ENGINE == "jruby"
21
+ gem "array_enumerator"
21
22
  end
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- datet (0.0.21)
5
- diff-lcs (1.2.1)
4
+ array_enumerator (0.0.3)
5
+ datet (0.0.25)
6
+ diff-lcs (1.2.2)
6
7
  git (1.2.5)
7
8
  http2 (0.0.13)
8
9
  jeweler (1.8.4)
@@ -11,32 +12,35 @@ GEM
11
12
  rake
12
13
  rdoc
13
14
  json (1.7.7)
14
- php4r (0.0.3)
15
+ php4r (0.0.4)
15
16
  datet
16
17
  http2
17
- rake (10.0.3)
18
- rdoc (4.0.0)
18
+ string-strtr
19
+ rake (10.0.4)
20
+ rdoc (4.0.1)
19
21
  json (~> 1.4)
20
22
  rmagick (2.13.2)
21
23
  rspec (2.13.0)
22
24
  rspec-core (~> 2.13.0)
23
25
  rspec-expectations (~> 2.13.0)
24
26
  rspec-mocks (~> 2.13.0)
25
- rspec-core (2.13.0)
27
+ rspec-core (2.13.1)
26
28
  rspec-expectations (2.13.0)
27
29
  diff-lcs (>= 1.1.3, < 2.0)
28
- rspec-mocks (2.13.0)
30
+ rspec-mocks (2.13.1)
29
31
  ruby_process (0.0.8)
30
32
  tsafe
31
33
  wref
32
34
  sqlite3 (1.3.7)
35
+ string-strtr (0.0.3)
33
36
  tsafe (0.0.11)
34
- wref (0.0.5)
37
+ wref (0.0.6)
35
38
 
36
39
  PLATFORMS
37
40
  ruby
38
41
 
39
42
  DEPENDENCIES
43
+ array_enumerator
40
44
  bundler
41
45
  datet
42
46
  http2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.103
1
+ 0.0.104
data/knjrbfw.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "knjrbfw"
8
- s.version = "0.0.103"
8
+ s.version = "0.0.104"
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 = "2013-03-04"
12
+ s.date = "2013-04-11"
13
13
  s.description = "Including stuff for HTTP, SSH and much more."
14
14
  s.email = "k@spernj.org"
15
15
  s.extra_rdoc_files = [
@@ -284,6 +284,7 @@ Gem::Specification.new do |s|
284
284
  s.add_development_dependency(%q<jeweler>, [">= 0"])
285
285
  s.add_development_dependency(%q<sqlite3>, [">= 0"])
286
286
  s.add_development_dependency(%q<rmagick>, [">= 0"])
287
+ s.add_development_dependency(%q<array_enumerator>, [">= 0"])
287
288
  else
288
289
  s.add_dependency(%q<wref>, [">= 0"])
289
290
  s.add_dependency(%q<tsafe>, [">= 0"])
@@ -296,6 +297,7 @@ Gem::Specification.new do |s|
296
297
  s.add_dependency(%q<jeweler>, [">= 0"])
297
298
  s.add_dependency(%q<sqlite3>, [">= 0"])
298
299
  s.add_dependency(%q<rmagick>, [">= 0"])
300
+ s.add_dependency(%q<array_enumerator>, [">= 0"])
299
301
  end
300
302
  else
301
303
  s.add_dependency(%q<wref>, [">= 0"])
@@ -309,6 +311,7 @@ Gem::Specification.new do |s|
309
311
  s.add_dependency(%q<jeweler>, [">= 0"])
310
312
  s.add_dependency(%q<sqlite3>, [">= 0"])
311
313
  s.add_dependency(%q<rmagick>, [">= 0"])
314
+ s.add_dependency(%q<array_enumerator>, [">= 0"])
312
315
  end
313
316
  end
314
317
 
@@ -332,7 +332,7 @@ class KnjDB_mysql
332
332
  sql << ","
333
333
  end
334
334
 
335
- sql << "'#{self.escape(val)}'"
335
+ sql << @knjdb.sqlval(val)
336
336
  end
337
337
  else
338
338
  hash.each do |key, val|
@@ -342,7 +342,7 @@ class KnjDB_mysql
342
342
  sql << ","
343
343
  end
344
344
 
345
- sql << "'#{self.escape(val)}'"
345
+ sql << @knjdb.sqlval(val)
346
346
  end
347
347
  end
348
348
  end
@@ -113,6 +113,15 @@ class KnjDB_sqlite3::Tables::Table
113
113
  return @data[:maxlength]
114
114
  end
115
115
 
116
+ def reload
117
+ @data = @db.select("sqlite_master", {"type" => "table", "name" => self.name}, {"orderby" => "name"}).fetch
118
+ end
119
+
120
+ def rows_count
121
+ data = @db.q("SELECT COUNT(*) AS count FROM `#{self.name}`").fetch
122
+ return data[:count].to_i
123
+ end
124
+
116
125
  #Drops the table from the database.
117
126
  def drop
118
127
  raise "Cant drop native table: '#{self.name}'." if self.native?
@@ -264,7 +264,7 @@ class Knj::Db
264
264
  sql << "#{@sep_col}#{key}#{@sep_col}"
265
265
  end
266
266
 
267
- sql << ") VALUES ("
267
+ sql << ") VALUES ("
268
268
 
269
269
  first = true
270
270
  arr_insert.each do |key, value|
@@ -274,7 +274,7 @@ class Knj::Db
274
274
  sql << ", "
275
275
  end
276
276
 
277
- sql << "#{@sep_val}#{@esc_driver.escape(value)}#{@sep_val}"
277
+ sql << self.sqlval(value)
278
278
  end
279
279
 
280
280
  sql << ")"
@@ -289,6 +289,23 @@ class Knj::Db
289
289
  end
290
290
  end
291
291
 
292
+ #Returns the correct SQL-value for the given value. If it is a number, then just the raw number as a string will be returned. nil's will be NULL and strings will have quotes and will be escaped.
293
+ def sqlval(val)
294
+ return @conn.sqlval(val) if @conn.respond_to?(:sqlval)
295
+
296
+ if val.is_a?(Fixnum) or val.is_a?(Integer)
297
+ return val.to_s
298
+ elsif val == nil
299
+ return "NULL"
300
+ elsif val.is_a?(Date)
301
+ return "#{@sep_val}#{Datet.in(val).dbstr(:time => false)}#{@sep_val}"
302
+ elsif val.is_a?(Time) or val.is_a?(DateTime)
303
+ return "#{@sep_val}#{Datet.in(val).dbstr}#{@sep_val}"
304
+ else
305
+ return "#{@sep_val}#{self.escape(val)}#{@sep_val}"
306
+ end
307
+ end
308
+
292
309
  #Simply and optimal insert multiple rows into a table in a single query. Uses the drivers functionality if supported or inserts each row manually.
293
310
  #
294
311
  #===Examples
@@ -362,6 +379,17 @@ class Knj::Db
362
379
  end
363
380
  end
364
381
 
382
+ #Checks if a given selector exists. If it does, updates it to match data. If not inserts the row.
383
+ def upsert(table, selector, data)
384
+ row = self.select(table, selector, "limit" => 1).fetch
385
+
386
+ if row
387
+ self.update(table, data, row)
388
+ else
389
+ self.insert(table, selector.merge(data))
390
+ end
391
+ end
392
+
365
393
  #Makes a select from the given arguments: table-name, where-terms and other arguments as limits and orders. Also takes a block to avoid raping of memory.
366
394
  def select(tablename, arr_terms = nil, args = nil, &block)
367
395
  #Set up vars.
data/lib/knj/os.rb CHANGED
@@ -54,17 +54,18 @@ module Knj::Os
54
54
  # print "I like it better now." if Knj::Os.os == "linux"
55
55
  def self.os
56
56
  if ENV["OS"]
57
- teststring = ENV["OS"].to_s
57
+ teststring = ENV["OS"].to_s.downcase
58
58
  elsif RUBY_PLATFORM
59
- teststring = RUBY_PLATFORM.to_s
59
+ teststring = RUBY_PLATFORM.to_s.downcase
60
60
  end
61
61
 
62
- if teststring.downcase.index("windows") != nil
62
+ if teststring.include?("windows")
63
63
  return "windows"
64
- elsif teststring.downcase.index("linux") != nil
64
+ elsif teststring.include?("linux")
65
65
  return "linux"
66
+ elsif teststring.include?("darwin")
66
67
  else
67
- raise "Could not figure out OS."
68
+ raise "Could not figure out OS: '#{teststring}'."
68
69
  end
69
70
  end
70
71
 
@@ -221,6 +222,11 @@ module Knj::Os
221
222
  def self.executed_executable
222
223
  return ENV["rvm_ruby_string"] if !ENV["rvm_ruby_string"].to_s.empty?
223
224
 
225
+ if ENV["MY_RUBY_HOME"]
226
+ ruby_bin_path = "#{ENV["MY_RUBY_HOME"]}/bin/ruby"
227
+ return ruby_bin_path if File.exists?(ruby_bin_path)
228
+ end
229
+
224
230
  #Try to look the executeable up by command.
225
231
  if self.os == "linux"
226
232
  unix_proc = Knj::Unix_proc.find_self
@@ -235,6 +241,8 @@ module Knj::Os
235
241
  end
236
242
  end
237
243
 
244
+ puts ENV.to_hash
245
+
238
246
  raise "Could not figure out the executed executable."
239
247
  end
240
248
  end
data/spec/amixer_spec.rb CHANGED
@@ -2,24 +2,29 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Amixer" do
4
4
  it "should load by using autoload" do
5
- require "knjrbfw"
6
-
7
- $amixer = Knj::Amixer.new
5
+ if Knj::Os.os == "linux"
6
+ require "knjrbfw"
7
+ $amixer = Knj::Amixer.new
8
+ end
8
9
  end
9
10
 
10
11
  it "should register various devices" do
11
- $devices = $amixer.devices
12
+ if Knj::Os.os == "linux"
13
+ $devices = $amixer.devices
14
+ end
12
15
  end
13
16
 
14
17
  it "should register various mixers and do various operations on them" do
15
- $devices.each do |name, device|
16
- mixers = device.mixers
17
-
18
- if device.active?(:stream => "PLAYBACK")
19
- mixers.each do |name, mixer|
20
- next if !mixer.volume?
21
- mixer.vol_add -5
22
- mixer.vol_add 3
18
+ if Knj::Os.os == "linux"
19
+ $devices.each do |name, device|
20
+ mixers = device.mixers
21
+
22
+ if device.active?(:stream => "PLAYBACK")
23
+ mixers.each do |name, mixer|
24
+ next if !mixer.volume?
25
+ mixer.vol_add -5
26
+ mixer.vol_add 3
27
+ end
23
28
  end
24
29
  end
25
30
  end
data/spec/db_spec.rb CHANGED
@@ -42,7 +42,9 @@ describe "Db" do
42
42
  "test_table" => {
43
43
  "columns" => [
44
44
  {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
45
- {"name" => "name", "type" => "varchar"}
45
+ {"name" => "name", "type" => "varchar"},
46
+ {"name" => "age", "type" => "int"},
47
+ {"name" => "nickname", "type" => "varchar"}
46
48
  ],
47
49
  "indexes" => [
48
50
  "name"
@@ -98,6 +100,25 @@ describe "Db" do
98
100
  raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
99
101
 
100
102
 
103
+ #Test upserting.
104
+ data = {:name => "Kasper Johansen"}
105
+ sel = {:nickname => "kaspernj"}
106
+
107
+ table = db.tables[:test_table]
108
+ table.reload
109
+ rows_count = table.rows_count
110
+
111
+ db.upsert(:test_table, sel, data)
112
+
113
+ table.reload
114
+ table.rows_count.should eql(rows_count + 1)
115
+
116
+ db.upsert(:test_table, sel, data)
117
+
118
+ table.reload
119
+ table.rows_count.should eql(rows_count + 1)
120
+
121
+
101
122
  #Test dumping.
102
123
  dump = Knj::Db::Dump.new(:db => db, :debug => false)
103
124
  str_io = StringIO.new
@@ -159,4 +180,27 @@ describe "Db" do
159
180
  #Delete test-database if everything went well.
160
181
  File.unlink(db_path) if File.exists?(db_path)
161
182
  end
183
+
184
+ it "should generate proper sql" do
185
+ require "knj/db"
186
+ require "knj/os"
187
+ require "rubygems"
188
+ require "sqlite3" if !Kernel.const_defined?("SQLite3") and RUBY_ENGINE != "jruby"
189
+
190
+ db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
191
+ File.unlink(db_path) if File.exists?(db_path)
192
+
193
+ db = Knj::Db.new(
194
+ :type => "sqlite3",
195
+ :path => db_path,
196
+ :return_keys => "symbols",
197
+ :index_append_table_name => true
198
+ )
199
+
200
+ time = Time.new(1985, 6, 17, 10, 30)
201
+ db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
202
+
203
+ date = Date.new(1985, 6, 17)
204
+ db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
205
+ end
162
206
  end
data/spec/objects_spec.rb CHANGED
@@ -2,7 +2,12 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Objects" do
4
4
  it "should be able to cache rows" do
5
- require "#{File.dirname(__FILE__)}/../../array_enumerator/lib/array_enumerator"
5
+ begin
6
+ require "#{File.dirname(__FILE__)}/../../array_enumerator/lib/array_enumerator"
7
+ rescue LoadError
8
+ require "array_enumerator"
9
+ end
10
+
6
11
  require "sqlite3" if RUBY_ENGINE != "jruby"
7
12
 
8
13
  $db_path = "#{Knj::Os.tmpdir}/knjrbfw_objects_cache_test.sqlite3"
data/spec/process_spec.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ require "socket"
4
+
3
5
  describe "Process" do
4
6
  it "should be able to start a server and a client" do
5
7
  require "timeout"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knjrbfw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.103
4
+ version: 0.0.104
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-04 00:00:00.000000000 Z
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: wref
@@ -187,6 +187,22 @@ dependencies:
187
187
  - - ! '>='
188
188
  - !ruby/object:Gem::Version
189
189
  version: '0'
190
+ - !ruby/object:Gem::Dependency
191
+ name: array_enumerator
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
190
206
  description: Including stuff for HTTP, SSH and much more.
191
207
  email: k@spernj.org
192
208
  executables: []
@@ -455,7 +471,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
455
471
  version: '0'
456
472
  segments:
457
473
  - 0
458
- hash: 3060785805087232524
474
+ hash: -708484530570275600
459
475
  required_rubygems_version: !ruby/object:Gem::Requirement
460
476
  none: false
461
477
  requirements: