knjrbfw 0.0.68 → 0.0.69

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.
@@ -24,6 +24,7 @@ GEM
24
24
  wref (0.0.5)
25
25
 
26
26
  PLATFORMS
27
+ java
27
28
  ruby
28
29
 
29
30
  DEPENDENCIES
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.68
1
+ 0.0.69
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjrbfw}
8
- s.version = "0.0.68"
8
+ s.version = "0.0.69"
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-07-26}
12
+ s.date = %q{2012-07-27}
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 = [
@@ -265,7 +265,6 @@ Gem::Specification.new do |s|
265
265
  "spec/process_spec.rb",
266
266
  "spec/spec_helper.rb",
267
267
  "spec/strings_spec.rb",
268
- "spec/threadsafe_spec.rb",
269
268
  "spec/web_spec.rb",
270
269
  "testfiles/image.jpg"
271
270
  ]
@@ -48,7 +48,6 @@ class Knj::Datarow
48
48
  # ]
49
49
  # end
50
50
  def self.depending_data
51
- @depending_data = [] if !@depending_data
52
51
  return @depending_data
53
52
  end
54
53
 
@@ -67,10 +66,13 @@ class Knj::Datarow
67
66
  # ]
68
67
  # end
69
68
  def self.autodelete_data
70
- @autodelete_data = [] if !@autodelete_data
71
69
  return @autodelete_data
72
70
  end
73
71
 
72
+ def self.autozero_data
73
+ return @autozero_data
74
+ end
75
+
74
76
  #This helps various parts of the framework determine if this is a datarow class without requiring it.
75
77
  #===Examples
76
78
  # print "This is a knj-object." if obj.respond_to?("is_knj?")
@@ -111,7 +113,7 @@ class Knj::Datarow
111
113
  colname = hval
112
114
  when :method
113
115
  methodname = hval
114
- when :depends, :autodelete, :where
116
+ when :depends, :autodelete, :autozero, :where
115
117
  #ignore
116
118
  else
117
119
  raise "Invalid key for 'has_many': '#{hkey}'."
@@ -121,14 +123,24 @@ class Knj::Datarow
121
123
  colname = "#{self.name.to_s.split("::").last.to_s.downcase}_id".to_sym if colname.to_s.empty?
122
124
 
123
125
  if val[:depends]
124
- self.depending_data << {
126
+ @depending_data = [] if !@depending_data
127
+ @depending_data << {
125
128
  :colname => colname,
126
129
  :classname => classname
127
130
  }
128
131
  end
129
132
 
130
133
  if val[:autodelete]
131
- self.autodelete_data << {
134
+ @autodelete_data = [] if !@autodelete_data
135
+ @autodelete_data << {
136
+ :colname => colname,
137
+ :classname => classname
138
+ }
139
+ end
140
+
141
+ if val[:autozero]
142
+ @autozero_data = [] if !@autozero_data
143
+ @autozero_data << {
132
144
  :colname => colname,
133
145
  :classname => classname
134
146
  }
@@ -793,17 +793,29 @@ class Knj::Objects
793
793
 
794
794
  if @args[:datarow]
795
795
  #If autodelete is set by 'has_many'-method, go through it and delete the various objects first.
796
- object.class.autodelete_data.each do |adel_data|
797
- self.list(adel_data[:classname], {adel_data[:colname].to_s => object.id}) do |obj_del|
798
- self.delete(obj_del, args)
796
+ if autodelete_data = object.class.autodelete_data
797
+ autodelete_data.each do |adel_data|
798
+ self.list(adel_data[:classname], {adel_data[:colname].to_s => object.id}) do |obj_del|
799
+ self.delete(obj_del, args)
800
+ end
799
801
  end
800
802
  end
801
803
 
802
804
  #If depend is set by 'has_many'-method, check if any objects exists and raise error if so.
803
- object.class.depending_data.each do |dep_data|
804
- obj = self.get_by(dep_data[:classname], {dep_data[:colname].to_s => object.id})
805
- if obj
806
- raise "Cannot delete <#{object.class.name}:#{object.id}> because <#{obj.class.name}:#{obj.id}> depends on it."
805
+ if dep_datas = object.class.depending_data
806
+ dep_datas.each do |dep_data|
807
+ if obj = self.get_by(dep_data[:classname], {dep_data[:colname].to_s => object.id})
808
+ raise "Cannot delete <#{object.class.name}:#{object.id}> because <#{obj.class.name}:#{obj.id}> depends on it."
809
+ end
810
+ end
811
+ end
812
+
813
+ #If autozero is set by 'has_many'-method, check if any objects exists and set the ID to zero.
814
+ if autozero_datas = object.class.autozero_data
815
+ autozero_datas.each do |zero_data|
816
+ self.list(zero_data[:classname], {zero_data[:colname].to_s => object.id}) do |obj_zero|
817
+ obj_zero[zero_data[:colname].to_sym] = 0
818
+ end
807
819
  end
808
820
  end
809
821
 
@@ -199,7 +199,7 @@ class Knj::Process
199
199
  buffer_done = false
200
200
 
201
201
  begin
202
- buffer_answers = Knj::Threadsafe.std_array #JRuby needs the threadsafety.
202
+ buffer_answers = Tsafe.std_array #JRuby needs the threadsafety.
203
203
  buffer_thread = Knj::Thread.new do
204
204
  loop do
205
205
  arr = buffer_answers.shift(200)
@@ -174,6 +174,16 @@ describe "Objects" do
174
174
  ]
175
175
  })
