Class ConfigOutput
In: app/models/config_output.rb
Parent: ActiveRecord::Base

出力ファイルの設定のモデル

Methods

clear   file_name   generate   headers   iconv   mime_type   process_eol   quotation   quote   rows   separator  

Constants

LABEL_FILE_FORMAT = [ [N_("ConfigOutput|file_format|CSV"), "csv"], [N_("ConfigOutput|file_format|PDF"), "pdf"], [N_("ConfigOutput|file_format|XML"), "xml"], ]
LABEL_EOL_PATTERN = [ [N_("ConfigOutput|eol_pattern|none"), "none"], [N_("ConfigOutput|eol_pattern|disabled"), "disabled"], [N_("ConfigOutput|eol_pattern|specified"), "specified"], ]
LABEL_SEPARATOR_PATTERN = [ [N_("ConfigOutput|separator_pattern|tab"), "tab"], [N_("ConfigOutput|separator_pattern|comma"), "comma"], [N_("ConfigOutput|separator_pattern|semicolon"), "semicolon"], [N_("ConfigOutput|separator_pattern|specified"), "specified"], ]
LABEL_QUOTATION_PATTERN = [ [N_("ConfigOutput|quotation|none"), "none"], [N_("ConfigOutput|quotation|single"), "single"], [N_("ConfigOutput|quotation|double"), "double"], [N_("ConfigOutput|quotation|specified"), "specified"], ]
LABEL_ENCODING = [ [N_("ConfigOutput|encoding|UTF-8"), "utf-8"], [N_("ConfigOutput|encoding|Shift_JIS"), "shift_jis"], ]

Public Instance methods

設定を初期化する。

[Source]

     # File app/models/config_output.rb, line 130
130:   def clear
131:     self.attributes = {
132:       :name => display_to_list.name,
133:       :file_format => "csv",
134:       :eol_pattern => "none",
135:       :eol_parameter => "", 
136:       :separator_pattern => "tab",
137:       :separator_parameter => "",
138:       :quotation_pattern => "none",
139:       :quotation_parameter => "",
140:       :encoding => "utf-8",
141:     }
142:   end

ファイル名を返す。

[Source]

     # File app/models/config_output.rb, line 310
310:   def file_name(user_agent)
311:     if user_agent =~ /MSIE/
312:       conv = Iconv.new('Shift_JIS', 'UTF-8') # FIXME: works only in japanese
313:       return "#{conv.iconv(name)}.#{file_format}"
314:     end
315:     return "#{iconv(name)}.#{file_format}"
316:   end

出力を生成する。

[Source]

     # File app/models/config_output.rb, line 185
185:   def generate(options={})
186:     case file_format
187:     when "csv"
188:       require "csv"
189:       result = ''
190:       CSV::Writer.generate(result, separator, "\r\n") do |csv|
191:         csv << iconv(headers)
192:         rows(options).each do |row|
193:           csv << iconv(row)
194:         end
195:       end
196:       return result
197:     when "pdf"
198:       header = SimplePDF::TextBox.new("%t", 8,
199:                                       :align => :left,
200:                                       :top_margin => 5.0,
201:                                       :left_margin => 10.0,
202:                                       :right_margin => 10.0)
203:       footer = SimplePDF::TextBox.new("%p/%P", 8,
204:                                       :align => :center,
205:                                       :top_margin => -20.0,
206:                                       :left_margin => 10.0,
207:                                       :right_margin => 10.0)
208: 
209:       table_header_style = SimpleCellStyle.new(:border_color => [0, 0, 0],
210:                                                :text_color => [0, 0, 0],
211:                                                :fill_color => [200, 200, 200],
212:                                                :top_margin => 2.0,
213:                                                :bottom_margin => 2.0,
214:                                                :left_margin => 2.0,
215:                                                :right_margin => 2.0,
216:                                                :align => :center,
217:                                                :font_size => 20.0,
218:                                                :line_height => 8.0)
219:       table_cell_style =  SimpleCellStyle.new(:border_color => [0, 0, 0],
220:                                               :text_color => [0, 0, 0],
221:                                               :fill_color => nil,
222:                                               :top_margin => 2.0,
223:                                               :bottom_margin => 2.0,
224:                                               :left_margin => 2.0,
225:                                               :right_margin => 2.0,
226:                                               :align => :left,
227:                                               :font_size => 20.0,
228:                                               :line_height => 10.0)
229: 
230:       contents = [headers.map {|h| {:data => h, :style => table_header_style}}]
231:       rows(options, :link => true).each do |row|
232:         contents << row.map {|x| x.update(:style => table_cell_style)}
233:       end
234:       table = SimpleTable.new(contents.size, headers.size)
235:       table.set_contents do |row, col|
236:         SimpleCell.new(contents[row][col][:data], contents[row][col][:style], contents[row][col][:link])
237:       end
238:       
239:       pdf = SimpleTablePDF.new(:orientation => "Portrait",
240:                                :format => "A4",
241:                                :language => user.person.last_language,
242:                                :title => name,
243:                                :author => "PjC",
244:                                :creator => "PjC",
245:                                :keywords => ["PjC", "テスト"], # FIXME
246:                                :subject => "サブタイトル", # FIXME
247:                                :zoom => "fullpage",
248:                                :header => header,
249:                                :footer => footer,
250:                                :table => table,
251:                                :top_margin => 25.0, # mm
252:                                :left_margin => 20.0, # mm
253:                                :right_margin => 20.0, # mm
254:                                :bottom_margin => 30.0, # mm
255:                                :page_order => 0)
256: 
257:       return pdf.output
258:     when "xml"
259:       require "rexml/document"
260:       doc = REXML::Document.new "<table></table>"
261:       root = doc.root
262: 
263:       thead = REXML::Element.new "thead"
264:       tr = REXML::Element.new "tr"
265:       headers.each do |header|
266:         th = REXML::Element.new "th"
267:         th.text = header
268:         tr.add_element th
269:       end
270:       thead.add_element tr
271:       root.add_element thead
272: 
273:       tbody = REXML::Element.new "tbody"
274:       rows(options).each do |row|
275:         tr = REXML::Element.new "tr"
276:         row.each do |d|
277:           td = REXML::Element.new "td"
278:           td.text = process_eol(d.to_s)
279:           tr.add_element td
280:         end
281:         tbody.add_element tr
282:       end
283:       root.add_element tbody
284: 
285:       doc.xml_decl.encoding = encoding
286:       output = ''
287:       doc.write output
288: 
289:       return output
290:     else
291:       raise ArgumentError, "invaild file_format: #{file_format}"
292:     end
293:   end

