makumba_import 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/makumba_import/legacy.rb +56 -50
  2. metadata +2 -2
@@ -1,40 +1,71 @@
1
- module LegacyMakumba
1
+ String.class_eval do
2
+ # http://www.func09.com/wordpress/archives/228
3
+ def to_java_hashcode
4
+ max = 2 ** 31 - 1
5
+ min = -2 ** 31
6
+ h = 0
7
+ n = self.size
8
+ n.times do |i|
9
+ h = 31 * h + self[i].ord
10
+ while h < min || max < h
11
+ h = max - ( min - h ) + 1 if h < min
12
+ h = min - ( max - h ) - 1 if max < h
13
+ end
14
+ end
15
+ h
16
+ end
17
+ end
2
18
 
3
- def self.append_features(base)
4
- super
5
- base.extend(ClassMethods)
19
+ class ActiveRecord::Base
20
+ def self.metaclass
21
+ class << self; self; end
22
+ end
23
+
24
+ def self.last_primary_key(dbsv = MakumbaImport::KeyHandler.get_dbsv)
25
+ self.select("max(#{primary_key}) as biggest").where("#{primary_key} > ? and #{primary_key} < ?", dbsv << 24, (dbsv + 1) << 24).first.biggest || (dbsv << 24)
6
26
  end
7
- module ClassMethods
8
- def fix_makumba_columns
9
- column_names.each do |old_name|
10
- if old_name.match(/_$/)
11
- new_name = old_name[0...-1]
12
- unless self.method_defined? new_name
13
- self.send(:define_method, new_name) { self.send(old_name) }
14
- self.send(:define_method, "#{new_name}=") { |value| self.send("#{old_name}=", value) }
15
- self.send(:define_method, "find_by_#{new_name}") { |value| self.send("find_by_#{old_name}", value) }
16
- end
27
+
28
+ def self.pointer_type=(type)
29
+ @@pointer_type = type
30
+ end
31
+
32
+ def self.pointer_type
33
+ @@pointer_type
34
+ end
35
+
36
+ def self.find_by_ptr(ptr)
37
+ hc = @@pointer_type.to_java_hashcode & "ffffffffl".to_i(16)
38
+ l = ptr.to_i(36)
39
+ n = l & "ffffffffl".to_i(16)
40
+ n = n ^ hc
41
+ self.find(n)
42
+ end
43
+
44
+ def self.fix_makumba_columns
45
+ column_names.each do |old_name|
46
+ if old_name.match(/_$/)
47
+ new_name = old_name[0...-1]
48
+ unless self.method_defined? new_name
49
+ self.send(:define_method, new_name) { self.send(old_name) }
50
+ self.send(:define_method, "#{new_name}=") { |value| self.send("#{old_name}=", value) }
51
+ self.metaclass.send(:define_method, "find_by_#{new_name}") { |value| self.send("find_by_#{old_name}", value) }
17
52
  end
18
53
  end
19
-
20
- self.send(:before_save, :update_makumba_fields)
21
54
  end
22
55
 
23
- def set_makumba_pointer_type(type)
24
- self.send(:define_method, "pointer_type") { type }
25
- end
56
+ self.send(:before_save, :update_makumba_fields)
57
+ end
58
+
59
+ def self.next_primary_key
60
+ MakumbaImport::KeyHandler.next_primary_key(self.to_s)
26
61
  end
27
62
 
28
63
  def update_makumba_fields
29
- self.id = next_primary_key if self.id.blank?
64
+ self.id = self.class.next_primary_key if self.id.blank?
30
65
  self.TS_create = Time.new if self.TS_create.blank?
31
66
  self.TS_modify = Time.new
32
67
  end
33
68
 
34
- def next_primary_key
35
- MakumbaImport::KeyHandler.next_primary_key(self.class.name)
36
- end
37
-
38
69
  def to_ptr
39
70
  def crc(v)
40
71
  r = 0
@@ -47,35 +78,10 @@ module LegacyMakumba
47
78
  r
48
79
  end
49
80
 
50
- # http://www.func09.com/wordpress/archives/228
51
- def to_hashcode(str)
52
- max = 2 ** 31 - 1
53
- min = -2 ** 31
54
- h = 0
55
- n = str.size
56
- n.times do |i|
57
- h = 31 * h + str[i].ord
58
- while h < min || max < h
59
- h = max - ( min - h ) + 1 if h < min
60
- h = min - ( max - h ) - 1 if max < h
61
- end
62
- end
63
- h
64
- end
65
-
66
81
  n = self.id
67
82
 
68
- hc = to_hashcode(pointer_type) & "ffffffffl".to_i(16)
83
+ hc = @@pointer_type.to_java_hashcode & "ffffffffl".to_i(16)
69
84
  ((crc(n) & "fl".to_i(16)) << 32 | n ^ hc).to_s(36)
70
85
  end
71
86
 
72
87
  end
73
-
74
- class ActiveRecord::Base
75
- include LegacyMakumba
76
-
77
- def self.last_primary_key(dbsv = MakumbaImport::KeyHandler.get_dbsv)
78
- self.select("max(#{primary_key}) as biggest").where("#{primary_key} > ? and #{primary_key} < ?", dbsv << 24, (dbsv + 1) << 24).first.biggest || (dbsv << 24)
79
- end
80
-
81
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: makumba_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
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-02-20 00:00:00.000000000 Z
12
+ date: 2013-06-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Generate models matching your Makumba MDDs
15
15
  email: marius.andra@gmail.com