mymatrix 0.0.2 → 0.0.3
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.
- data/lib/mymatrix/version.rb +1 -1
- data/lib/mymatrix.rb +8 -213
- metadata +5 -5
data/lib/mymatrix/version.rb
CHANGED
data/lib/mymatrix.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
#!/usr/bin/ruby -Ku
|
2
2
|
# -*- encoding: utf-8 -*-
|
3
3
|
require "mymatrix/version"
|
4
|
+
require 'loader_factory'
|
5
|
+
|
4
6
|
|
5
7
|
$:.unshift(File.dirname(__FILE__)) unless
|
6
8
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
7
9
|
|
8
10
|
require 'rubygems'
|
9
|
-
require 'spreadsheet'
|
10
11
|
require 'nkf'
|
11
12
|
require 'logger'
|
12
13
|
require 'pp'
|
@@ -17,8 +18,6 @@ if(RUBY_VERSION =~ /1\.[^9]/)
|
|
17
18
|
end
|
18
19
|
|
19
20
|
class MyMatrix
|
20
|
-
# VERSION = '0.0.1'
|
21
|
-
|
22
21
|
attr_accessor :file, :internal_lf, :mx
|
23
22
|
include Enumerable
|
24
23
|
#to_t()の際のセパレータ。
|
@@ -30,19 +29,6 @@ class MyMatrix
|
|
30
29
|
# ====Return
|
31
30
|
# 生成されたMyMatrixオブジェクト
|
32
31
|
def initialize(file=nil, opts={})
|
33
|
-
#platform check
|
34
|
-
if(RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/)
|
35
|
-
$mymatrix_filesystem = 's'
|
36
|
-
elsif(RUBY_PLATFORM.downcase =~ /darwin/)
|
37
|
-
$mymatrix_filesystem = 'm'
|
38
|
-
elsif(RUBY_PLATFORM.downcase =~ /linux/)
|
39
|
-
$mymatrix_filesystem = 'u'
|
40
|
-
else
|
41
|
-
$mymatrix_filesystem = 'u'
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
|
46
32
|
#内部改行コード。
|
47
33
|
@internal_lf = '<br>'
|
48
34
|
rnd = rand(9999)
|
@@ -57,31 +43,9 @@ class MyMatrix
|
|
57
43
|
@file = file
|
58
44
|
|
59
45
|
@mx = []
|
60
|
-
|
61
|
-
@mx = makeMatrixFromXLS(@file, opts)
|
62
|
-
elsif(@file =~ /(\.tsv|\.txt|\.TSV|\.TXT)/)
|
63
|
-
@mx = makeMatrixFromTSV(@file, opts)
|
64
|
-
elsif(@file =~ /(\.csv|\.CSV)/)
|
65
|
-
#opts[:sep] = ','
|
66
|
-
#@mx = makeMatrixFromTSV(@file, opts)
|
67
|
-
@mx = makeMatrixFromCSV(@file, opts)
|
46
|
+
@mx = LoaderFactory.load(@file, opts)
|
68
47
|
|
69
|
-
# elsif(@file =~ /\.mmx$/)
|
70
|
-
# readmx = Marshal.load(open(@file).read)
|
71
|
-
# return readmx
|
72
|
-
elsif(@file == nil)
|
73
|
-
else
|
74
|
-
#デフォルトはTSVで読み込むようにする。
|
75
|
-
@mx = makeMatrixFromTSV(@file, opts)
|
76
|
-
end
|
77
48
|
|
78
|
-
#@mxの末尾に空レコードが入っていたら、その空白を削除
|
79
|
-
while(@mx[@mx.size-1] && @mx[@mx.size-1].join == '')
|
80
|
-
@mx.pop
|
81
|
-
end
|
82
|
-
if(@mx.size == 0)
|
83
|
-
@mx = []
|
84
|
-
end
|
85
49
|
@headers = @mx.shift
|
86
50
|
registerMatrix
|
87
51
|
return self
|
@@ -100,8 +64,6 @@ class MyMatrix
|
|
100
64
|
str = MyMatrix.cp932ize(str)
|
101
65
|
out = str.encode("Windows-31J")
|
102
66
|
end
|
103
|
-
|
104
|
-
|
105
67
|
return out
|
106
68
|
end
|
107
69
|
# 外部ファイルエンコード(CP932)を内部エンコード(UTF8)に変換する
|
@@ -121,24 +83,6 @@ class MyMatrix
|
|
121
83
|
out = str
|
122
84
|
return out
|
123
85
|
end
|
124
|
-
# ファイルオープン時、パス文字列のエンコードを変換してシステムに返却するためのメソッド
|
125
|
-
def encodePath(path)
|
126
|
-
case $mymatrix_filesystem
|
127
|
-
when 'u'
|
128
|
-
#utf8=>utf8なので何もしない
|
129
|
-
#path = MyMatrix.toutf8(path)
|
130
|
-
#path.encode('UTF-8')
|
131
|
-
path
|
132
|
-
when 's'
|
133
|
-
path = MyMatrix.tosjis(path)
|
134
|
-
#path.encode('Windows-31J')
|
135
|
-
when 'w'
|
136
|
-
path = MyMatrix.tosjis(path)
|
137
|
-
#path.encode('Windows-31J')
|
138
|
-
when 'm'
|
139
|
-
path = MyMatrix.toUtf8Mac(path)
|
140
|
-
end
|
141
|
-
end
|
142
86
|
|
143
87
|
#--
|
144
88
|
#protected methods
|
@@ -174,113 +118,7 @@ class MyMatrix
|
|
174
118
|
end
|
175
119
|
end
|
176
120
|
end
|
177
|
-
# xls読み込みメソッド
|
178
|
-
def makeMatrixFromXLS(xlsFile, opts={})
|
179
|
-
if(opts)
|
180
|
-
offset = opts[:offset]
|
181
|
-
end
|
182
|
-
offset ||= 0
|
183
|
-
|
184
|
-
out = []
|
185
|
-
#todo xlsFileがなかったら作成
|
186
|
-
encodePath(xlsFile)
|
187
|
-
xl = Spreadsheet.open(encodePath(xlsFile), 'rb')
|
188
|
-
sheet = xl.worksheet(0)
|
189
|
-
rowsize = sheet.last_row_index
|
190
|
-
(rowsize+1-offset).times do |i|
|
191
|
-
row = sheet.row(i+offset)
|
192
|
-
orow = []
|
193
|
-
row.each do |ele|
|
194
|
-
#様々な型で値が入っている。改行も入っている
|
195
|
-
if(ele.class == Float)&&(ele.to_s =~ /(\d+)\.0/)
|
196
|
-
ele = $1
|
197
|
-
end
|
198
|
-
if(ele.class == Spreadsheet::Formula)
|
199
|
-
ele = ele.value
|
200
|
-
end
|
201
|
-
if(ele == nil)
|
202
|
-
ele = ''
|
203
|
-
end
|
204
|
-
ele = ele.to_s.gsub(/\n/, '<br>')
|
205
|
-
orow << ele
|
206
|
-
end
|
207
|
-
out << orow
|
208
|
-
end
|
209
|
-
|
210
|
-
return out
|
211
|
-
end
|
212
|
-
#TSV: tab separated value 読み込みメソッド
|
213
|
-
def makeMatrixFromTSV(file, opts={:sep=>SEPARATOR, :offset=>0})
|
214
|
-
out = []
|
215
|
-
epath = encodePath(file)
|
216
|
-
if(!File.exist?(epath))
|
217
|
-
open(epath, 'w') do |fo|
|
218
|
-
fo.print("\n\n")
|
219
|
-
end
|
220
|
-
end
|
221
|
-
#fi = open(file.encode('Windows-31J'), "r:Windows-31J")
|
222
|
-
fi = open(encodePath(file), "r:Windows-31J")
|
223
|
-
if(opts[:offset])
|
224
|
-
opts[:offset].times do |i|
|
225
|
-
fi.gets
|
226
|
-
end
|
227
|
-
end
|
228
|
-
opts[:sep]||=SEPARATOR
|
229
|
-
fi.each do |line|
|
230
|
-
row = MyMatrix.toutf8(line).chomp.split(/#{opts[:sep]}/)
|
231
|
-
#「1,300台」などカンマが使われている場合、「"1,300台"」となってしまうので、カンマを無視する
|
232
|
-
newRow = []
|
233
|
-
row.each do |cell|
|
234
|
-
stri = cell.dup
|
235
|
-
stri.gsub!(/^\"(.*)\"$/, '\1')
|
236
|
-
#"
|
237
|
-
stri.gsub!(/""/, '"')
|
238
|
-
newRow << stri
|
239
|
-
end
|
240
|
-
out << newRow
|
241
|
-
end
|
242
|
-
fi.close
|
243
|
-
return out
|
244
|
-
end
|
245
121
|
|
246
|
-
#CSV読み込みメソッド
|
247
|
-
def makeMatrixFromCSV(file, opts={:offset=>0})
|
248
|
-
#1.9系ではFasterCSVを使えない
|
249
|
-
if(RUBY_VERSION =~ /1\.[^9]/)
|
250
|
-
#1.8以下の場合
|
251
|
-
require 'fastercsv'
|
252
|
-
csv = FasterCSV
|
253
|
-
else
|
254
|
-
#1.9以上の場合
|
255
|
-
require 'csv'
|
256
|
-
Encoding.default_external = 'Windows-31J'
|
257
|
-
csv = CSV
|
258
|
-
end
|
259
|
-
out = []
|
260
|
-
i= 0
|
261
|
-
syspath = encodePath(file)
|
262
|
-
csv.foreach(syspath, {:row_sep => "\r\n", :encoding => 'Shift_JIS'}) do |row|
|
263
|
-
if(opts[:offset])
|
264
|
-
if(opts[:offset] < i)
|
265
|
-
next
|
266
|
-
end
|
267
|
-
end
|
268
|
-
#「1,300台」などカンマが使われている場合、「"1,300台"」となってしまうので、カンマを無視する
|
269
|
-
newRow = []
|
270
|
-
row.each do |cell|
|
271
|
-
cell = cell.to_s
|
272
|
-
cell ||= ''
|
273
|
-
cell = MyMatrix.toutf8(cell)
|
274
|
-
#cell = cell.gsub(/^\"/, "")
|
275
|
-
#cell = cell.gsub(/\"$/, "")
|
276
|
-
#"
|
277
|
-
newRow << cell
|
278
|
-
end
|
279
|
-
out << newRow
|
280
|
-
i += 1
|
281
|
-
end
|
282
|
-
return out
|
283
|
-
end
|
284
122
|
|
285
123
|
def isEnd(row)
|
286
124
|
out = true
|
@@ -351,6 +189,7 @@ class MyMatrix
|
|
351
189
|
end
|
352
190
|
alias val getValue
|
353
191
|
=begin
|
192
|
+
|
354
193
|
def getValues(row, arr)
|
355
194
|
out = []
|
356
195
|
arr.each do |ele|
|
@@ -361,7 +200,9 @@ class MyMatrix
|
|
361
200
|
end
|
362
201
|
return out
|
363
202
|
end
|
203
|
+
|
364
204
|
=end
|
205
|
+
|
365
206
|
def setValue(row, str, value)
|
366
207
|
if(!row)
|
367
208
|
raise 'row is nil'
|
@@ -532,7 +373,7 @@ class MyMatrix
|
|
532
373
|
end
|
533
374
|
#使い方はto_t()を参照。yield。
|
534
375
|
def to_text(outFile)
|
535
|
-
outFile = encodePath(outFile)
|
376
|
+
outFile = FileIO.encodePath(outFile)
|
536
377
|
out = []
|
537
378
|
out << @headers
|
538
379
|
@mx.each do |row|
|
@@ -1127,12 +968,6 @@ class MyMatrix
|
|
1127
968
|
end
|
1128
969
|
return out
|
1129
970
|
end
|
1130
|
-
# def save(file)
|
1131
|
-
# p Marshal.dump(self)
|
1132
|
-
# open(file, 'w') do |fo|
|
1133
|
-
# fo.write(Marshal.dump(self))
|
1134
|
-
# end
|
1135
|
-
# end
|
1136
971
|
|
1137
972
|
=begin
|
1138
973
|
def to_xls(opts)
|
@@ -1193,46 +1028,6 @@ class MyMatrix
|
|
1193
1028
|
postfix = opts[:postfix].to_s
|
1194
1029
|
|
1195
1030
|
basename = File.basename(path, ".*")
|
1196
|
-
opath = (
|
1031
|
+
opath = (FileIO.encodePath("#{dir}/#{basename}_#{postfix}#{ext}"))
|
1197
1032
|
end
|
1198
1033
|
end
|
1199
|
-
|
1200
|
-
#ruby -Ks で利用する場合。ruby1.9では使えないはず。obsolete
|
1201
|
-
class SjisMyMatrix < MyMatrix
|
1202
|
-
def getValue(row, col)
|
1203
|
-
col = MyMatrix.toutf8(col)
|
1204
|
-
MyMatrix.tosjis(super(row, col))
|
1205
|
-
end
|
1206
|
-
def setValue(row, col, value)
|
1207
|
-
col = MyMatrix.toutf8(col)
|
1208
|
-
value = MyMatrix.toutf8(value)
|
1209
|
-
super(row, col, value)
|
1210
|
-
end
|
1211
|
-
def addHeaders(hs)
|
1212
|
-
arr =[]
|
1213
|
-
hs.each do |ele|
|
1214
|
-
arr << MyMatrix.toutf8(ele)
|
1215
|
-
end
|
1216
|
-
super(arr)
|
1217
|
-
end
|
1218
|
-
def getHeaders
|
1219
|
-
out = []
|
1220
|
-
arr = super()
|
1221
|
-
arr.each do |ele|
|
1222
|
-
out << MyMatrix.tosjis(ele)
|
1223
|
-
end
|
1224
|
-
return out
|
1225
|
-
end
|
1226
|
-
end
|
1227
|
-
|
1228
|
-
|
1229
|
-
#rails で使う場合。obsolete
|
1230
|
-
class MyRailsMatrix < MyMatrix
|
1231
|
-
def headers2db(t)
|
1232
|
-
getHeaders.each do |header|
|
1233
|
-
t.column header, :string
|
1234
|
-
end
|
1235
|
-
end
|
1236
|
-
end
|
1237
|
-
|
1238
|
-
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mymatrix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152540560 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152540560
|
25
25
|
description: mymatrix is a handling library for MS Excel and csv/tsv text.
|
26
26
|
email:
|
27
27
|
- yukihico@gmail.com
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
78
|
rubyforge_project: mymatrix
|
79
|
-
rubygems_version: 1.
|
79
|
+
rubygems_version: 1.8.10
|
80
80
|
signing_key:
|
81
81
|
specification_version: 3
|
82
82
|
summary: MS Excel and csv/tsv text handling library
|