arxutils 0.1.36 → 0.1.37

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1b719f143307e145df60195bc5e2670aa7fb2474
4
- data.tar.gz: 00c4f031d2555d54eb937523eb8b02c7f5a57e52
2
+ SHA256:
3
+ metadata.gz: bfbf386f39f4af2fb9c1b4d9b142c89608749bfa6bda9b4bab93fac8db969689
4
+ data.tar.gz: 2597ac0653cd95df7f20848fcb601a86a63d987eca2ae9dd9dd0c45ffdceda49
5
5
  SHA512:
6
- metadata.gz: bee744f9f8444396d9e0c41dda4851dfb24c0e6959c3e465bf1e91b0f64a2e1c4a99427aae8fce2e778d75dcf99bd872a773f5143cf1ea9f9c7e15b0afb80be1
7
- data.tar.gz: a0fd2f8c5f1c0973d0673194ebd98b83e033fba1ba6eb7620a5ca2f13cddd75d0113c05425afad57e9fb7de838bf7b7f83c0b112a71078fdd9911b21a085be48
6
+ metadata.gz: 7517445f61025f9d9318ae29abe86e9b16a9dfde361301225c531fb9047fa03073d9891a09616a4369070fe8d412f3f19b6f4b1e35ce1d075ccd494c91419a06
7
+ data.tar.gz: fdd422812f1f003fe26b3b4f9206fd9a5472bfded8e52476b674613046a2ae05cd63e65754ccecc7afaa1bc89b13b1ab01dd0db10eea33d5d176536e03cda269
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ vendor/
data/.rspec CHANGED
File without changes
File without changes
data/Gemfile CHANGED
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
@@ -29,8 +29,9 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_runtime_dependency "activesupport"
31
31
  # spec.add_runtime_dependency "erb"
32
- spec.add_runtime_dependency "activerecord"
32
+ spec.add_runtime_dependency "activerecord" , "~> 4.2"
33
33
  spec.add_runtime_dependency "sqlite3"
34
+ spec.add_runtime_dependency "mysql2" , "~> 0.4.1"
34
35
  spec.add_runtime_dependency "encx"
35
36
 
36
37
  spec.add_development_dependency "bundler", "~> 1.10"
File without changes
data/bin/setup CHANGED
File without changes
File without changes
@@ -6,12 +6,8 @@ require 'arxutils/arxutils'
6
6
  require 'arxutils/arx'
7
7
  require 'arxutils/transactstate'
8
8
  require 'arxutils/hier'
9
- require 'arxutils/misc'
10
9
  require 'arxutils/dbutil/dbinit'
11
10
  require 'arxutils/dbutil/dbmgr'
12
- require 'arxutils/store'
13
- require 'arxutils/store/storedb'
14
- require 'arxutils/store/storecsv'
15
11
  require 'arxutils/migrate'
16
12
 
17
13
 
@@ -2,10 +2,15 @@
2
2
  require 'erb'
3
3
 
4
4
  module Arxutils
5
+ # スキーマ設定に基づき、テンプレートから変換する
5
6
  class Arx
7
+ # スキーマ設定配列を、テンプレートで参照可能になるように展開する
6
8
  def initialize( data , fname )
9
+ # テンプレートファイルへのパス
7
10
  @fname = fname
8
- # 以下のものの配列
11
+
12
+ # スキーマ設定配列
13
+ # スキーマ設定とは以下をキーにもつハッシュである
9
14
  # :flist
10
15
  # :classname
11
16
  # :classname_downcase
@@ -15,6 +20,7 @@ module Arxutils
15
20
  # :relation
16
21
  @data = data
17
22
 
23
+ # スキーマ設定の:itemsの値を展開後格納するためのStructクラス
18
24
  @@field ||= Struct.new("Field" , :name, :type, :null )
19
25
 
20
26
  if @data[:items]
@@ -24,6 +30,7 @@ module Arxutils
24
30
  end
25
31
  end
26
32
 
33
+ # テンプレートファイルを元にした変換結果を返す
27
34
  def create
28
35
  contents = File.open( @fname ).read
29
36
  erb = ERB.new(contents)
@@ -1,17 +1,22 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module Arxutils
4
+ # モジュールArxutils内でのユーティリティクラス
4
5
  class Arxutils
5
- def Arxutils.dirname
6
- File.dirname( __FILE__ )
7
- end
8
-
9
- def Arxutils.templatedir
6
+ # モジュールArxutils内のテンプレートファイル格納ディレクトリへのパスを返す
7
+ def self.templatedir
10
8
  File.join( Arxutils.dirname , ".." , "template" )
11
9
  end
12
10
 
13
- def Arxutils.configdir
11
+ # モジュールArxutils内の構成情報格納ディレクトリへのパスを返す
12
+ def self.configdir
14
13
  File.join( Arxutils.dirname , ".." , 'config' )
15
14
  end
15
+
16
+ private
17
+ # モジュールArxutils内のlib/arxutilsディレクトリへのパスを返す
18
+ def self.dirname
19
+ File.dirname( __FILE__ )
20
+ end
16
21
  end
17
22
  end
@@ -1,45 +1,74 @@
1
1
  #! -*- encoding : UTF-8 -*-
2
+ # coding: utf-8
2
3
  require 'fileutils'
3
4
  require 'yaml'
4
5
  require 'active_record'
5
- require 'sqlite3'
6
+ #require 'sqlite3'
7
+ require 'mysql2'
6
8
 
