takenoko 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1bb80ebb114ac3ee473ee833d63d9436dbf25ea9
4
- data.tar.gz: 76dc1e87a834c27e407dff4180b5a0ea8f2590cf
3
+ metadata.gz: 9d15b5e30e05ec7639868f1061f6c04b340e837c
4
+ data.tar.gz: 7e31a25c1fbe2c5d850e59098bda414f5f473271
5
5
  SHA512:
6
- metadata.gz: 7510fcbbbc44c414a0a5e0c3b25139be5ee11b340ee6b66e30a6592018fcf96696e3a7aac28b55931817c639adc48af709412dbf8473f3f6801d8f5b928f371f
7
- data.tar.gz: 0fb1c6bfce088a274b00840b10d4fd6d14e0dc539f14a7435cdd994e2b2a36ad0e5d382dcc545613e413cd8b3b9b7cd1ad0ebecaf49bb8760d894f84cd60d854
6
+ metadata.gz: 4456cdcf48a2bf37b961b655ff9ce561c2c0fdf6cd2a3cfa708377a2d07b9086efb60c3ee303aa2feee1ece66b09d57e326240523d390d1a83925b197b2cca87
7
+ data.tar.gz: 7d9b34e206e3edb024c5e6c44174c9067656a3c397c9d8dcc6da8bd9ab4972420583d29193950507a1aba0700f7c0a969e930f2cc92db6262466aae7821fb6b2
data/lib/takenoko.rb CHANGED
@@ -48,10 +48,10 @@ module Takenoko
48
48
  mattr_accessor :folder_id
49
49
  @@folder_id = nil
50
50
 
51
-
52
51
  require 'takenoko/exporter'
53
52
  require 'takenoko/google_client'
54
53
  require 'takenoko/attach_helper'
54
+ require 'takenoko/mapping_generator'
55
55
 
56
56
  def config
57
57
  yield self
@@ -62,14 +62,8 @@ module Takenoko
62
62
  return @@google_client
63
63
  end
64
64
 
65
- def check_config
66
- raise "Must specify mapping_file or sheet_id" unless (@@mapping_file || @@sheet_id)
67
- raise "file not found:#{@@mapping_file}" if @@mapping_file && !::File.exist?(@@mapping_file)
68
- return true
69
- end
70
-
71
65
  def mapping_config
72
- (@@always_reload && generate_base_config) || @@mapping_config || generate_base_config
66
+ MappingGenerator.generate
73
67
  end
74
68
 
75
69
  def table_config(table_name)
@@ -110,72 +104,6 @@ module Takenoko
110
104
  end
111
105
  end
112
106
 
