makumba_import 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,9 @@ More specifically it generates a database schema and models matching what's desc
7
7
 
8
8
  It supports all sorts of pointers, links, etc.
9
9
 
10
+ Update: now supports saving and creating new models. Keeps track of primary keys via Redis.
11
+
12
+
10
13
  Usage
11
14
  ---
12
15
 
@@ -27,8 +30,13 @@ Dir["#{Gem.searcher.find('makumba_import').full_gem_path}/lib/tasks/*.rake"].eac
27
30
  4. Generate a initializer called makumba_import.rb with the following contents:
28
31
 
29
32
  ```
30
- MakumbaImport::Importer.setMddPath "/..your.path../WEB-INF/classes/dataDefinitions"
31
- MakumbaImport::Importer.setOutputPath "./" # where to generate the .rb files. Leave blank to import straight into the app folders
33
+ MakumbaImport::Importer.set_mdd_path "/Users/marius/Projects/Cherry/cherry/web/WEB-INF/classes/dataDefinitions"
34
+ MakumbaImport::Importer.set_output_path "./" # where to generate the .rb files. Leave blank to import straight into the app folders
35
+
36
+ MakumbaImport::KeyHandler.set_dbsv 0
37
+ MakumbaImport::KeyHandler.init_redis REDIS # set this to your Redis class
38
+
39
+ #MakumbaImport::KeyHandler.update_redis_keys # uncomment once per server to store the latest keys per dbsv in redis
32
40
  ```
33
41
 
34
42
  5. Run
@@ -43,7 +51,7 @@ Extra:
43
51
  Use
44
52
 
45
53
  ```
46
- SomeModel.first.toExternalForm
54
+ SomeModel.first.to_ptr
47
55
  ```
48
56
 
49
57
  to get the makumba pointer
@@ -1,15 +1,15 @@
1
1
  module MakumbaImport
2
2
  class Importer
3
3
 
4
- @mddPath = ""
5
- @outputPath = ""
4
+ @mdd_path = ""
5
+ @output_path = ""
6
6
 
7
- def self.setMddPath(mddPath)
8
- @mddPath = mddPath
7
+ def self.set_mdd_path(mdd_path)
8
+ @mdd_path = mdd_path
9
9
  end
10
10
 
11
- def self.setOutputPath(outputPath)
12
- @outputPath = outputPath
11
+ def self.set_output_path(output_path)
12
+ @output_path = output_path
13
13
  end
14
14
 
15
15
  def self.get_data(lines, mdd)
@@ -108,7 +108,7 @@ module MakumbaImport
108
108
  end
109
109
 
110
110
  def self.load_mdds
111
- dir = @mddPath
111
+ dir = @mdd_path
112
112
 
113
113
  @files = Dir.glob(dir+"/*/*.mdd")
114
114
  @files |= Dir.glob(dir+"/*/*/*.mdd")
@@ -183,7 +183,7 @@ module MakumbaImport
183
183
 
184
184
  txt << "end\n\n"
185
185
 
186
- File.open(@outputPath+"db/schema.rb", "w+") do |f|
186
+ File.open(@output_path+"db/schema.rb", "w+") do |f|
187
187
  f.write(txt)
188
188
  end
189
189
  end
@@ -236,7 +236,7 @@ module MakumbaImport
236
236
 
237
237
  txt << "end\n\n"
238
238
 
239
- File.open(@outputPath+"app/models/"+filename, "w+") do |f|
239
+ File.open(@output_path+"app/models/"+filename, "w+") do |f|
240
240
  f.write(txt)
241
241
  end
242
242
 
@@ -0,0 +1,52 @@
1
+ module MakumbaImport
2
+ class KeyHandler
3
+
4
+ @dbsv = 0
5
+ @redis = nil
6
+
7
+ def self.set_dbsv(dbsv)
8
+ @dbsv = dbsv
9
+ end
10
+
11
+ def self.get_dbsv
12
+ @dbsv
13
+ end
14
+
15
+ def self.init_redis(redis)
16
+ @redis = redis
17
+ end
18
+
19
+ def self.next_primary_key(model)
20
+ @redis.incr "maxprimary_#{model}_#{@dbsv}"
21
+ end
22
+
23
+ def self.update_redis_keys
24
+ Dir[Rails.root.to_s + '/app/models/**/*.rb'].each do |file|
25
+ begin
26
+ require file
27
+ rescue
28
+ end
29
+ end
30
+
31
+ models = ActiveRecord::Base.subclasses.collect { |type| type.name }.sort
32
+
33
+ #p models
34
+
35
+ models.each do |model|
36
+ begin
37
+ object = Object.const_get(model)
38
+ primary_key = object.primary_key
39
+ current_max = object.unscoped.maximum(primary_key, :conditions => ["#{primary_key} > ? and #{primary_key} < ?", @dbsv << 24, (@dbsv + 1) << 24])
40
+ redis_key = "maxprimary_#{model}_#{@dbsv}"
41
+
42
+ @redis.set redis_key, current_max
43
+ rescue => e
44
+ p e
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
52
+
@@ -12,9 +12,12 @@ module LegacyMakumba
12
12
  unless self.method_defined? new_name
13
13
  self.send(:define_method, new_name) { self.send(old_name) }
14
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) }
15
16
  end
16
17
  end
17
18
  end
19
+
20
+ self.send(:before_save, :update_makumba_fields)
18
21
  end
19
22
 
20
23
  def set_makumba_pointer_type(type)
@@ -22,7 +25,17 @@ module LegacyMakumba
22
25
  end
23
26
  end
24
27
 
25
- def toExternalForm
28
+ def update_makumba_fields
29
+ self.id = next_primary_key if self.id.blank?
30
+ self.TS_create = Time.new if self.TS_create.blank?
31
+ self.TS_modify = Time.new
32
+ end
33
+
34
+ def next_primary_key
35
+ MakumbaImport::KeyHandler.next_primary_key(self.class.name)
36
+ end
37
+
38
+ def to_pointer
26
39
  def crc(v)
27
40
  r = 0
28
41
  32.times do
@@ -60,4 +73,9 @@ end
60
73
 
61
74
  class ActiveRecord::Base
62
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
+
63
81
  end
@@ -4,4 +4,5 @@ require 'open-uri'
4
4
  require 'fileutils'
5
5
 
6
6
  require 'makumba_import/importer'
7
+ require 'makumba_import/key_handler'
7
8
  require 'makumba_import/legacy'
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.1.0
4
+ version: 0.2.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: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Generate models matching your Makumba MDDs
15
15
  email: marius.andra@gmail.com
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/makumba_import/importer.rb
21
+ - lib/makumba_import/key_handler.rb
21
22
  - lib/makumba_import/legacy.rb
22
23
  - lib/makumba_import.rb
23
24
  - lib/tasks/makumba_import.rake
@@ -42,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
43
  version: '0'
43
44
  requirements: []
44
45
  rubyforge_project:
45
- rubygems_version: 1.8.11
46
+ rubygems_version: 1.8.24
46
47
  signing_key:
47
48
  specification_version: 3
48
49
  summary: Makumba database integration with Rails applications