fixy 0.0.6 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +12 -0
- data/README.md +10 -4
- data/lib/fixy/formatter/alphanumeric.rb +1 -1
- data/lib/fixy/record.rb +23 -4
- data/lib/fixy/version.rb +1 -1
- data/spec/fixy/record_spec.rb +44 -0
- metadata +15 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f33b913ca5e515fc32e84463bbb0bca7b04d4fb0349ae673f3906fe8530355c5
|
4
|
+
data.tar.gz: 693fbf4d139be73d471c6665fbeed04e26ea5b3f55007672fa6ddeb38d17fec9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8cd433833fa4598b166f4ba4bdfab5879579288b4f977810f50e97ad2391dae9318cf036593d44af8cf871f3a5228ad4c45e75abb6437559cc3a429731045bb
|
7
|
+
data.tar.gz: 4b4802efeeb1b0aee6c988428fd15f17e7d9692733199fb6c67fe9053c9a4da488ec6fa3a58f88b5927c7abc8b25ce3853ff19df4cdac8f1c9df2022d43a7342
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -36,6 +36,7 @@ A fixed-width document (`Fixy::Document`) is composed of multiple single-line re
|
|
36
36
|
Every record is defined through a specific format, which defines the following aspects:
|
37
37
|
|
38
38
|
* Record length (how many characters in the line)
|
39
|
+
* Line ending (optional, defaults to "\n")
|
39
40
|
* Required formatters (e.g. Alphanumeric, Rate, Amount)
|
40
41
|
* Field declaration:
|
41
42
|
* Field human readable name
|
@@ -93,6 +94,11 @@ You can also specify the field definition and field value together by passing a
|
|
93
94
|
|
94
95
|
field(:first_name, 10, '1-10', :alphanumeric) { @first_name }
|
95
96
|
```
|
97
|
+
If a record requires a specific line ending, you can specify it as part of the Record definition.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
set_line_ending Fixy::Record::LINE_ENDING_CRLF
|
101
|
+
```
|
96
102
|
|
97
103
|
Given a record definition, you can generate a single line (e.g. for testing purposes):
|
98
104
|
|
@@ -147,10 +153,10 @@ Occasionally, it is useful to generate a document using existing records. This i
|
|
147
153
|
```ruby
|
148
154
|
class ParsedPeopleDocument < Fixy::Document
|
149
155
|
def build
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
156
|
+
parse_record IdentityRecord, 'Arcturus Mengsk '
|
157
|
+
parse_record IdentityRecord, 'Sarah Kerrigan '
|
158
|
+
parse_record IdentityRecord, 'Jim Raynor '
|
159
|
+
end
|
154
160
|
end
|
155
161
|
```
|
156
162
|
|
@@ -10,7 +10,7 @@ module Fixy
|
|
10
10
|
#
|
11
11
|
|
12
12
|
def format_alphanumeric(input, byte_width)
|
13
|
-
input_string = String.new(input.to_s)
|
13
|
+
input_string = String.new(input.to_s).tr "#{self.class::LINE_ENDING_CRLF}#{line_ending}", ''
|
14
14
|
result = ''
|
15
15
|
|
16
16
|
if input_string.bytesize <= byte_width
|
data/lib/fixy/record.rb
CHANGED
@@ -1,11 +1,20 @@
|
|
1
1
|
module Fixy
|
2
2
|
class Record
|
3
|
+
LINE_ENDING_LF = "\n".freeze
|
4
|
+
LINE_ENDING_CR = "\r".freeze
|
5
|
+
LINE_ENDING_CRLF = "#{LINE_ENDING_CR}#{LINE_ENDING_LF}".freeze
|
6
|
+
DEFAULT_LINE_ENDING = LINE_ENDING_LF
|
7
|
+
|
3
8
|
class << self
|
4
9
|
def set_record_length(count)
|
5
10
|
define_singleton_method('record_length') { count }
|
6
11
|
end
|
7
12
|
|
8
|
-
def
|
13
|
+
def set_line_ending(character)
|
14
|
+
@line_ending = character
|
15
|
+
end
|
16
|
+
|
17
|
+
def field(name, size, range, type, &block)
|
9
18
|
@record_fields ||= default_record_fields
|
10
19
|
range_matches = range.match /^(\d+)(?:-(\d+))?$/
|
11
20
|
|
@@ -33,7 +42,7 @@ module Fixy
|
|
33
42
|
# We're good to go :)
|
34
43
|
@record_fields[range_from] = { name: name, from: range_from, to: range_to, size: size, type: type}
|
35
44
|
|
36
|
-
field_value(name,
|
45
|
+
field_value(name, block) if block_given?
|
37
46
|
end
|
38
47
|
|
39
48
|
# Convenience method for creating field methods
|
@@ -55,6 +64,11 @@ module Fixy
|
|
55
64
|
@record_fields
|
56
65
|
end
|
57
66
|
|
67
|
+
def line_ending
|
68
|
+
# Use the default line ending unless otherwise specified
|
69
|
+
@line_ending || DEFAULT_LINE_ENDING
|
70
|
+
end
|
71
|
+
|
58
72
|
def default_record_fields
|
59
73
|
if superclass.respond_to?(:record_fields, true) && superclass.record_fields
|
60
74
|
superclass.record_fields.dup
|
@@ -98,7 +112,7 @@ module Fixy
|
|
98
112
|
end
|
99
113
|
|
100
114
|
# Documentation mandates that every record ends with new line.
|
101
|
-
output <<
|
115
|
+
output << line_ending
|
102
116
|
|
103
117
|
{ fields: fields, record: decorator.record(output) }
|
104
118
|
end
|
@@ -128,7 +142,7 @@ module Fixy
|
|
128
142
|
end
|
129
143
|
|
130
144
|
# Documentation mandates that every record ends with new line.
|
131
|
-
output <<
|
145
|
+
output << line_ending
|
132
146
|
|
133
147
|
# All ready. In the words of Mr. Peters: "Take it and go!"
|
134
148
|
decorator.record(output)
|
@@ -145,5 +159,10 @@ module Fixy
|
|
145
159
|
def record_fields
|
146
160
|
self.class.record_fields
|
147
161
|
end
|
162
|
+
|
163
|
+
# Retrieves the line ending for this record type
|
164
|
+
def line_ending
|
165
|
+
self.class.line_ending
|
166
|
+
end
|
148
167
|
end
|
149
168
|
end
|
data/lib/fixy/version.rb
CHANGED
data/spec/fixy/record_spec.rb
CHANGED
@@ -10,6 +10,8 @@ describe 'Defining a Record' do
|
|
10
10
|
|
11
11
|
set_record_length 20
|
12
12
|
|
13
|
+
set_line_ending Fixy::Record::LINE_ENDING_CRLF
|
14
|
+
|
13
15
|
field :first_name, 10, '1-10' , :alphanumeric
|
14
16
|
field :last_name , 10, '11-20', :alphanumeric
|
15
17
|
end
|
@@ -118,6 +120,23 @@ describe 'Generating a Record' do
|
|
118
120
|
end
|
119
121
|
end
|
120
122
|
|
123
|
+
context 'when a field value contains the record separator' do
|
124
|
+
it 'should strip that separator' do
|
125
|
+
class PersonRecordNewLine < Fixy::Record
|
126
|
+
include Fixy::Formatter::Alphanumeric
|
127
|
+
|
128
|
+
set_record_length 9
|
129
|
+
|
130
|
+
field :name, 9, '1-9' , :alphanumeric
|
131
|
+
|
132
|
+
field_value :name, -> { "Two\nLine" }
|
133
|
+
end
|
134
|
+
|
135
|
+
value = PersonRecordNewLine.new.generate
|
136
|
+
value.should == "TwoLine \n"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
121
140
|
context 'when definition is incomplete (e.g. undefined columns)' do
|
122
141
|
it 'should raise an error' do
|
123
142
|
class PersonRecordF < Fixy::Record
|
@@ -185,6 +204,19 @@ describe 'Generating a Record' do
|
|
185
204
|
PersonRecordJ.new.generate.should eq('Use My Value'.ljust(20) << "\n")
|
186
205
|
end
|
187
206
|
end
|
207
|
+
|
208
|
+
context 'when setting a line ending' do
|
209
|
+
class PersonRecordWithLineEnding < Fixy::Record
|
210
|
+
include Fixy::Formatter::Alphanumeric
|
211
|
+
set_record_length 20
|
212
|
+
set_line_ending Fixy::Record::LINE_ENDING_CRLF
|
213
|
+
field(:description , 20, '1-20', :alphanumeric) { 'Use My Value' }
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'uses the given line ending' do
|
217
|
+
PersonRecordWithLineEnding.new.generate.should eq('Use My Value'.ljust(20) << "\r\n")
|
218
|
+
end
|
219
|
+
end
|
188
220
|
end
|
189
221
|
|
190
222
|
describe 'Parsing a record' do
|
@@ -207,6 +239,18 @@ describe 'Parsing a record' do
|
|
207
239
|
end
|
208
240
|
end
|
209
241
|
|
242
|
+
context 'with custom line endings' do
|
243
|
+
let(:record) { "Use My Value " }
|
244
|
+
it 'should generate fixed width record' do
|
245
|
+
PersonRecordWithLineEnding.parse(record).should eq({
|
246
|
+
record: (record + Fixy::Record::LINE_ENDING_CRLF),
|
247
|
+
fields: [
|
248
|
+
{ name: :description, value: 'Use My Value '}
|
249
|
+
]
|
250
|
+
})
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
210
254
|
context 'when properly defined' do
|
211
255
|
let(:record) { "Sarah Kerrigan " }
|
212
256
|
class PersonRecordK < Fixy::Record
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Omar Skalli
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Library for generating fixed width flat files.
|
@@ -59,7 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
62
|
+
- ".gitignore"
|
63
63
|
- CHANGELOG.md
|
64
64
|
- Gemfile
|
65
65
|
- LICENSE.txt
|
@@ -84,24 +84,23 @@ homepage: https://github.com/chetane/fixy
|
|
84
84
|
licenses:
|
85
85
|
- MIT
|
86
86
|
metadata: {}
|
87
|
-
post_install_message:
|
87
|
+
post_install_message:
|
88
88
|
rdoc_options: []
|
89
89
|
require_paths:
|
90
90
|
- lib
|
91
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
|
-
- -
|
93
|
+
- - ">="
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
97
|
requirements:
|
98
|
-
- -
|
98
|
+
- - ">="
|
99
99
|
- !ruby/object:Gem::Version
|
100
100
|
version: '0'
|
101
101
|
requirements: []
|
102
|
-
|
103
|
-
|
104
|
-
signing_key:
|
102
|
+
rubygems_version: 3.0.6
|
103
|
+
signing_key:
|
105
104
|
specification_version: 4
|
106
105
|
summary: Provides a DSL for defining, generating, and debugging fixed width documents.
|
107
106
|
test_files:
|