fixed-layout-mapper 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.
- checksums.yaml +7 -0
- data/.gitignore +5 -4
- data/Gemfile +4 -4
- data/README.md +143 -138
- data/Rakefile +8 -8
- data/fixed-layout-mapper.gemspec +25 -24
- data/lib/fixed-layout-mapper.rb +250 -250
- data/lib/fixed-layout-mapper/version.rb +3 -3
- data/test/fixed_layout_mapper_spec.rb +241 -241
- metadata +39 -12
@@ -1,3 +1,3 @@
|
|
1
|
-
module FixedLayoutMapper
|
2
|
-
VERSION = "0.0.
|
3
|
-
end
|
1
|
+
module FixedLayoutMapper
|
2
|
+
VERSION = "0.0.3"
|
3
|
+
end
|
@@ -1,241 +1,241 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
col :
|
19
|
-
|
20
|
-
|
21
|
-
rt
|
22
|
-
rt.
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
col :
|
29
|
-
col :
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
rt
|
34
|
-
rt.
|
35
|
-
rt.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
col :
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
rt
|
47
|
-
rt.
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
rt
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
col :
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
rt
|
70
|
-
rt.f.
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
col :
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
rt
|
84
|
-
rt.f[0].
|
85
|
-
rt.f[
|
86
|
-
rt.f[1].
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
col :
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
rt
|
100
|
-
rt.f[0].
|
101
|
-
rt.f[
|
102
|
-
rt.f[
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
rt
|
117
|
-
|
118
|
-
|
119
|
-
rt
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
rt
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
col :
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
rt
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
mapper.map("12345", :layout1)
|
206
|
-
}.
|
207
|
-
end
|
208
|
-
|
209
|
-
it "
|
210
|
-
mapper = define_layout do
|
211
|
-
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
|
212
|
-
col :f, 2
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
mapper.map("1", :layout1)
|
217
|
-
}.
|
218
|
-
end
|
219
|
-
|
220
|
-
it "
|
221
|
-
mapper = define_layout do
|
222
|
-
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
|
223
|
-
col :f, 2
|
224
|
-
end
|
225
|
-
|
226
|
-
|
227
|
-
rt = mapper.map("12", :layout1)
|
228
|
-
rt.f.
|
229
|
-
end
|
230
|
-
|
231
|
-
it "
|
232
|
-
mapper = define_layout do
|
233
|
-
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
|
234
|
-
col :f, 2
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
mapper.map("1", :layout1)
|
239
|
-
}.
|
240
|
-
end
|
241
|
-
end
|
1
|
+
|
2
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/fixed-layout-mapper')
|
4
|
+
require 'rspec'
|
5
|
+
|
6
|
+
describe FixedLayoutMapper::Mapper do
|
7
|
+
def define_layout(&block)
|
8
|
+
FixedLayoutMapper::Mapper.define_layout(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def genstr(ary)
|
12
|
+
ary.join
|
13
|
+
end
|
14
|
+
|
15
|
+
it "サイズ指定でカラムを定義できること" do
|
16
|
+
mapper = define_layout do
|
17
|
+
col :field1, 2
|
18
|
+
col :field2, 1
|
19
|
+
end
|
20
|
+
rt = mapper.map(%w(12 3).join)
|
21
|
+
expect(rt.field1).to eq "12"
|
22
|
+
expect(rt.field2).to eq "3"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "サイズの配列でカラムを定義するとその桁で分割された配列になること" do
|
26
|
+
mapper = define_layout do
|
27
|
+
col :f1, 2
|
28
|
+
col :f2, [1, 2, 3]
|
29
|
+
col :f3, [4] * 2
|
30
|
+
end
|
31
|
+
|
32
|
+
rt = mapper.map(%w(12 a bb ccc 1234 5678).join)
|
33
|
+
expect(rt.f1).to eq "12"
|
34
|
+
expect(rt.f2).to eq %w(a bb ccc)
|
35
|
+
expect(rt.f3).to eq %w(1234 5678)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "レイアウトに名前をつけられること" do
|
39
|
+
mapper = define_layout do
|
40
|
+
layout :layout1 do
|
41
|
+
col :f1, 2
|
42
|
+
col :f2, 1
|
43
|
+
end
|
44
|
+
end
|
45
|
+
rt = mapper.map(%w(12 3).join, :layout1)
|
46
|
+
expect(rt.f1).to eq "12"
|
47
|
+
expect(rt.f2).to eq "3"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "レイアウト名を指定していない場合:default_layoutで定義されていること" do
|
51
|
+
mapper = define_layout do
|
52
|
+
col :f1, 2
|
53
|
+
end
|
54
|
+
|
55
|
+
rt = mapper.map(%w(12).join, :default_layout)
|
56
|
+
expect(rt.f1).to eq "12"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "あるレイアウトで別のレイアウト(以下サブレイアウト)を使えること" do
|
60
|
+
mapper = define_layout do
|
61
|
+
layout :layout1 do
|
62
|
+
col :f1, 2
|
63
|
+
col :f2, 1
|
64
|
+
end
|
65
|
+
|
66
|
+
col :f, :layout1
|
67
|
+
end
|
68
|
+
rt = mapper.map(%w(12 3).join)
|
69
|
+
expect(rt.f.f1).to eq "12"
|
70
|
+
expect(rt.f.f2).to eq "3"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "サブレイアウトの配列も定義できること" do
|
74
|
+
mapper = define_layout do
|
75
|
+
layout :layout1 do
|
76
|
+
col :f1, 2
|
77
|
+
col :f2, 1
|
78
|
+
end
|
79
|
+
|
80
|
+
col :f, [:layout1, :layout1]
|
81
|
+
end
|
82
|
+
rt = mapper.map(%w(12 3 ab c).join)
|
83
|
+
expect(rt.f[0].f1).to eq "12"
|
84
|
+
expect(rt.f[0].f2).to eq "3"
|
85
|
+
expect(rt.f[1].f1).to eq "ab"
|
86
|
+
expect(rt.f[1].f2).to eq "c"
|
87
|
+
end
|
88
|
+
|
89
|
+
it "配列にはサイズでもサブレイアウトでも指定できること" do
|
90
|
+
mapper = define_layout do
|
91
|
+
layout :layout1 do
|
92
|
+
col :f1, 2
|
93
|
+
col :f2, 1
|
94
|
+
end
|
95
|
+
|
96
|
+
col :f, [:layout1, 1, 2]
|
97
|
+
end
|
98
|
+
rt = mapper.map(%w(12 3 a bc).join)
|
99
|
+
expect(rt.f[0].f1).to eq "12"
|
100
|
+
expect(rt.f[0].f2).to eq "3"
|
101
|
+
expect(rt.f[1]).to eq "a"
|
102
|
+
expect(rt.f[2]).to eq "bc"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "複数のレイアウトを定義できること" do
|
106
|
+
mapper = define_layout do
|
107
|
+
layout :layout1 do
|
108
|
+
col :f1, 1
|
109
|
+
end
|
110
|
+
|
111
|
+
layout :layout2 do
|
112
|
+
col :f2, 2
|
113
|
+
end
|
114
|
+
end
|
115
|
+
rt = mapper.map(%w(1).join, :layout1)
|
116
|
+
expect(rt.f1).to eq "1"
|
117
|
+
|
118
|
+
rt = mapper.map(%w(ab).join, :layout2)
|
119
|
+
expect(rt.f2).to eq "ab"
|
120
|
+
end
|
121
|
+
|
122
|
+
it "変換時にアクションを定義した場合その戻り値でマッピングされること" do
|
123
|
+
mapper = define_layout do
|
124
|
+
col :f1, 2 do |v|
|
125
|
+
v + v
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
rt = mapper.map(%w(12).join, :default_layout)
|
130
|
+
expect(rt.f1).to eq "1212"
|
131
|
+
end
|
132
|
+
|
133
|
+
it "レングス指定のレイアウトのレングスが取得できること" do
|
134
|
+
mapper = define_layout do
|
135
|
+
col :f1, 2
|
136
|
+
end
|
137
|
+
|
138
|
+
expect(mapper.get_layout.length).to eq 2
|
139
|
+
end
|
140
|
+
|
141
|
+
it "配列指定のレイアウトのレングスが取得できること" do
|
142
|
+
mapper = define_layout do
|
143
|
+
col :f1, [1, 2, 3]
|
144
|
+
end
|
145
|
+
|
146
|
+
expect(mapper.get_layout.length).to eq 6
|
147
|
+
end
|
148
|
+
|
149
|
+
it "サブレイアウト指定のレイアウトのレングスが取得できること" do
|
150
|
+
mapper = define_layout do
|
151
|
+
layout :layout1 do
|
152
|
+
col :f, 1
|
153
|
+
end
|
154
|
+
|
155
|
+
layout :layout2 do
|
156
|
+
col :f ,3
|
157
|
+
end
|
158
|
+
|
159
|
+
col :f1, :layout1
|
160
|
+
col :f2, :layout2
|
161
|
+
end
|
162
|
+
|
163
|
+
expect(mapper.get_layout.length).to eq 4
|
164
|
+
end
|
165
|
+
|
166
|
+
it "配列+サブレイアウト指定のレイアウトのレングスが取得できること" do
|
167
|
+
mapper = define_layout do
|
168
|
+
layout :layout1 do
|
169
|
+
col :f, 1
|
170
|
+
end
|
171
|
+
col :f1, [10, :layout1, 1]
|
172
|
+
end
|
173
|
+
expect(mapper.get_layout.length).to eq 12
|
174
|
+
end
|
175
|
+
|
176
|
+
it "レイアウトにレングス制約(少なくともデータのほうが長い)を指定された場合データのレングスの方が長くてもエラーにならないこと" do
|
177
|
+
mapper = define_layout do
|
178
|
+
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
|
179
|
+
col :f, 2
|
180
|
+
end
|
181
|
+
end
|
182
|
+
rt = mapper.map("12345", :layout1)
|
183
|
+
expect(rt.f).to eq "12"
|
184
|
+
end
|
185
|
+
|
186
|
+
it "レイアウトにレングス制約(少なくともデータのほうが長い)を指定された場合データのレングスの方が短い場合例外が発生すること" do
|
187
|
+
mapper = define_layout do
|
188
|
+
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
|
189
|
+
col :f, 2
|
190
|
+
end
|
191
|
+
end
|
192
|
+
expect {
|
193
|
+
mapper.map("1", :layout1)
|
194
|
+
}.to raise_error("byte length is too short")
|
195
|
+
end
|
196
|
+
|
197
|
+
it "レイアウトにレングス制約(データ長さと同じ)を指定された場合データのレングスの方が長いとエラーになること" do
|
198
|
+
mapper = define_layout do
|
199
|
+
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
|
200
|
+
col :f, 2
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
expect {
|
205
|
+
mapper.map("12345", :layout1)
|
206
|
+
}.to raise_error("byte length is invalid")
|
207
|
+
end
|
208
|
+
|
209
|
+
it "レイアウトにレングス制約(データ長さと同じ)を指定された場合データのレングスの方が短いとエラーになること" do
|
210
|
+
mapper = define_layout do
|
211
|
+
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
|
212
|
+
col :f, 2
|
213
|
+
end
|
214
|
+
end
|
215
|
+
expect {
|
216
|
+
mapper.map("1", :layout1)
|
217
|
+
}.to raise_error("byte length is invalid")
|
218
|
+
end
|
219
|
+
|
220
|
+
it "レイアウトにレングス制約(データ長さと同じ)を指定された場合データのレングスと同じ場合はエラーが発生しないこと" do
|
221
|
+
mapper = define_layout do
|
222
|
+
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
|
223
|
+
col :f, 2
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
rt = mapper.map("12", :layout1)
|
228
|
+
expect(rt.f).to eq "12"
|
229
|
+
end
|
230
|
+
|
231
|
+
it "レイアウトにレングス制約(データ長さよりは長い)を指定された場合データのレングスの方が短いとエラーになること" do
|
232
|
+
mapper = define_layout do
|
233
|
+
layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
|
234
|
+
col :f, 2
|
235
|
+
end
|
236
|
+
end
|
237
|
+
expect {
|
238
|
+
mapper.map("1", :layout1)
|
239
|
+
}.to raise_error("byte length is too short")
|
240
|
+
end
|
241
|
+
end
|