7
9
  module Arxutils
10
+ # DB操作用ユーティリティクラス
8
11
  module Dbutil
12
+ # DB格納ディレクトリ名
9
13
  DB_DIR = 'db'
14
+ # migrate用スクリプト格納ディレクトリ名
10
15
  MIGRATE_DIR = 'migrate'
16
+ # SQLITE3用DB構成名
11
17
  DBCONFIG_SQLITE3 = 'sqlite3'
18
+ # MYSQL用DB構成名
12
19
  DBCONFIG_MYSQL = 'mysql'
20
+ # DB構成格納用ディレクトリ名
13
21
  CONFIG_DIR = 'config'
22
+ # データベース用ログファイル名
14
23
  DATABASELOG = 'database.log'
15
-
24
+
25
+ # DB接続までの初期化を行う
16
26
  class Dbinit
17
- attr_accessor :dbconfig_dest_path , :dbconfig_src_path , :dbconfig_src_fname , :dbconfig_dest_fname , :migrate_dir
18
-
19
- def initialize( db_dir , migrate_base_dir , src_config_dir , dbconfig , env, log_fname, forced = false )
27
+ # 生成するDB構成情報ファイルパス
28
+ attr_accessor :dbconfig_dest_path
29
+ # 参照用DB構成情報ファイル名
30
+ attr_accessor :dbconfig_src_fname
31
+ # migrate用スクリプトの出力先ディレクトリ名
32
+ attr_accessor :migrate_dir
33
+
34
+ # DB接続までの初期化に必要なディレクトリの確認、作成
35
+ def initialize( db_dir , migrate_base_dir , src_config_dir , dbconfig , env, log_fname, opts )
36
+ # DB格納ディレクトリ名
20
37
  @db_dir = db_dir
38
+ # DB構成ファイルのテンプレート格納ディレクトリ
21
39
  @src_config_dir = src_config_dir
40
+ # DB構成ファイルの出力先ディレクトリ
22
41
  @dest_config_dir = "config"
42
+ # DB構成ファイル名
23
43
  @dbconfig_dest_fname = "#{dbconfig}.yaml"
44
+ # DB構成ファイル用テンプレートファイル名
24
45
  @dbconfig_src_fname = "#{dbconfig}.tmpl"
46
+ # DB構成ファイルへのパス
25
47
  @dbconfig_dest_path = File.join( @dest_config_dir , @dbconfig_dest_fname)
48
+ # DB構成ファイル用テンプレートファイルへのパス
26
49
  @dbconfig_src_path = File.join(@src_config_dir , @dbconfig_src_fname)
50
+ # 環境の指定
27
51
  @env = env
52
+ # DB用ログファイル名
28
53
  @log_fname = log_fname
29
54
 
30
55
  if @db_dir and @log_fname
56
+ # DB用ログファイルへのパス
31
57
  @log_path = File.join( @db_dir , @log_fname )
58
+ # migrate用スクリプト格納ディレクトリへのパス
32
59
  @migrate_dir = File.join( @db_dir , migrate_base_dir )
33
60
  end
34
61
  FileUtils.mkdir_p( @db_dir ) if @db_dir
35
62
  FileUtils.mkdir_p( @migrate_dir ) if @migrate_dir
36
63
  FileUtils.mkdir_p( @dest_config_dir )
37
- if forced
64
+ # remigrateが指定されれば、migrate用スクリプトとDB構成ファイルを削除する
65
+ if opts["remigate"]
38
66
  FileUtils.rm( Dir.glob( File.join( @migrate_dir , "*"))) if @migrate_dir
39
67
  FileUtils.rm( Dir.glob( File.join( @dest_config_dir , "*")))
40
68
  end
41
69
  end
42
-
70
+
71
+ # DB接続し、DB用ログファイルの設定
43
72
  def setup
44
73
  dbconfig = YAML.load( File.read( @dbconfig_dest_path ) )
45
74
  ActiveRecord::Base.establish_connection(dbconfig[@env])
@@ -1,19 +1,21 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require 'arxutils/dbutil/dbinit'
4
-
5
4
  require 'date'
6
5
  require 'pp'
7
6
 
8
7
  module Arxutils
9
8
  module Dbutil
9
+ # DB接続時に、現在日時も取得したい場合のユーティリティクラス
10
10
  class DbMgr
11
- def DbMgr.init( db_dir , migrate_dir , config_dir , dbconfig, env , log_fname, forced = false )
12
- dbinit = Dbinit.new( db_dir, migrate_dir , config_dir , dbconfig, env , log_fname, forced )
13
- DbMgr.setup( dbinit )
11
+ # Dbinitクラスのインスタンス生成とDB接続、現在日時取得
12
+ def self.init( db_dir , migrate_dir , config_dir , dbconfig, env , log_fname, opts )
13
+ dbinit = Dbinit.new( db_dir, migrate_dir , config_dir , dbconfig, env , log_fname, opts )
14
+ self.setup( dbinit )
14
15
  end
15
-
16
- def DbMgr.setup( dbinit )
16
+
17
+ # DB接続、現在日時取得
18
+ def self.setup( dbinit )
17
19
  @@ret ||= nil
18
20
  unless @@ret
19
21
  begin
@@ -28,42 +30,6 @@ module Arxutils
28
30
 
29
31
  @@ret
30
32
  end