113
- def generate_base_config
114
- return unless check_config
115
-
116
- conf = HashWithIndifferentAccess.new
117
-
118
- if @@sheet_id
119
- sheet_conf = HashWithIndifferentAccess.new({tables: {}})
120
- google_client.spreadsheet.worksheets.each do |ws|
121
- next if ws.title.match(/\s*#.*/)
122
- sheet_conf[:tables][ws.title] = {
123
- worksheet_id: ws.gid,
124
- worksheet: ws.title
125
- }
126
- end
127
- conf.deep_merge!(sheet_conf)
128
- end
129
-
130
- if @@mapping_file
131
- file_conf = YAML.load_file(@@mapping_file).with_indifferent_access
132
- raise "tables not exists" if file_conf[:tables].blank?
133
- conf.deep_merge!(file_conf)
134
- end
135
-
136
- conf[:tables].compact!
137
- conf[:tables].each do |t, v|
138
- unless conf[:tables][t]
139
- conf[:tables][t] = Hash.new
140
- end
141
- table = conf[:tables][t]
142
- raise "#{sheet_id} cannot be blank" unless table[:sheet_id] ||= @@sheet_id
143
- table[:worksheet] = t if table[:worksheet].blank? && table[:worksheet_id].blank?
144
- table[:find_column] = table[:find_column] || :id
145
- table_name = table[:table_name] = t.pluralize if table[:table_name].blank?
146
- table[:class_name] = table[:class_name] || (table_name && table_name.singularize.camelize) || table[:table_name].singularize.camelize
147
- [
148
- :allow_overwrite,
149
- :truncate_all_data,
150
- :file_extension,
151
- :export_file_location,
152
- :enable_postprocess,
153
- :postprocess_class,
154
- :folder_id
155
- ].each do |f|
156
- table[f] = class_variable_get("@@" + f.to_s) unless table.key?(f)
157
- end
158
-
159
- if table[:enable_postprocess] && table[:postprocess_class].blank?
160
- table[:postprocess_class] = table[:class_name]
161
- end
162
-
163
- raise "Not support file extension: #{table[:file_extension]}" unless SUPPORTED_FILE_EXT.include?(table[:file_extension])
164
-
165
- table[:download_location] = table[:download_location] || (@@download_location && (@@download_location +"/"+table[:table_name]))
166
- if attach_files = table[:attach_files].present?
167
- attach_files = table[:attach_files]
168
- attach_files = attach_files.map do |col|
169
- raise "column_name must be set" unless col[:column_name]
170
- col[:download_location] = col[:download_location].present? ? table[:download_location] + "/" + col[:download_location] : table[:download_location]
171
- raise "folder_id should be set" unless col[:folder_id] = col[:folder_id] || table[:folder_id]
172
- end
173
- end
174
- end
175
- @@mapping_config = conf
176
- end
177
- alias reload_config! generate_base_config
178
-
179
107
  class Railtie < Rails::Railtie
180
108
  railtie_name :takenoko
181
109
 
@@ -0,0 +1,113 @@
1
+ module Takenoko
2
+ module MappingGenerator
3
+ extend self
4
+ @@mapping_conf = nil
5
+ ALLOWED_OVERWRITE_FIELD = [
6
+ :allow_overwrite,
7
+ :truncate_all_data,
8
+ :file_extension,
9
+ :export_file_location,
10
+ :enable_postprocess,
11
+ :postprocess_class,
12
+ :folder_id
13
+ ]
14
+
15
+ FILTER = [
16
+ [:set_worksheet, ->(name, table) {
17
+ table[:worksheet] = name if table[:worksheet].blank? && table[:worksheet_id].blank?
18
+ }],
19
+
20
+ [:set_sheetid, ->(name, table){
21
+ raise "#{sheet_id} cannot be blank" unless table[:sheet_id] ||= Takenoko.sheet_id
22
+ }],
23
+
24
+ [:set_find_column, ->(name, table){
25
+ table[:find_column] ||= :id
26
+ }],
27
+
28
+ [:set_tablename, ->(name, table){
29
+ table[:table_name] = name.pluralize if table[:table_name].blank?
30
+ }],
31
+
32
+ [:set_class_name, ->(name, table){
33
+ table[:class_name] ||= table[:table_name] && table[:table_name].singularize.camelize
34
+ }],
35
+
36
+ [:overwrite_global_config, ->(name, table){
37
+ ALLOWED_OVERWRITE_FIELD.each do |f|
38
+ table[f] = Takenoko.public_send(f) unless table.key?(f)
39
+ end
40
+ }],
41
+
42
+ [:set_postprocess_class, ->(name, table){
43
+ table[:postprocess_class] ||= table[:class_name]
44
+ }],
45
+
46
+ [:check_file_extension, ->(name, table){
47
+ raise "Not support file extension: #{table[:file_extension]}" unless SUPPORTED_FILE_EXT.include?(table[:file_extension])
48
+ }],
49
+
50
+ [:set_download_location, ->(name, table){
51
+ table[:download_location] ||= Takenoko.download_location && (Takenoko.download_location + "/" + table[:table_name])
52
+ }],
53
+
54
+ [:set_attach_files, ->(name, table){
55
+ if (attach_files = table[:attach_files]).present?
56
+ attach_files = attach_files.map do |col|
57
+ raise "column_name must be set" unless col[:column_name]
58
+ raise "folder_id should be set" unless col[:folder_id] ||= table[:folder_id]
59
+ col[:download_location] ||= col[:download_location].present? ? table[:download_location] + "/" + col[:download_location] : table[:download_location]
60
+ end
61
+ end
62
+ }],
63
+ ]
64
+
65
+ def generate
66
+ return @@mapping_conf if Takenoko.allow_overwrite && @@mapping_conf
67
+ @@mapping_conf = base_config
68
+ @@mapping_conf[:tables].each do |name, table|
69
+ FILTER.each do |f|
70
+ f[1].call(name, table)
71
+ end
72
+ end
73
+ return @@mapping_conf
74
+ end
75
+
76
+ private
77
+
78
+ def check_config
79
+ raise "Must specify mapping_file or sheet_id" unless (Takenoko.mapping_file || Takenoko.sheet_id)
80
+ raise "file not found:#{Takenoko.mapping_file}" if Takenoko.mapping_file && !::File.exist?(Takenoko.mapping_file)
81
+ return true
82
+ end
83
+
84
+ def spread_sheet_config
85
+ sheet_conf = HashWithIndifferentAccess.new({tables: {}})
86
+ return sheet_conf unless Takenoko.sheet_id
87
+ Takenoko.google_client.spreadsheet.worksheets.each do |ws|
88
+ next if ws.title.match(/\s*#.*/)
89
+ sheet_conf[:tables][ws.title] = {
90
+ worksheet_id: ws.gid,
91
+ worksheet: ws.title
92
+ }
93
+ end
94
+ sheet_conf
95
+ end
96
+
97
+ def mapping_file_config
98
+ return {} unless Takenoko.mapping_file
99
+ file_conf = YAML.load_file(Takenoko.mapping_file).with_indifferent_access
100
+ raise "tables not exists" if file_conf[:tables].blank?
101
+ file_conf
102
+ end
103
+
104
+ def base_config
105
+ check_config
106
+ spread_sheet_config
107
+ conf = spread_sheet_config.deep_merge(mapping_file_config).with_indifferent_access
108
+ conf[:tables].compact!
109
+ conf
110
+ end
111
+
112
+ end
113
+ end
@@ -1,3 +1,3 @@
1
1
  module Takenoko
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: takenoko
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - KhiemNS
@@ -83,11 +83,12 @@ files:
83
83
  - lib/takenoko/attach_helper.rb
84
84
  - lib/takenoko/exporter.rb
85
85
  - lib/takenoko/google_client.rb
86
+ - lib/takenoko/mapping_generator.rb
86
87
  - lib/takenoko/version.rb
87
88
  - lib/tasks/takenoko.rake
88
89
  - test/takenoko_test.rb
89
90
  - test/test_helper.rb
90
- homepage: https://github.com/khiemns54/takenoko/tree/release/0.2.0
91
+ homepage: https://github.com/khiemns54/takenoko/tree/release/0.2.1
91
92
  licenses: []
92
93
  metadata: {}
93
94
  post_install_message: