rakudax 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ module Rakudax
2
+ class Settings < Settingslogic
3
+ source Rakudax::Base.settings_path
4
+ namespace Rakudax::Base.env
5
+ suppress_errors true
6
+ end
7
+ end
@@ -0,0 +1,116 @@
1
+ module Rakudax
2
+ class Skel
3
+ require 'pathname'
4
+ require 'fileutils'
5
+
6
+ DATABASE_YAML=<<-EOS
7
+ #sample_before:
8
+ # adapter: mysql2
9
+ # encoding: utf8
10
+ # reconnect: true
11
+ # database: sample_mysql
12
+ # pool: 1
13
+ # username: sample
14
+ # password: sample
15
+ # host: localhost
16
+
17
+ #sample_after:
18
+ # adapter: sqlite3
19
+ # pool: 1
20
+ # database: db/sample_sqlite.sqlite3
21
+ # timeout: 5000
22
+ EOS
23
+
24
+ GENERATE_YAML=<<-EOS
25
+ default: &default
26
+ limit: 0
27
+ models: []
28
+
29
+ production:
30
+ <<: *default
31
+
32
+ development:
33
+ <<: *default
34
+ EOS
35
+
36
+ MIGRATE_YAML=<<-EOS
37
+ default: &default
38
+ force_reset: true # truncate all data
39
+ models: []
40
+
41
+ production:
42
+ <<: *default
43
+
44
+ development:
45
+ <<: *default
46
+ EOS
47
+
48
+ SUBMIT_YAML=<<-EOS
49
+ default: &default
50
+ force_reset: true # truncate all data
51
+ models: []
52
+
53
+ production:
54
+ <<: *default
55
+
56
+ development:
57
+ <<: *default
58
+ EOS
59
+
60
+ VERIFY_YAML=<<-EOS
61
+ default: &default
62
+ limit: 0
63
+ models: []
64
+
65
+ production:
66
+ <<: *default
67
+
68
+ development:
69
+ <<: *default
70
+ EOS
71
+
72
+ GENERAL_MODULE=<<-EOS
73
+ module GeneralAttributes
74
+ ##This module is loaded automatically in `generate` or `migrate`
75
+ ##sample: overwrite(or add) a creator attribute.
76
+ # def creator
77
+ # "rakuda"
78
+ # end
79
+ end
80
+ EOS
81
+
82
+ def self.root
83
+ @@root ||= @@name.nil? ? Pathname.pwd : Pathname.pwd.join(@@name)
84
+ end
85
+
86
+ def self.create(name=nil)
87
+ @@name = name
88
+ self.create_dir("config")
89
+ self.create_file("config/database.yml", DATABASE_YAML)
90
+ self.create_file("config/generate.yml", GENERATE_YAML)
91
+ self.create_file("config/migrate.yml", MIGRATE_YAML)
92
+ self.create_file("config/submit.yml", SUBMIT_YAML)
93
+ self.create_file("config/verify.yml", VERIFY_YAML)
94
+ self.create_dir("modules")
95
+ self.create_file("modules/general_attributes.rb", GENERAL_MODULE)
96
+ end
97
+
98
+ def self.create_file(path, body)
99
+ path = self.root.join(path)
100
+ File.write path, body
101
+ puts "create #{path}"
102
+ rescue => ex
103
+ puts "cant create new file #{path}"
104
+ puts ex.message
105
+ end
106
+
107
+ def self.create_dir(path)
108
+ path = self.root.join(path)
109
+ FileUtils.mkdir_p path
110
+ puts "create #{path}"
111
+ rescue => ex
112
+ puts "cant create new folder #{path}"
113
+ puts ex.message
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,166 @@
1
+ module Rakudax
2
+ class Tasks
3
+ def self.generate
4
+ FileUtils.rm_rf Rakudax::Base.im_path
5
+ FileUtils.mkdir_p Rakudax::Base.im_path
6
+
7
+ puts "[start generate]=============#{Time.now}"
8
+ puts "output: #{Rakudax::Base.im_path}"
9
+
10
+ Rakudax::Settings.models.each do |model|
11
+ gen_code = "
12
+ class #{model.name} < ActiveRecord::Base
13
+ after_initialize :readonly!
14
+ begin
15
+ include GeneralAttributes
16
+ rescue
17
+ end
18
+ def self.after_name
19
+ '#{(model.after_name || model.name).underscore}'
20
+ end
21
+ #{"attr_accessor :id" if !model.id.nil? && model.auto_numbering}
22
+ "
23
+ (model.associations || []).each do |asc|
24
+ next unless asc.method
25
+ next unless asc.scope
26
+ gen_code += "#{asc.method} :#{asc.scope}#{", #{asc.options}" unless asc.options.nil?}
27
+ "
28
+ end
29
+
30
+ (model.aliases || {}).each do |k, v|
31
+ next unless k
32
+ next unless v
33
+ gen_code += "alias_attribute :#{v}, :#{k}
34
+ "
35
+ end
36
+
37
+ (model.modules || []).each do |mdl|
38
+ gen_code += "include #{mdl}
39
+ "
40
+ end
41
+
42
+ gen_code += "
43
+ def convert
44
+ {"
45
+ (model.attributes || []).each_with_index do |k, i|
46
+ if i == 0
47
+ gen_code += "\n '#{k}' => self.#{k}"
48
+ else
49
+ gen_code += ",\n '#{k}' => self.#{k}"
50
+ end
51
+ end
52
+
53
+ gen_code += "
54
+ }.reject{|key, value| (value.nil? || value == '')}
55
+ end
56
+ end
57
+ "
58
+ puts gen_code if Rakudax::Base.debug
59
+ eval gen_code
60
+ const_get(model.name).establish_connection Rakudax::Base.dbconfig[model.db]
61
+ const_get(model.name).table_name = model.table unless model.table.nil?
62
+ const_get(model.name).primary_key = model.id.to_sym unless model.id.nil?
63
+ const_get(model.name).inheritance_column = nil if model.inheritance
64
+ end
65
+
66
+ finally = []
67
+
68
+ Rakudax::Settings.models.each do |model|
69
+ print "Generating #{model.name} ".ljust(40, ".")
70
+ puts "total count: #{const_get(model.name).count}"
71
+ Rakudax::Base.models[const_get(model.name).after_name] ||= []
72
+ queue = Queue.new
73
+ ary_threads = []
74
+ if Rakudax::Settings.limit > 0
75
+ const_get(model.name).limit(Rakudax::Settings.limit).each_with_index do |value, idx|
76
+ queue.push [idx, value]
77
+ end
78
+ else
79
+ const_get(model.name).all.each_with_index do |value, idx|
80
+ queue.push [idx, value]
81
+ end
82
+ end
83
+
84
+ Rakudax::Base.threads.to_i.times do
85
+ ary_threads << Thread.start do
86
+ while !queue.empty?
87
+ idx, object = queue.pop(true)
88
+ object.id = idx + (model.auto_numbering_begin || 1).to_i if model.auto_numbering
89
+ value = object.convert
90
+ valid = true
91
+ encoding = Rakudax::Base.dbconfig[model.db]["encoding"] || "utf8"
92
+ unless encoding == "utf8"
93
+ require "nkf"
94
+ opt = case encoding
95
+ when "ujis"; '-E -w'
96
+ when "sjis"; '-S -w'
97
+ when "jis"; '-J -w'
98
+ else; '-E -w'
99
+ end
100
+ value.each do |k, v|
101
+ if v.nil? || v.blank? || k == "creater" || k == "updater" || (model.keep_encodes || []).include?(k)
102
+ next
103
+ elsif v.is_a?(String)
104
+ value[k] = NKF.nkf opt, v
105
+ else
106
+ STDERR.puts "Fatal Error: #{k} is not a String" if Rakudax::Base.debug
107
+ end
108
+ end
109
+ end
110
+ (model.required || []).each do | col |
111
+ valid = false if value[col].nil?
112
+ end
113
+ if valid
114
+ Rakudax::Base.models[const_get(model.name).after_name].push value
115
+ else
116
+ STDERR.puts "Faital Error: #{value} validation error"
117
+ end
118
+ end
119
+
120
+ Rakudax::Base.models[const_get(model.name).after_name].keep_if{|item| item.length != 0}
121
+
122
+ model_name = const_get(model.name).after_name
123
+ if model.data_output_finally == true
124
+ finally.push model_name
125
+ next
126
+ end
127
+ case Rakudax::Base.output_type
128
+ when "yaml"
129
+ File.open(Rakudax::Base.im_path.join(model_name), 'w') do | file |
130
+ file << YAML.dump(Rakudax::Base.models[model_name])
131
+ end
132
+ when "json"
133
+ require 'json'
134
+ File.open(Rakudax::Base.im_path.join(model_name), 'w') do | file |
135
+ file << Rakudax::Base.models[model_name].to_json
136
+ end
137
+ else
138
+ STDERR.puts "Faital Error: output type [#{Rakudax::Base.output_type}] not supported."
139
+ end
140
+ Rakudax::Base.models[model_name] = nil unless model.keep_mem_data == true
141
+ end
142
+ end
143
+ ary_threads.each { |th| th.join }
144
+ end
145
+
146
+ finally.each do |model_name|
147
+ case Rakudax::Base.output_type
148
+ when "yaml"
149
+ File.open(Rakudax::Base.im_path.join(model_name), 'w') do | file |
150
+ file << YAML.dump(Rakudax::Base.models[model_name])
151
+ end
152
+ when "json"
153
+ require 'json'
154
+ File.open(Rakudax::Base.im_path.join(model_name), 'w') do | file |
155
+ file << Rakudax::Base.models[model_name].to_json
156
+ end
157
+ else
158
+ STDERR.puts "Faital Error: output type [#{Rakudax::Base.output_type}] not supported."
159
+ end
160
+ end
161
+
162
+ puts "[finish generate]=============#{Time.now}"
163
+ end
164
+ end
165
+ end
166
+
@@ -0,0 +1,153 @@
1
+ module Rakudax
2
+ class Tasks
3
+ def self.migrate
4
+ puts "[start migrate]=============#{Time.now}"
5
+ Rakudax::Settings.models.each do |migration|
6
+ classname = migration.name
7
+ classname_before = "#{classname}Before"
8
+ model = migration.before
9
+ gen_code = "
10
+ class #{classname_before} < ActiveRecord::Base
11
+ after_initialize :readonly!
12
+ begin
13
+ include GeneralAttributes
14
+ rescue
15
+ end
16
+ #{"attr_accessor :id" if !model.id.nil? && model.auto_numbering}
17
+ "
18
+ (model.associations || []).each do |asc|
19
+ next unless asc.method
20
+ next unless asc.scope
21
+ gen_code += "#{asc.method} :#{asc.scope}#{", #{asc.options}" unless asc.options.nil?}
22
+ "
23
+ end
24
+
25
+ (model.aliases || {}).each do |k, v|
26
+ next unless k
27
+ next unless v
28
+ gen_code += "alias_attribute :#{v}, :#{k}
29
+ "
30
+ end
31
+
32
+ (model.modules || []).each do |mdl|
33
+ gen_code += "include #{mdl}
34
+ end
35
+ "
36
+ end
37
+
38
+ puts gen_code if Rakudax::Base.debug
39
+ eval gen_code
40
+ const_get(classname_before).establish_connection Rakudax::Base.dbconfig[model.db]
41
+ const_get(classname_before).table_name = model.table unless model.table.nil?
42
+ const_get(classname_before).primary_key = model.id.to_sym unless model.id.nil?
43
+ const_get(classname_before).inheritance_column = nil if model.inheritance
44
+
45
+ model = migration.after
46
+ gen_code = "
47
+ class #{classname} < ActiveRecord::Base
48
+ "
49
+
50
+ (model.associations || []).each do |asc|
51
+ next unless asc.method
52
+ next unless asc.scope
53
+ gen_code += "#{asc.method} :#{asc.scope}#{", #{asc.options}" unless asc.options.nil?}
54
+ "
55
+ end
56
+
57
+ (model.attrs || []).each do |attr|
58
+ next unless attr.method
59
+ next unless attr.scope
60
+ gen_code += "#{attr.method} :#{attr.scope}#{", #{attr.options}" unless attr.options.nil?}
61
+ "
62
+ end
63
+
64
+ (model.modules || []).each do |mdl|
65
+ gen_code += "include #{mdl}
66
+ "
67
+ end
68
+
69
+ gen_code += "
70
+ end
71
+ "
72
+
73
+ puts gen_code if Rakudax::Base.debug
74
+ eval gen_code
75
+ const_get(classname).establish_connection Rakudax::Base.dbconfig[model.db]
76
+ const_get(classname).table_name = model.table unless model.table.nil?
77
+ const_get(classname).primary_key = model.id.to_sym unless model.id.nil?
78
+ end
79
+
80
+ Rakudax::Settings.models.each do |migration|
81
+ classname = migration.name
82
+ classname_before = "#{classname}Before"
83
+ model = migration.after
84
+
85
+ classcount = 0
86
+ print "Checking #{classname} ... "
87
+ begin
88
+ classcount = const_get(classname).count
89
+ puts "Success (before count: #{classcount})"
90
+ rescue
91
+ puts "Fail"
92
+ STDERR.puts "Fatal Error: class #{classname} not found"
93
+ next
94
+ end
95
+
96
+ if Rakudax::Settings.force_reset
97
+ print "Delete All #{classname}s ... "
98
+ tablename = model.table || classname.tableize
99
+ ActiveRecord::Base.establish_connection Rakudax::Base.dbconfig[model.db]
100
+ case Rakudax::Base.dbconfig[model.db]["adapter"]
101
+ when "sqlite3"
102
+ ActiveRecord::Base.connection.execute("DELETE FROM #{tablename}")
103
+ ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='#{tablename}';")
104
+ ActiveRecord::Base.connection.execute("VACUUM")
105
+ else
106
+ ActiveRecord::Base.connection.execute("TRUNCATE #{tablename}")
107
+ end
108
+ if const_get(classname).count == 0
109
+ classcount = 0
110
+ puts "Success"
111
+ else
112
+ puts "Fail"
113
+ end
114
+ end
115
+
116
+ scope = const_get(classname_before).all
117
+ data_count = scope.count
118
+ print "Migrate #{const_get(classname).to_s}#{"s" if data_count > 1}(#{data_count}) ..."
119
+ scope.each do |data|
120
+ obj = const_get(classname).new
121
+ if migration.auto_matching
122
+ const_get(classname).attribute_names.each do |key|
123
+ puts key
124
+ obj.send("#{key}=", data.send(key)) if data.attribute_names.include?(key)
125
+ end
126
+ end
127
+ puts migration.attributes
128
+ (migration.attributes || {}).each do |key1, key2|
129
+ obj.send("#{key2}=", data.send(key1))
130
+ end
131
+ begin
132
+ unless obj.save
133
+ STDERR.puts "Fatal Error: New #{classname} cant create. #{obj.errors.full_messages}"
134
+ end
135
+ rescue => ex
136
+ STDERR.puts "Fatal Error: New #{classname} cant create. #{hash} (#{ex.message})"
137
+ end
138
+ end
139
+
140
+ totalcount = const_get(classname).count
141
+ createcount = totalcount - classcount
142
+ if createcount == data_count
143
+ puts " OK (succeed: #{createcount}/failed: 0/ total: #{totalcount})"
144
+ else
145
+ puts " NG (succeed: #{createcount}/failed: #{data_count - createcount}/ total: #{totalcount})"
146
+ end
147
+ value = nil
148
+ end
149
+
150
+ puts "[finish migrate]=============#{Time.now}"
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,114 @@
1
+ module Rakudax
2
+ class Tasks
3
+ def self.submit
4
+ unless Dir.exists?(Rakudax::Base.im_path)
5
+ puts "error: Intermediate directory not found."
6
+ puts "Intermediate directory path: #{Rakudax::Base.im_path}"
7
+ exit 2
8
+ end
9
+
10
+ puts "[submit start]=============#{Time.now}"
11
+ puts "Data file path: #{Rakudax::Base.im_path}/<files>"
12
+
13
+ Rakudax::Settings.models.each do |model|
14
+ gen_code = "
15
+ class #{model.name} < ActiveRecord::Base
16
+ "
17
+
18
+ (model.associations || []).each do |asc|
19
+ next unless asc.method
20
+ next unless asc.scope
21
+ gen_code += "#{asc.method} :#{asc.scope}#{", #{asc.options}" unless asc.options.nil?}
22
+ "
23
+ end
24
+
25
+ (model.attrs || []).each do |attr|
26
+ next unless attr.method
27
+ next unless attr.scope
28
+ gen_code += "#{attr.method} :#{attr.scope}#{", #{attr.options}" unless attr.options.nil?}
29
+ "
30
+ end
31
+
32
+ (model.modules || []).each do |mdl|
33
+ gen_code += "include #{mdl}
34
+ "
35
+ end
36
+
37
+ gen_code += "
38
+ end
39
+ "
40
+
41
+ puts gen_code if Rakudax::Base.debug
42
+ eval gen_code
43
+ const_get(model.name).establish_connection Rakudax::Base.dbconfig[model.db]
44
+ const_get(model.name).table_name = model.table unless model.table.nil?
45
+ const_get(model.name).primary_key = model.id.to_sym unless model.id.nil?
46
+ end
47
+
48
+
49
+ Rakudax::Settings.models.each do |model|
50
+ classname = model.name
51
+ data_path = Rakudax::Base.im_path.join(classname.underscore)
52
+ unless File.exists?(data_path)
53
+ puts "Loaded: #{data_path}"
54
+ puts "Skip #{classname}, because data is not found."
55
+ next
56
+ end
57
+ value = YAML.load(File.read(data_path))
58
+ classcount = 0
59
+ print "Checking #{classname} ... "
60
+ begin
61
+ classcount = const_get(classname).count
62
+ puts "Success (before count: #{classcount})"
63
+ rescue => ex
64
+ puts "Fail"
65
+ STDERR.puts "Fatal Error: class #{classname} not found"
66
+ puts ex.message if Rakudax::Base.debug
67
+ next
68
+ end
69
+
70
+ if Rakudax::Settings.force_reset
71
+ print "Delete All #{classname}s ... "
72
+ tablename = model.table || classname.tableize
73
+ ActiveRecord::Base.establish_connection Rakudax::Base.dbconfig[model.db]
74
+ case Rakudax::Base.dbconfig[model.db]["adapter"]
75
+ when "sqlite3"
76
+ ActiveRecord::Base.connection.execute("DELETE FROM #{tablename}")
77
+ ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='#{tablename}';")
78
+ ActiveRecord::Base.connection.execute("VACUUM")
79
+ else
80
+ ActiveRecord::Base.connection.execute("TRUNCATE #{tablename}")
81
+ end
82
+ if const_get(classname).count == 0
83
+ classcount = 0
84
+ puts "Success"
85
+ else
86
+ puts "Fail"
87
+ end
88
+ end
89
+
90
+ print "Submit #{const_get(classname).to_s}#{"s" if value.count > 1} ..."
91
+ value.each do | hash |
92
+ $hash = hash
93
+ begin
94
+ const_get(classname).create hash
95
+ rescue => ex
96
+ STDERR.puts "Fatal Error: New #{classname} cant create. #{hash}"
97
+ puts ex.message if Rakudax::Base.debug
98
+ end
99
+ end
100
+
101
+ totalcount = const_get(classname).count
102
+ createcount = totalcount - classcount
103
+ if createcount == value.count
104
+ puts " OK (succeed: #{createcount}/failed: 0/ total: #{totalcount})"
105
+ else
106
+ puts " NG (succeed: #{createcount}/failed: #{value.count - createcount}/ total: #{totalcount})"
107
+ end
108
+ value = nil
109
+ end
110
+
111
+ puts "[submit finish]=============#{Time.now}"
112
+ end
113
+ end
114
+ end