176
176
 
177
+ $db.tables.create("Timelog", {
178
+ "columns" => [
179
+ {"name" => "id", "type" => "int", "autoincr" => true, "primarykey" => true},
180
+ {"name" => "person_id", "type" => "int"}
181
+ ],
182
+ "indexes" => [
183
+ "person_id"
184
+ ]
185
+ })
186
+
177
187
  table = $db.tables["Project"]
178
188
 
179
189
  indexes = table.indexes
@@ -212,11 +222,19 @@ describe "Objects" do
212
222
  class Person < Knj::Datarow
213
223
  has_one [:Project]
214
224
 
225
+ has_many [
226
+ {:class => :Timelog, :autozero => true}
227
+ ]
228
+
215
229
  def html
216
230
  return self[:name]
217
231
  end
218
232
  end
219
233
 
234
+ class Timelog < Knj::Datarow
235
+
236
+ end
237
+
220
238
  $ob = Knj::Objects.new(:db => $db, :datarow => true, :require => false)
221
239
 
222
240
  $ob.add(:Person, {
@@ -334,6 +352,27 @@ describe "Objects" do
334
352
  raise "Expected persons count to be 0 but it wasnt: #{persons.map{|e| e.data} }" if persons.length > 0
335
353
  end
336
354
 
355
+ it "should do autozero when deleting objects" do
356
+ person1 = $ob.add(:Person, {
357
+ :name => "Kasper"
358
+ })
359
+ person2 = $ob.add(:Person, {
360
+ :name => "Charlotte"
361
+ })
362
+
363
+ timelog1 = $ob.add(:Timelog, {
364
+ :person_id => person1.id
365
+ })
366
+ timelog2 = $ob.add(:Timelog, {
367
+ :person_id => person2.id
368
+ })
369
+
370
+ $ob.delete(person1)
371
+
372
+ raise "Expected timelog1's person-ID to be zero but it wasnt: '#{timelog1[:person_id]}'." if timelog1[:person_id].to_i != 0
373
+ raise "Expected timelog2's person-ID to be #{person2.id} but it wasnt: '#{timelog2[:person_id]}'." if timelog2[:person_id].to_i != person2.id.to_i
374
+ end
375
+
337
376
  it "should delete the temp database again." do
338
377
  db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
339
378
  File.unlink(db_path) if File.exists?(db_path)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: knjrbfw
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.68
5
+ version: 0.0.69
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-07-26 00:00:00 +02:00
13
+ date: 2012-07-27 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -370,7 +370,6 @@ files:
370
370
  - spec/process_spec.rb
371
371
  - spec/spec_helper.rb
372
372
  - spec/strings_spec.rb
373
- - spec/threadsafe_spec.rb
374
373
  - spec/web_spec.rb
375
374
  - testfiles/image.jpg
376
375
  has_rdoc: true
@@ -387,7 +386,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
387
386
  requirements:
388
387
  - - ">="
389
388
  - !ruby/object:Gem::Version
390
- hash: -3521471445860246373
389
+ hash: -3747368515681482900
391
390
  segments:
392
391
  - 0
393
392
  version: "0"
@@ -1,57 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Threadsafe" do
4
- it "should be able to spawn threadsafe proxy-objects" do
5
- require "knjrbfw"
6
-
7
- arr = Knj::Threadsafe::Proxy.new(:obj => {})
8
-
9
- 0.upto(5) do |i|
10
- arr[i] = i
11
- end
12
-
13
- Knj::Thread.new do
14
- arr.each do |key, val|
15
- res = key + val
16
- sleep 0.1
17
- end
18
- end
19
-
20
- 5.upto(10) do |i|
21
- arr[i] = i
22
- sleep 0.1
23
- end
24
- end
25
-
26
- it "should be able to spawn special classes" do
27
- require "knjrbfw"
28
-
29
- #Create new synchronized hash.
30
- arr = Knj::Threadsafe::Synced_hash.new
31
-
32
- #Make sure we get the right results.
33
- arr[1] = 2
34
-
35
- res = arr[1]
36
- raise "Expected 2 but got '#{res}'." if res != 2
37
-
38
- #Set some values to test with.
39
- 0.upto(5) do |i|
40
- arr[i] = i
41
- end
42
-
43
- #Try to call through each through a thread and then also try to set new values, which normally would crash the hash.
44
- Knj::Thread.new do
45
- arr.each do |key, val|
46
- res = key + val
47
- sleep 0.1
48
- end
49
- end
50
-
51
- #This should not crash it, since they should wait for each other.
52
- 5.upto(10) do |i|
53
- arr[i] = i
54
- sleep 0.1
55
- end
56
- end
57
- end