表示項目に対応するヘッダを返す。

[Source]

     # File app/models/config_output.rb, line 157
157:   def headers
158:     result = []
159:     each_item {|config_output_item| result << config_output_item.name}
160:     result
161:   end

x のエンコーディングを設定に合わせて変換する。

[Source]

     # File app/models/config_output.rb, line 145
145:   def iconv(x)
146:     return x if file_format == "pdf"
147:     return x if encoding == "utf-8"
148:     return x.map {|e| iconv(e)} if x.is_a?(Array)
149:     if x.is_a?(String)
150:       @iconverter ||= Iconv.new(encoding, "UTF-8")
151:       return @iconverter.iconv(x)
152:     end
153:     return x
154:   end

出力形式に合わせた MIME type を返す。

[Source]

     # File app/models/config_output.rb, line 296
296:   def mime_type
297:     case file_format
298:     when "csv"
299:       "text/csv; charset=#{encoding}; header=present"
300:     when "pdf"
301:       "application/pdf"
302:     when "xml"
303:       "text/xml; charset=#{encoding}"
304:     else
305:       raise ArgumentError, "invalid file_format: #{file_format}"
306:     end
307:   end

行末文字を返す。

[Source]

    # File app/models/config_output.rb, line 75
75:   def process_eol(x)
76:     case eol_pattern
77:     when "none"
78:       x
79:     when "disabled"
80:       x.gsub(/\r?\n/, '')
81:     when "specified"
82:       x.gsub(/\r?\n/, eol_parameter)
83:     else
84:       raise ArgumentError, "invalid eol_pattern; #{eol_pattern}"
85:     end
86:   end

(CSV 形式で)値を囲む引用符を返す。

[Source]

     # File app/models/config_output.rb, line 105
105:   def quotation
106:     case quotation_pattern
107:     when "none"
108:       ""
109:     when "single"
110:       "'"
111:     when "double"
112:       '"'
113:     when "specified"
114:       quotation_parameter
115:     else
116:       raise ArgumentError, "invalid quotation_pattern: #{quotation_pattern}"
117:     end
118:   end

(CSV 形式で)引用符で囲まれた値を返す。

[Source]

     # File app/models/config_output.rb, line 121
121:   def quote(x)
122:     q = quotation
123:     y = process_eol(x)
124:     return y if q.blank?
125:     z = y.gsub(q, "#{q}#{q}")
126:     return "#{q}#{z}#{q}"
127:   end

出力対象の行の一覧を返す。

[Source]

     # File app/models/config_output.rb, line 164
164:   def rows(options, data_options={})
165:     result = []
166:     display_to_list.model_class.find(:all, options).each do |it|
167:       row = []
168:       each_item do |config_output_item|
169:         begin
170:           if data_options[:link]
171:             row << {:data => config_output_item.to_data(it), :link => config_output_item.link_url(it)}
172:           else
173:             row << config_output_item.to_data(it)
174:           end
175:         rescue NoMethodError
176:           # ignored
177:         end
178:       end
179:       result << row
180:     end
181:     result
182:   end

(CSV 形式で)値を区切る文字を返す。

[Source]

     # File app/models/config_output.rb, line 89
 89:   def separator
 90:     case separator_pattern
 91:     when "tab"
 92:       "\t"
 93:     when "comma"
 94:       ","
 95:     when "semicolon"
 96:       ";"
 97:     when "specified"
 98:       separator_parameter
 99:     else
100:       raise ArgumentError, "invalid separator_pattern: #{separator_pattern}"
101:     end
102:   end

[Validate]