31
- def DbMgr.conv_string(value , encoding)
32
- if value.class == String
33
- if value.encodingy != encoding
34
- value.encode(encoding)
35
- else
36
- value
37
- end
38
- else
39
- value
40
- end
41
- end
42
-
43
- def DbMgr.conv_boolean( k , v )
44
- ret = v
45
- if k =~ /enable/
46
- if v.class == String
47
- case v
48
- when 'T'
49
- ret = true
50
- when 'F'
51
- ret = false
52
- else
53
- raise
54
- end
55
- elsif v.class == TrueClass
56
- # do nothin
57
- elsif v.class == FalseClass
58
- # do nothin
59
- else
60
- p v.class
61
- p v
62
- raise
63
- end
64
- end
65
- ret
66
- end
67
33
  end
68
34
  end
69
35
  end
@@ -1,42 +1,61 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module Arxutils
4
+ # 階層処理
4
5
  class HierOp
5
- attr_reader :field_name, :hier_symbol, :base_klass, :hier_klass, :current_klass, :invalid_klass
6
-
6
+ # 階層処理を付加したいフィールド名(未使用か?)
7
+ attr_reader :field_name
8
+ # '/'が区切り文字の文字列で階層処理を実現するクラスの階層構造を表す文字列を持つメソッド/アトリビュートを表すシンボル
9
+ attr_reader :hier_symbol
10
+ # '/'が区切り文字の文字列で階層処理を実現するクラスのクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
11
+ # シンボルhier_symbolで指定できるメソッド/アトリビュート(string)を持つ。
12
+ # nameというメソッド/アトリビュート(string)を持つ。"'/'を区切り文字として持つ階層を表す文字列
13
+ # registerメソッドを呼び出す時は、hier_symbolのみを指定してcreate出来なければならない(そうでなければSQLの制約違反発生)
14
+ attr_reader :base_klass
15
+ # '/'が区切り文字の文字列で階層処理を実現するクラスのカレントに対応するクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
16
+ attr_reader :current_klass
17
+ # '/'が区切り文字の文字列で階層処理を実現するクラスのインバリッドに対応するクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
18
+ attr_reader :invalid_klass
19
+ # IDの親子関係で階層処理を実現するクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
20
+ # parent_id(integer) , child_id(integer) , leve(integer)というメソッド/アトリビュートを持つ
21
+ attr_reader :hier_klass
22
+
23
+ # 初期化
7
24
  def initialize( field_name, hier_symbol , hier_name, base_klass , hier_klass , current_klass , invalid_klass )
25
+ # 階層処理を付加したいフィールド名
8
26
  @field_name = field_name
27
+ # '/'が区切り文字の文字列で階層処理を実現するクラスの階層構造を表す文字列を持つメソッド/アトリビュートを表すシンボ
9
28
  @hier_symbol = hier_symbol
29
+ # '/'が区切り文字の文字列で階層処理を実現するクラスのクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
10
30
  @base_klass = base_klass
11
- @hier_klass = hier_klass
31
+ # '/'が区切り文字の文字列で階層処理を実現するクラスのカレントに対応するクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
12
32
  @current_klass = current_klass
33
+ # '/'が区切り文字の文字列で階層処理を実現するクラスのインバリッドに対応するクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
13
34
  @invalid_klass = invalid_klass
35
+ # IDの親子関係で階層処理を実現するクラス名(DB中のテーブルに対応するActiveRecordの子クラス)
36
+ # print_id(integer), child_id(integer), level(integer)
37
+ @hier_klass = hier_klass
14
38
  end
15
39
 
16
- def get_category_hier_json( kind_num )
17
- JSON( @hier_klass.pluck( :parent_id , :child_id , :level ).map{ |ary|
18
- text = @base_klass.find( ary[1] ).__send__( @hier_symbol ).split("/").pop
19
- if ary[2] == 0
20
- parent_id = "#"
21
- else
22
- parent_id = %Q!#{ary[0]}!
23
- end
24
- child_id = %Q!#{ary[1]}!
25
- { "id" => child_id , "parent" => parent_id , "text" => text }
26
- } )
27
- end
28
-
40
+ # 指定した階層(階層を/で区切って表現)のアイテムをbase_klassから削除
29
41
  def delete( hier )
30
42
  # 子として探す
31
43
  id = nil
32
- row_item = @base_klass.find_by( { @hier_symbol => hier } )
33
- if row_item
34
- id = row_item.id
35
- delete_at( id )
44
+ base = @base_klass.find_by( { @hier_symbol => hier } )
45
+ if base
46
+ delete_at( base.org_id )
47
+ @base_klass.delete_at( base.id )
36
48
  end
37
49
  id
38
50
  end
39
51
 
52
+ def delete_by_id( id )
53
+ base = @base_klass.find_by( org_id: id )
54
+ delete_at( id )
55
+ @base_klass.delete_at( base.id )
56
+ end
57
+
58
+ # 文字列で指定した階層を移動
40
59
  def move( src_hier , dest_parent_hier )
41
60
  # dest_parent_hierがsrc_hierの子であれば(=src_hierがdest_parent_hierの先頭からの部分文字列である)何もせずエラーを返す
42
61
  escaped = Regexp.escape( src_hier )
@@ -46,12 +65,12 @@ module Arxutils
46
65
  if ret
47
66
  return false
48
67
  end
49
-
68
+
50
69
  src_row_item = @base_klass.where( name: src_hier )
51
70
  src_num = src_row_item.id
