pikelet 2.0.0.beta.6 → 2.0.0.beta.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pikelet/field_definition.rb +20 -5
- data/lib/pikelet/record_definer.rb +1 -1
- data/lib/pikelet/version.rb +1 -1
- data/spec/pikelet/field_definition_spec.rb +29 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e3adcc8650568a13523518174d2723b61ddf4ba
|
4
|
+
data.tar.gz: d79cc1a300a4d0356ada007222b6759f98e86c6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fb05cf2c716cd1cee499cbe4abe9fe4fd6d4bf99b55c0d9bf471a5749dc17d122514f2aebd8d52e671c3dadf5633e69e1183a10ee16275fdbe46897dd0f5546
|
7
|
+
data.tar.gz: d4b1012c93182ce8d00364ed9d47ccd9ba5003e224c122ccf78d4f33111f08fdf3b3208501a2054fa9aba06b5e75172a7803d57ea6c51a33be97ac7df167d3e3
|
@@ -2,18 +2,33 @@ module Pikelet
|
|
2
2
|
class FieldDefinition
|
3
3
|
attr_reader :index, :parser, :formatter, :padding, :alignment, :width
|
4
4
|
|
5
|
-
def initialize(index, parse: nil, format: nil, pad: nil, align: nil, &block)
|
5
|
+
def initialize(index, parse: nil, type: :alpha, format: nil, pad: nil, align: nil, &block)
|
6
6
|
raise ArgumentError, "index must be a range" unless index.is_a? Range
|
7
|
+
raise ArgumentError, "type must be :alpha or :numeric" unless %i{ alpha numeric }.include? type
|
8
|
+
if align
|
9
|
+
raise ArgumentError, "align must be :left, :right, or :center" unless %i{ left right centre center }.include?(align)
|
10
|
+
end
|
11
|
+
|
7
12
|
@index = index
|
8
13
|
@width = index.size
|
9
14
|
@parser = parse || block || :strip
|
10
15
|
@formatter = format || :to_s
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@
|
16
|
+
|
17
|
+
if type == :numeric
|
18
|
+
@padding = pad && pad.to_s || "0"
|
19
|
+
@alignment = align || :right
|
15
20
|
else
|
21
|
+
@padding = pad && pad.to_s || " "
|
22
|
+
@alignment = align || :left
|
23
|
+
end
|
24
|
+
|
25
|
+
case alignment
|
26
|
+
when :right
|
27
|
+
@align_method = :rjust
|
28
|
+
when :left
|
16
29
|
@align_method = :ljust
|
30
|
+
when :centre, :center
|
31
|
+
@align_method = :center
|
17
32
|
end
|
18
33
|
end
|
19
34
|
|
@@ -7,7 +7,7 @@ module Pikelet
|
|
7
7
|
@definition = RecordDefinition.new(file_definition, record_class: record_class, base: base)
|
8
8
|
end
|
9
9
|
|
10
|
-
def field(name, index, **options, &block)
|
10
|
+
def field(name, index, *flags, **options, &block)
|
11
11
|
definition.field(name, index, **options, &block)
|
12
12
|
end
|
13
13
|
|
data/lib/pikelet/version.rb
CHANGED
@@ -7,8 +7,9 @@ describe Pikelet::FieldDefinition do
|
|
7
7
|
let(:formatter) { { } }
|
8
8
|
let(:padding) { { } }
|
9
9
|
let(:alignment) { { } }
|
10
|
+
let(:type) { { } }
|
10
11
|
let(:block) { nil }
|
11
|
-
let(:definition) { Pikelet::FieldDefinition.new(index, **parser, **formatter, **padding, **alignment, &block) }
|
12
|
+
let(:definition) { Pikelet::FieldDefinition.new(index, **type, **parser, **formatter, **padding, **alignment, &block) }
|
12
13
|
|
13
14
|
describe "defaults" do
|
14
15
|
let(:index) { 0...1 }
|
@@ -19,6 +20,13 @@ describe Pikelet::FieldDefinition do
|
|
19
20
|
its(:formatter) { is_expected.to eq :to_s }
|
20
21
|
its(:padding) { is_expected.to eq " " }
|
21
22
|
its(:alignment) { is_expected.to eq :left }
|
23
|
+
|
24
|
+
context "for numeric type" do
|
25
|
+
let(:type) { { type: :numeric } }
|
26
|
+
|
27
|
+
its(:padding) { is_expected.to eq "0" }
|
28
|
+
its(:alignment) { is_expected.to eq :right }
|
29
|
+
end
|
22
30
|
end
|
23
31
|
|
24
32
|
describe "#parse" do
|
@@ -184,6 +192,26 @@ describe Pikelet::FieldDefinition do
|
|
184
192
|
context "given a value that underflows the field" do
|
185
193
|
let(:value) { "12" }
|
186
194
|
|
195
|
+
context "with center alignment" do
|
196
|
+
let(:alignment) { { align: :center } }
|
197
|
+
|
198
|
+
context "with single-character padding" do
|
199
|
+
let(:padding) { { pad: '-' } }
|
200
|
+
|
201
|
+
it "pads the field on the left and right" do
|
202
|
+
expect(formatted).to eq "-12-"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
context "with multi-character padding" do
|
207
|
+
let(:padding) { { pad: '<->' } }
|
208
|
+
|
209
|
+
it "pads the field on the left and right" do
|
210
|
+
expect(formatted).to eq "<12<"
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
187
215
|
context "with left alignment" do
|
188
216
|
let(:alignment) { { align: :left } }
|
189
217
|
|