pikelet 2.0.0.beta.1 → 2.0.0.beta.2
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 +17 -7
- data/lib/pikelet/version.rb +1 -1
- data/spec/pikelet/field_definition_spec.rb +116 -28
- data/spec/pikelet_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38ba6a85d7364f3d8a5b93bb24cc8d64bac5de91
|
4
|
+
data.tar.gz: e6e9e5e60b8ac35c8669fa992327ee422d3871f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0a0c5500a5f72c17735608a678f78fbdd9c433c086be1875e1077d11da3e2463c3704cf75621f4a4c6ff569e8ea8873d82bd26d06f4042ee47b8b9a68a83c08
|
7
|
+
data.tar.gz: e65c43af74592ffe8462c91dab56abc0d67bbb82880ba227b0342f3ea542827686653dadbf46873dc244a2e0d55313c5d23e5daf821ce78f200ea62e2ead15ab
|
@@ -1,23 +1,25 @@
|
|
1
1
|
module Pikelet
|
2
2
|
class FieldDefinition
|
3
|
-
attr_reader :index, :parser, :width
|
3
|
+
attr_reader :index, :parser, :formatter, :padding, :alignment, :width
|
4
4
|
|
5
|
-
def initialize(index, parse: nil, &block)
|
5
|
+
def initialize(index, parse: nil, format: nil, pad: nil, align: nil, &block)
|
6
6
|
raise ArgumentError, "index must be a range" unless index.is_a? Range
|
7
7
|
@index = index
|
8
8
|
@width = index.size
|
9
9
|
@parser = parse || block || :strip
|
10
|
-
@
|
10
|
+
@formatter = format || :to_s
|
11
|
+
@padding = pad && pad.to_s || " "
|
12
|
+
@alignment = align || :left
|
11
13
|
end
|
12
14
|
|
13
15
|
def parse(record)
|
14
16
|
if value = record[index]
|
15
|
-
parser.call(value)
|
17
|
+
parser.to_proc.call(value)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
def format(value)
|
20
|
-
pad(truncate(value))
|
22
|
+
pad(truncate(formatter.to_proc.call(value)))
|
21
23
|
end
|
22
24
|
|
23
25
|
def insert(value, record)
|
@@ -27,12 +29,20 @@ module Pikelet
|
|
27
29
|
|
28
30
|
private
|
29
31
|
|
32
|
+
def blank
|
33
|
+
@blank ||= padding * width
|
34
|
+
end
|
35
|
+
|
30
36
|
def truncate(value)
|
31
|
-
value
|
37
|
+
value[0...width]
|
32
38
|
end
|
33
39
|
|
34
40
|
def pad(value)
|
35
|
-
|
41
|
+
if alignment == :left
|
42
|
+
value + blank[value.size...width]
|
43
|
+
else
|
44
|
+
blank[-width..-(1+value.size)] + value
|
45
|
+
end
|
36
46
|
end
|
37
47
|
end
|
38
48
|
end
|
data/lib/pikelet/version.rb
CHANGED
@@ -3,9 +3,26 @@ require "pikelet"
|
|
3
3
|
require "csv"
|
4
4
|
|
5
5
|
describe Pikelet::FieldDefinition do
|
6
|
+
let(:parser) { { } }
|
7
|
+
let(:formatter) { { } }
|
8
|
+
let(:padding) { { } }
|
9
|
+
let(:alignment) { { } }
|
10
|
+
let(:block) { nil }
|
11
|
+
let(:definition) { Pikelet::FieldDefinition.new(index, **parser, **formatter, **padding, **alignment, &block) }
|
12
|
+
|
13
|
+
describe "defaults" do
|
14
|
+
let(:index) { 0...1 }
|
15
|
+
|
16
|
+
subject { definition }
|
17
|
+
|
18
|
+
its(:parser) { is_expected.to eq :strip }
|
19
|
+
its(:formatter) { is_expected.to eq :to_s }
|
20
|
+
its(:padding) { is_expected.to eq " " }
|
21
|
+
its(:alignment) { is_expected.to eq :left }
|
22
|
+
end
|
23
|
+
|
6
24
|
describe "#parse" do
|
7
25
|
let(:data) { "The quick brown fox" }
|
8
|
-
let(:definition) { Pikelet::FieldDefinition.new(index) }
|
9
26
|
|
10
27
|
subject(:parsed) { definition.parse(data) }
|
11
28
|
|
@@ -26,38 +43,38 @@ describe Pikelet::FieldDefinition do
|
|
26
43
|
end
|
27
44
|
|
28
45
|
context "given a custom parser" do
|
29
|
-
let(:
|
46
|
+
let(:block_parser) { ->(value) { value } }
|
30
47
|
|
31
48
|
before do
|
32
|
-
allow(
|
49
|
+
allow(block_parser).to receive(:call).and_call_original
|
33
50
|
parsed
|
34
51
|
end
|
35
52
|
|
36
53
|
context "as a block" do
|
37
|
-
let(:index)
|
38
|
-
let(:
|
54
|
+
let(:index) { 4...9 }
|
55
|
+
let(:block) { block_parser }
|
39
56
|
|
40
57
|
it "yields the value to the parser" do
|
41
|
-
expect(
|
58
|
+
expect(block_parser).to have_received(:call).with("quick")
|
42
59
|
end
|
43
60
|
end
|
44
61
|
|
45
62
|
context "as a parse option" do
|
46
|
-
let(:index)
|
47
|
-
let(:
|
63
|
+
let(:index) { 10...15 }
|
64
|
+
let(:parser) { { parse: block_parser } }
|
48
65
|
|
49
66
|
it "yields the value to the parser" do
|
50
|
-
expect(
|
67
|
+
expect(block_parser).to have_received(:call).with("brown")
|
51
68
|
end
|
52
69
|
end
|
53
70
|
end
|
54
71
|
|
55
72
|
context "given a shorthand parser" do
|
56
|
-
let(:
|
73
|
+
let(:symbol_parser) { :upcase }
|
57
74
|
|
58
75
|
context "as a block" do
|
59
|
-
let(:index)
|
60
|
-
let(:
|
76
|
+
let(:index) { 10...15 }
|
77
|
+
let(:block) { symbol_parser }
|
61
78
|
|
62
79
|
it "invokes the named method on the value" do
|
63
80
|
expect(parsed).to eq "BROWN"
|
@@ -65,8 +82,8 @@ describe Pikelet::FieldDefinition do
|
|
65
82
|
end
|
66
83
|
|
67
84
|
context "as a parse option" do
|
68
|
-
let(:index)
|
69
|
-
let(:
|
85
|
+
let(:index) { 4...9 }
|
86
|
+
let(:parser) { { parse: symbol_parser } }
|
70
87
|
|
71
88
|
it "invokes the named method on the value" do
|
72
89
|
expect(parsed).to eq "QUICK"
|
@@ -87,7 +104,6 @@ describe Pikelet::FieldDefinition do
|
|
87
104
|
let(:index) { 4...9 }
|
88
105
|
let(:record) { "The _____ brown fox" }
|
89
106
|
let(:value) { "quick" }
|
90
|
-
let(:definition) { Pikelet::FieldDefinition.new(index) }
|
91
107
|
|
92
108
|
subject(:result) { definition.insert(value, record) }
|
93
109
|
|
@@ -106,34 +122,106 @@ describe Pikelet::FieldDefinition do
|
|
106
122
|
end
|
107
123
|
|
108
124
|
describe "#format" do
|
109
|
-
let(:index) { 4...9 }
|
110
|
-
|
111
125
|
subject(:formatted) { definition.format(value) }
|
112
126
|
|
113
|
-
|
114
|
-
let(:
|
127
|
+
describe "truncation" do
|
128
|
+
let(:index) { 4...9 }
|
115
129
|
|
116
130
|
context "given a value that fits the field exactly" do
|
117
131
|
let(:value) { "quick" }
|
118
132
|
|
119
|
-
it "
|
120
|
-
expect(formatted).to eq
|
133
|
+
it "does not truncate the value" do
|
134
|
+
expect(formatted).to eq value
|
121
135
|
end
|
122
136
|
end
|
123
137
|
|
124
|
-
context "given a value
|
125
|
-
let(:value) { "
|
138
|
+
context "given a value that overflows the field" do
|
139
|
+
let(:value) { "12345678" }
|
126
140
|
|
127
141
|
it "truncates the value" do
|
128
|
-
expect(formatted).to eq
|
142
|
+
expect(formatted).to eq value[0...5]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "given a custom formatter" do
|
148
|
+
let(:index) { 4...5 }
|
149
|
+
let(:block_formatter) { ->(value) { value } }
|
150
|
+
let(:formatter) { { format: block_formatter} }
|
151
|
+
let(:value) { "brown" }
|
152
|
+
|
153
|
+
before do
|
154
|
+
allow(block_formatter).to receive(:call).and_call_original
|
155
|
+
formatted
|
156
|
+
end
|
157
|
+
|
158
|
+
it "yields the raw value to the formatter" do
|
159
|
+
expect(block_formatter).to have_received(:call).with("brown")
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "using a shorthand formatter" do
|
164
|
+
let(:index) { 4...9 }
|
165
|
+
let(:formatter) { { format: :upcase } }
|
166
|
+
let(:value) { "brown" }
|
167
|
+
|
168
|
+
it "invokes the named method on the raw value" do
|
169
|
+
expect(formatted).to eq "BROWN"
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "padding & alignment" do
|
174
|
+
let(:index) { 3...7 }
|
175
|
+
|
176
|
+
context "given a value that fits the field exactly" do
|
177
|
+
let(:value) { "1234" }
|
178
|
+
|
179
|
+
it "does not pad the field" do
|
180
|
+
expect(formatted).to eq value
|
129
181
|
end
|
130
182
|
end
|
131
183
|
|
132
|
-
context "
|
133
|
-
let(:value) { "
|
184
|
+
context "given a value that underflows the field" do
|
185
|
+
let(:value) { "12" }
|
186
|
+
|
187
|
+
context "with left alignment" do
|
188
|
+
let(:alignment) { { align: :left } }
|
189
|
+
|
190
|
+
context "with single-character padding" do
|
191
|
+
let(:padding) { { pad: '-' } }
|
192
|
+
|
193
|
+
it "pads the field on the right" do
|
194
|
+
expect(formatted).to eq "12--"
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context "with multi-character padding" do
|
199
|
+
let(:padding) { { pad: '<->' } }
|
200
|
+
|
201
|
+
it "pads the field on the right" do
|
202
|
+
expect(formatted).to eq "12><"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context "with right alignment" do
|
208
|
+
let(:alignment) { { align: :right } }
|
209
|
+
|
210
|
+
context "with single-character padding" do
|
211
|
+
let(:padding) { { pad: '-' } }
|
212
|
+
|
213
|
+
it "pads the field on the left" do
|
214
|
+
expect(formatted).to eq "--12"
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "with multi-character padding" do
|
219
|
+
let(:padding) { { pad: '<->' } }
|
134
220
|
|
135
|
-
|
136
|
-
|
221
|
+
it "pads the field on the left" do
|
222
|
+
expect(formatted).to eq "><12"
|
223
|
+
end
|
224
|
+
end
|
137
225
|
end
|
138
226
|
end
|
139
227
|
end
|
data/spec/pikelet_spec.rb
CHANGED
@@ -39,8 +39,8 @@ describe Pikelet do
|
|
39
39
|
|
40
40
|
it { is_expected.to have(2).lines }
|
41
41
|
|
42
|
-
its(:first) { is_expected.to eq "
|
43
|
-
its(:last) { is_expected.to eq "
|
42
|
+
its(:first) { is_expected.to eq "NAMENicolaus Copernicus " }
|
43
|
+
its(:last) { is_expected.to eq "ADDR123 South Street Nowhereville 45678Y Someplace " }
|
44
44
|
end
|
45
45
|
|
46
46
|
describe "#parse" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pikelet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.beta.
|
4
|
+
version: 2.0.0.beta.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Carney
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|