52
- # srcが子である(tblでは項目を一意に指定できる)tblでの項目を得る
71
+ # srcが子である(tblでは項目を一意に指定できる)tblでの項目を得る
53
72
  src_row = @hire_klass.find_by( child_id: src_num )
54
-
73
+
55
74
  dest_parent_row_item = @base_klass.find_by( name: dest_parent_hier )
56
75
  unless dest_parent_row_item
57
76
  dest_parent_num = register( dest_parent_hier )
@@ -59,20 +78,22 @@ module Arxutils
59
78
  dest_parent_num = dest_parent_row_item.id
60
79
  end
61
80
  dest_parent_level = get_level_by_child( dest_parent_num )
62
-
81
+
63
82
  # srcの親をdest_parentにする
64
83
  src_row.parent_id = dest_parent_num
65
-
84
+ src_row.save
66
85
  # destに移動後のsrcの子のレベルを調整する
67
86
  level_adjust( src_row , dest_parent_level )
68
87
  # destに移動後のsrcのhierを再設定
69
88
  set_hier( src_row_item , make_hier( dest_parent_row_item.name , get_name( src_row_item ) ) )
89
+ src_row_item.save
70
90
  # destに移動後のsrcの子のhierを調整する
71
91
  hier_adjust( src_row_item )
72
92
 
73
93
  true
74
94
  end
75
95
 
96
+ # 配列で指定した階層を親の階層としてhier_klassに登録
76
97
  def register_parent( hier_ary , child_num , level )
77
98
  hier_ary.pop
78
99
  parent_hier_ary = hier_ary
@@ -81,13 +102,13 @@ module Arxutils
81
102
  hs = { parent_id: parent_num , child_id: child_num , level: level }
82
103
  @hier_klass.create( hs )
83
104
  end
84
-
105
+
106
+ # 文字列で指定した階層(/を区切り文字として持つ)をhier_klassに登録
85
107
  def register( hier )
86
108
  hier_ary = hier.split('/')
87
109
  level = get_level_by_array( hier_ary )
88
110
 
89
111
  # もしhier_aryがnilだけを1個持つ配列、または空文字列だけを1個もつ配列であれば、hier_nameは空文字列になる
90
-
91
112
  item_row = @current_klass.find_by( {@hier_symbol => hier} )
92
113
  unless item_row
93
114
  # @base_klassがhierだけでcreateできる場合は(他にフィールドがnot_nullでないか)、ここに来てもよい。
@@ -118,42 +139,47 @@ module Arxutils
118
139
  end
119
140
  new_num
120
141
  end
121
-
142
+
122
143
  private
123
-
144
+
145
+ # IDで指定した階層を削除
124
146
  def delete_at( num )
125
147
  # 子として探す
126
- row = @hier_klass.find_by( child_id: num )
127
- level = row.level
128
- parent_id = row.parent_id
129
- row_item = @base_klass.find( num )
130
-
131
- parent_item_row = @base_klass.find( parent_id )
132
- parent_hier = parent_item_row.name
148
+ hier = @hier_klass.find_by( child_id: num )
149
+ level = hier.level
150
+ parent_id = hier.parent_id
151
+ base = @base_klass.find_by( ord_id: num )
152
+
153
+ parent_base = @base_klass.find_by( ord_id: parent_id )
154
+ parent_hier_string = parent_base.__send__ @hier_symbol
133
155
 
134
156
  # 属する子を探す
135
- child_rows = @hier_klass.where( parent_id: num )
157
+ children_hier = @hier_klass.where( parent_id: num )
136
158
  # 属する子の階層レベルを調整する(削除するのでlevel - 1になる)
137
- child_rows.map{ |x| level_adjust( x , level - 1 ) }
159
+ children_hier.map{ |x| level_adjust( x , level - 1 ) }
138
160
  # 属する子の親を、親の親にする
139
- child_rows.map{ |x|
161
+ children_hier.map{ |x|
140
162
  x.parent_id = parent_id
163
+ x.save
141
164
  }
142
165
  # 属する子のhierを調整する
143
- child_rows.map{ |x|
144
- child_item_row = @base_klass.find( x.child_id )
145
- name = get_name( child_item_row )
146
- child_item_row.name = make_hier( parent_hier , name )
147
- hier_adjust( child_item_row )
166
+ children_hier.map{ |x|
167
+ child_base = @base_klass.find_by( org_id: x.child_id )
168
+ name = get_name( child_base )
169
+ child_base.hier = make_hier( parent_hier , name )
170
+ child_base.save
171
+ hier_adjust( child_base )
148
172
  }
149
173
  end
150
174
 
175
+ # 配列で指定した階層のレベルを得る
151
176
  def get_level_by_array( hier_ary )
152
177
  level = hier_ary.size - 1
153
178
  level = 0 if level < 0
154
179
  level
155
180
  end
156
181
 
182
+ # 階層を表すデータ構造から階層の名前を得る
157
183
  def get_name( items_row )
158
184
  name = ""
159
185
  if items_row
@@ -162,34 +188,39 @@ module Arxutils
162
188
  name
163
189
  end
164
190
 
165
- def hier_adjust( item_row )
166
- parent_hier = item_row.name
167
- parent_num = item_row.id
191
+ # 階層を表すデータ構造で指定された階層の下部階層の名前を調整する
192
+ def hier_adjust( base )
193
+ parent_hier_string = base.__send__ @hier_symbol
194
+ parent_num = base.org_id
168
195
 
169
196
  tbl_rows = @hier_klass.where( parent_id: parent_num )
170
197
  if tbl_rows.size > 0
171
198
  tbl_rows.map{|x|
172
199
  child_num = x.child_id
173
- item_row = @base_klass.find( child_num )
174
- item_row.name = make_hier( parent_hier , get_name( item_row ) )
200
+ item_row = @base_klass.find_by( org_id: child_num )
201
+ item_row.hier = make_hier( parent_hier_string , get_name( item_row ) )
202
+ item_row.save
175
203
  hier_adjust( item_row )
176
204
  }
177
205
  end
178
206
  end
179
-
207
+
180
208
  # 指定項目と、その子のlevelを調整
181
209
  def level_adjust( row , parent_level )
182
210
  row.level = parent_level + 1
211
+ row.save
183
212
  child_rows = @hier_klass.where( parent_id: row.id )
184
213
  if child_rows.size > 0
185
214
  child_rows.map{ |x| level_adjust( x , row.level ) }
186
215
  end
187
- end
216
+ end
188
217
 
218
+ # IDで指定された階層のレベルを得る
189
219
  def get_level_by_child( num )
190
220
  @hier_klass.find_by( child_id: num ).level
191
221
  end
192
222
 
223
+ # 文字列で指定された親の階層の下の子の名前から、子の名前を作成
193
224
  def make_hier( parent_hier , name )
194
225
  [ parent_hier , name ].join('/')
195
226
  end
@@ -6,28 +6,38 @@ require 'active_support'
6
6
  require 'active_record'
7
7
  require 'pp'
8
8
 
9
+ # ActiveRecord用ユーティリティモジュール
9
10
  module Arxutils
11
+ ##
12
+ # migrateに必要なファイルをテンプレートから作成し、migarteを実行する
10
13
  class Migrate
11
- attr_accessor :dbinit , :dbconfig_dest_path , :dbconfig_dest_fname , :dbconfig_src_path , :dbconfig_src_fname
12
-
13
- def Migrate.migrate( data_ary , relation_def_fpath , module_name, count_classname_downcase , dbconfig , forced )
14
- src_config_dir = Arxutils.configdir
15
- mig = Migrate.new( Dbutil::MIGRATE_DIR , src_config_dir , dbconfig, Dbutil::DATABASELOG, forced )
16
- # dbconfigのテンプレートは内容が固定である。convertを呼び出し、Arxのインスタンスを作成するときに、適切なdata_aryの要素を与える必要がある(ただしテンプレートへの埋め込みには用いられない
17
- mig.make_dbconfig( dbconfig )
18
-
19
- data_ary.reduce(0) { |next_num , x|
20
- mig.make( next_num , x )
21
- }
14
+ # migrate用スクリプトファイル名の先頭の番号の間隔
15
+ FILENAME_COUNTER_STEP = 10
22
16
 
23
- content_array = data_ary.map { |x|
17
+ # migrateに必要なファイルをテンプレートから作成し、migarteを実行する
18
+ def self.migrate( db_dir , src_config_dir , log_fname, migrate_dir, env, db_scheme_ary , dbconfig , opts )
19
+ log_file_name = sprintf("%s-%s" , dbconfig.to_s , log_fname )
20
+ mig = Migratex.new( db_dir , migrate_dir , src_config_dir , dbconfig, env, log_file_name, opts )
21
+ # DB構成情報の生成
22
+ # dbconfigのテンプレートは内容が固定である。
23
+ if( opts["makeconfig"] )
24
+ mig.make_dbconfig( opts )
25
+ return
26
+ end
27
+ # スキーマ設定配列から、migrate用のスクリプトを作成する
28
+ db_scheme_ary.map{ |x| mig.make_script_group(x) }.flatten(1).each_with_index{|data , index|
29
+ idy = (index + 1) * FILENAME_COUNTER_STEP
30
+ mig.output_script( idy , *data )
31
+ }
32
+ # スキーマ設定配列から、relationのmigrate用のスクリプトの内容(ハッシュ形式)の配列を作成する
33
+ content_array = db_scheme_ary.map { |x|
24
34
  mig.make_relation( x , "count" )
25
35
  }.select{ |x| x.size > 0 }
26
- need_count_class_plural = content_array.reduce([]){ |s,x|
27
- s << x[:need_count_class_plural] if x[:need_count_class_plural] != nil
28
- s
29
- }
30
- if content_array.size > 0
36
+ # 複数形のクラス名を集める
37
+ need_count_class_plural = content_array.select{ |x| x[:need_count_class_plural] != nil }.map{ |x| x[:need_count_class_plural] }
38
+
39
+ # relationのmigrateが必要であれば、それをテンプレートファイルから作成して、スクリプトの内容として追加する
40
+ if content_array.find{|x| x != nil}
31
41
  data_count = {count_classname: "Count" ,
32
42
  need_count_class_plural: need_count_class_plural,
33
43
  }
@@ -36,88 +46,123 @@ module Arxutils
36
46
  ary.unshift( count_content )
37
47
  content_array = ary
38
48
  end
39
- File.open( relation_def_fpath , 'w' , {:encoding => Encoding::UTF_8}){ |f|
40
- f.puts("module #{module_name}")
41
- content_array.map{ |content|
42
- f.puts( content )
43
- f.puts( "\n" )
44
- }
45
- f.puts("end")
46
- }
47
-
48
- Dbutil::DbMgr.setup( mig.dbinit )
49
+ # relationのスクリプトを作成
50
+ mig.output_relation_script( content_array , opts[:relation] )
49
51
 
52
+ # データベース接続とログ設定
53
+ ::Arxutils::Dbutil::DbMgr.setup( mig.dbinit )
54
+
55
+ # migrateを実行する
50
56
  mig.migrate
51
57
  end
52
-
53
- def initialize( migrate_base_dir , config_dir , dbconfig, log_fname, forced = false )
54
- @dbinit = Dbutil::Dbinit.new( migrate_base_dir , config_dir , dbconfig, log_fname, forced )
55
- @dbconfig_dest_path = @dbinit.dbconfig_dest_path
56
- @dbconfig_src_path = @dbinit.dbconfig_src_path
57
- @dbconfig_src_fname = @dbinit.dbconfig_src_fname
58
-
59
- @migrate_dir = @dbinit.migrate_dir
60
- @src_path = Arxutils.templatedir
61
- @src_config_path = Arxutils.configdir
62
- end
63
58
 
64
- def convert_count_class_relation( data , src_fname )
65
- convert( data , @src_path , src_fname )
66
- end
59
+ # migrate用のスクリプトの内容をテンプレートから作成し、ファイルに出力し、migrateを実行する
60
+ class Migratex
61
+ # DB接続までの初期化を行うDbinitクラスのインスタンス
62
+ attr_reader :dbinit
67
63
 
68
- def convert( data , src_dir , src_fname )
69
- arx = Arx.new( data , File.join( src_dir, src_fname ) )
70
- arx.create
71
- end
72
-
73
- def make_dbconfig( data )
74
- content = convert( data , @src_config_path , @dbconfig_src_fname )
75
- File.open( @dbconfig_dest_path , 'w' , {:encoding => Encoding::UTF_8}){ |f|
76
- f.puts( content )
77
- }
78
- end
64
+ # migrate用のスクリプトの生成、migrateの実行を行うmigratexの生成
65
+ def initialize( db_dir , migrate_base_dir , src_config_dir , dbconfig, env, log_fname, opts )
66
+ # DB接続までの初期化を行うDbinitクラスのインスタンス
67
+ @dbinit = Dbutil::Dbinit.new( db_dir , migrate_base_dir , src_config_dir , dbconfig, env, log_fname, opts )
68
+ # 生成するDB構成情報ファイルパス
69
+ @dbconfig_dest_path = @dbinit.dbconfig_dest_path
70
+ # 参照用DB構成情報ファイル名
71
+ @dbconfig_src_fname = @dbinit.dbconfig_src_fname
79
72
 
80
- def make_relation( data , count_classname_downcase )
81
- if data[:classname_downcase] != count_classname_downcase
82
- data[:flist].reduce( { content: [], need_count_class: nil } ){ |s, x|
83
- case x
84
- when "base" , "noitem"
85
- name_base = "relation"
86
- data[:relation] = [] unless data[:relation]
87
- else
88
- data[:count_classname_downcase] = count_classname_downcase
89
- name_base = "relation_#{x}"
90
- s[:need_count_class_plural] ||= data[:plural]
91
- end
92
- content = convert( data , @src_path , "#{name_base}.tmpl" )
93
- s[:content] << content
94
- s
73
+ # migrate用スクリプトの出力先ディレクトリ名
74
+ @migrate_dir = @dbinit.migrate_dir
75
+ # テンプレートファイル格納ディレクトリ名
76
+ @src_path = Arxutils.templatedir
77
+ # 構成ファイル格納ディレクトリ
78
+ @src_config_path = Arxutils.configdir
79
+ end
80
+
81
+ # Countクラス用のrelationのスクリプトの内容に変換
82
+ def convert_count_class_relation( data , src_fname )
83
+ convert( data , @src_path , src_fname )
84
+ end
85
+
86
+ # テンプレートファイルからスクリプトの内容に変換
87
+ def convert( data , src_dir , src_fname )
88
+ arx = Arx.new( data , File.join( src_dir, src_fname ) )
89
+ # 指定テンプレートファイルからスクリプトの内容に作成
90
+ arx.create
91
+ end
92
+
93
+ # データベース構成ファイルをテンプレートから生成する
94
+ def make_dbconfig( data )
95
+ content = convert( data , @src_config_path , @dbconfig_src_fname )
96
+ File.open( @dbconfig_dest_path , 'w' , {:encoding => Encoding::UTF_8}){ |f|
97
+ f.puts( content )
95
98
  }
96
- else
97
- {}
98
99
  end
99
- end
100
-
101
- def make( next_num , data )
102
- data[:flist].reduce(next_num) do |idy , x|
103
- idy += 10
104
- content = convert( data , @src_path , "#{x}.tmpl" )
105
- case x
106
- when "base" , "noitem"
107
- additional = ""
100
+
101
+ # 英子文字で表現したクラス名が、countを表していなければ、relationを
102
+ # 英子文字で表現したクラス名が、countを表していれが、空のハッシュを返す
103
+ # スキーマでbase, noitem以外のフィールドが指定されていれば、そのフィールドに対するrelationの設定の内容を返す
104
+ def make_relation( data , count_classname_downcase )
105
+ if data[:classname_downcase] != count_classname_downcase
106
+ # 指定フィールドのフィールド名に対応したテンプレートファイルを用いて、relation設定を作成
107
+ data[:flist].reduce( { content: [], need_count_class: nil } ){ |s, field_name|
108
+ case field_name
109
+ when "base" , "noitem"
110
+ name_base = "relation"
111
+ # data[:relation]がnilに設定されていたら改めて空の配列を設定
112
+ data[:relation] = [] unless data[:relation]
113
+ else
114
+ data[:count_classname_downcase] = count_classname_downcase
115
+ name_base = "relation_#{field_name}"
116
+ s[:need_count_class_plural] ||= data[:plural]
117
+ end
118
+ # テンプレートファイルからスクリプトの内容を作成
119
+ content = convert( data , @src_path , "#{name_base}.tmpl" )
120
+ s[:content] << content
121
+ s
122
+ }
108
123
  else
109
- additional = x
124
+ {}
110
125
  end
111
- fname = File.join( @migrate_dir , sprintf("%03d_create_%s%s.rb" , idy , additional , data[:classname_downcase]) )
112
- File.open( fname , 'w' , {:encoding => Encoding::UTF_8}){ |f|
113
- f.puts( content )
126
+ end
127
+
128
+ # スキーマ設定からmigarte用スクリプトの内容を生成
129
+ def make_script_group( data )
130
+ data[:flist].map{ |kind| [kind, convert( data , @src_path , "#{kind}.tmpl" ), data[:classname_downcase]]}
131
+ end
132
+
133
+ # migrationのスクリプトをファイル出力する
134
+ def output_script( idy, kind , content , classname_downcase )
135
+ case kind
136
+ when "base" , "noitem"
137
+ additional = ""
138
+ else
139
+ additional = kind
140
+ end
141
+ fname = File.join( @migrate_dir , sprintf("%03d_create_%s%s.rb" , idy , additional , classname_downcase) )
142
+ File.open( fname , 'w' , {:encoding => Encoding::UTF_8}){ |f|
143
+ f.puts( content )
144
+ }
145
+ end
146
+
147
+ # relationのスクリプトをファイル出力する
148
+ def output_relation_script( content_array , opts )
149
+ dir = opts[:dir]
150
+ fname = opts[:filename]
151
+ fpath = File.join( dir , fname )
152
+ File.open( fpath , "w" ){ |file|
153
+ opts[:module].map{|mod| file.puts("module #{mod}")}
154
+ content_array.map{|x|
155
+ file.puts x
156
+ file.puts ""
157
+ }
158
+ opts[:module].map{|mod| file.puts("end")}
114
159
  }
115
- idy
116
160
  end
117
- end
118
161
 
119
- def migrate
120
- ActiveRecord::Migrator.migrate(@migrate_dir , ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
162
+ # migrateを実行する
163
+ def migrate
164
+ ActiveRecord::Migrator.migrate(@migrate_dir , ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
165
+ end
121
166
  end
122
167
  end
123
168
  end
@@ -1,51 +1,68 @@
1
1
  module Arxutils
2
+ # 簡易的なトランザクション処理
2
3
  class TransactState
3
- attr_accessor :ids , :state
4
-
4
+ # 対象ID群
5
+ attr_reader :ids
6
+ # 状態
7
+ attr_accessor :state
8
+
9
+ #初期化
5
10
  def initialize
11
+ # 対象ID群
6
12
  @ids = []
13
+ # 状態
7
14
  @state = :NONE
8
15
  end
9
16
 
17
+ # :TRACE状態の時のみ対象IDとして追加
10
18
  def add( xid )
11
19
  @ids << xid if @state == :TRACE
12
20
  end
13
21
 
22
+ # 対象ID群をクリア
14
23
  def clear
15
24
  @ids = []
16
25
  end
17
26
 
27
+ # 処理の必要性の確認
18
28
  def need?
19
29
  @ids.size > 0
20
30
  end
21
31
 
22
32
  end
23
33
 
34
+ # 複数の簡易的なトランザクション処理
24
35
  class TransactStateGroup
36
+ # 初期化
25
37
  def initialize( *names )
26
38
  @state = :NONE
27
39
  @inst = {}
28
40
  names.map{|x| @inst[x] = TransactState.new }
29
41
  end
30
-
42
+
43
+ # 処理の必要性の確認
31
44
  def need?
32
45
  @state != :NONE
33
46
  end
34
-
47
+
48
+ # 状態の一括設定
35
49
  def set_all_inst_state
36
50
  @inst.map{|x| x[1].state = @state }
37
51
  end
38
-
52
+
53
+ # 状態を:TRACEに一括設定
39
54
  def trace
40
55
  @state = :TRACE
41
56
  set_all_inst_state
42
57
  end
43
-
58
+
59
+ # 状態を:NONEに一括設定
44
60
  def reset
45
61
  @state = :NONE
46
62
  set_all_inst_state
47
63
  end
48
-
64
+
65
+ # 指定名の状態を返す
49
66
  def method_missing(name , lang = nil)
50
67
  @inst[name]
51
68
  end
@@ -1,3 +1,4 @@
1
1
  module Arxutils
2
- VERSION = "0.1.36"
2
+ # バージョン番号
3
+ VERSION = "0.1.37"
3
4
  end
@@ -1,5 +1,5 @@
1
- # SQLite version 3.x
2
- # gem install sqlite3
1
+ # MySQL
2
+ # gem install mysql2
3
3
  #
4
4
  # Ensure the SQLite 3 gem is defined in your Gemfile
5
5
  # gem 'sqlite3'
@@ -7,7 +7,7 @@
7
7
  default_env: &default
8
8
  adapter: mysql2
9
9
  host: localhost
10
- username: root
10
+ username:
11
11
  password:
12
12
  database: <%= @data[:dbname] %>
13
13
  pool: 5
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,3 +1,3 @@
1
1
  class <%= @data[:classname] %> < ActiveRecord::Base
2
2
  <% @data[:relation].each do |x| %> <%= x %>
3
- <% end %> end
3
+ <% end %> end
@@ -1,4 +1,3 @@
1
1
  class <%= @data[:count_classname] %> < ActiveRecord::Base
2
2
  <% @data[:need_count_class_plural].each do |x| %> has_many :invalid<%= x %>
3
3
  <% end %> end
4
-
@@ -1,3 +1,3 @@
1
1
  class Current<%= @data[:classname_downcase] %> < ActiveRecord::Base
2
2
  belongs_to :<%= @data[:classname_downcase] %> , foreign_key: 'org_id'
3
- end
3
+ end
@@ -1,4 +1,4 @@
1
1
  class Invalid<%= @data[:classname_downcase] %> < ActiveRecord::Base
2
2
  belongs_to :<%= @data[:classname_downcase] %> , foreign_key: 'org_id'
3
3
  belongs_to :<%= @data[:count_classname_downcase] %> , foreign_key: '<%= @data[:count_field] %>'
4
- end
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arxutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.36
4
+ version: 0.1.37
5
5
  platform: ruby
6
6
  authors:
7
7
  - yasuo kominami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-29 00:00:00.000000000 Z
11
+ date: 2019-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '4.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '4.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sqlite3
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mysql2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.4.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.4.1
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: encx
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -133,10 +147,6 @@ files:
133
147
  - lib/arxutils/dbutil/dbmgr.rb
134
148
  - lib/arxutils/hier.rb
135
149
  - lib/arxutils/migrate.rb
136
- - lib/arxutils/misc.rb
137
- - lib/arxutils/store.rb
138
- - lib/arxutils/store/storecsv.rb
139
- - lib/arxutils/store/storedb.rb
140
150
  - lib/arxutils/transactstate.rb
141
151
  - lib/arxutils/version.rb
142
152
  - lib/config/mysql.tmpl
@@ -168,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
178
  version: '0'
169
179
  requirements: []
170
180
  rubyforge_project:
171
- rubygems_version: 2.5.2
181
+ rubygems_version: 2.7.6
172
182
  signing_key:
173
183
  specification_version: 4
174
184
  summary: utility functions for ActiveRecord.
@@ -1,44 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- module Arxutils
4
- def load_file( in_file )
5
- File.open( in_file , "r" , { :encoding => 'UTF-8' } )
6
- end
7
-
8
- def normalize_to_integer( *args )
9
- args.map{ |x|
10
- if x != nil and x !~ /^\s*$/
11
- x.to_i
12
- else
13
- nil
14
- end
15
- }
16
- end
17
-
18
- def update_integer( model , hs )
19
- value_hs = hs.reduce({}){ |hsx,item|
20
- val = model.send(item[0])
21
- if val == nil or val < item[1]
22
- hsx[ item[0] ] = item[1]
23
- end
24
- hsx
25
- }
26
- if value_hs.size > 0
27
- begin
28
- model.update(value_hs)
29
- rescue => ex
30
- puts ex.message
31
- end
32
- end
33
- end
34
-
35
- def capture_queries(&block)
36
- queries = []
37
- ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload|
38
- queries << payload
39
- end
40
- yield block
41
- ActiveSupport::Notifications.unsubscribe('sql.active_record')
42
- queries
43
- end
44
- end
@@ -1,22 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'arxutils/store/storedb'
3
- require 'arxutils/store/storecsv'
4
-
5
- module Arxutils
6
- class Store
7
- extend Forwardable
8
-
9
- def Store.init( kind , hs , &block )
10
- case kind
11
- when :db
12
- obj = StoreDb.init( hs , block )
13
- when :csv
14
- obj = StoreCsv.new( hs )
15
- else
16
- obj = nil
17
- end
18
-
19
- obj
20
- end
21
- end
22
- end
@@ -1,18 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'csv'
3
-
4
- module Arxutils
5
- class Store
6
- class StoreCsv
7
- def initialize( hs )
8
- @csv = CSV.open( hs[:csv_fname] , "w" ,
9
- { :encoding => hs[:encoding],
10
- :headers => hs[:headers],
11
- :force_quotes => hs[:force_quotes],
12
- :write_headers => hs[:write_headers],
13
- } )
14
- end
15
- end
16
- end
17
- end
18
-
@@ -1,25 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'arxutils'
3
-
4
- module Arxutils
5
- class Store
6
- class StoreDb
7
- def StoreDb.init( hs , block = nil )
8
- ret = nil
9
- register_time = Dbutil::DbMgr.init( hs["db_dir"], hs["migrate_dir"] , hs["config_dir"], hs["dbconfig"] , hs["env"] , hs["log_fname"] )
10
-
11
- if block
12
- ret = block.call( register_time )
13
- end
14
- ret
15
- end
16
- # hs
17
- # :csv_fname
18
- # :mode
19
- # :encoding => 'UTF-8',
20
- # :headers => @headers_s,
21
- # :force_quotes => true,
22
- # :write_headers => true,
23
- end
24
- end
25
- end