norma43_parser 1.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE.md +13 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +5 -0
  4. data/.github/workflows/ci.yml +22 -0
  5. data/.rubocop.yml +234 -0
  6. data/CHANGELOG.md +43 -0
  7. data/Gemfile +7 -1
  8. data/Gemfile.lock +72 -0
  9. data/README.md +6 -4
  10. data/Rakefile +2 -0
  11. data/lib/norma43/line_handlers.rb +2 -3
  12. data/lib/norma43/line_parsers/account_end.rb +18 -0
  13. data/lib/norma43/line_parsers/account_start.rb +18 -0
  14. data/lib/norma43/line_parsers/additional_currency.rb +12 -0
  15. data/lib/norma43/line_parsers/additional_item.rb +11 -0
  16. data/lib/norma43/line_parsers/document_end.rb +9 -0
  17. data/lib/norma43/line_parsers/document_start.rb +13 -0
  18. data/lib/norma43/line_parsers/file_format_validator.rb +15 -12
  19. data/lib/norma43/line_parsers/line_parser.rb +29 -28
  20. data/lib/norma43/line_parsers/transaction.rb +18 -0
  21. data/lib/norma43/line_processors.rb +2 -2
  22. data/lib/norma43/models.rb +5 -4
  23. data/lib/norma43/parser.rb +38 -42
  24. data/lib/norma43/utils/contexts.rb +40 -37
  25. data/lib/norma43/utils/string_helpers.rb +10 -6
  26. data/lib/norma43/utils/typecaster.rb +16 -12
  27. data/lib/norma43/version.rb +3 -1
  28. data/lib/norma43.rb +9 -3
  29. data/norma43_parser.gemspec +10 -10
  30. data/spec/example1_parse_spec.rb +12 -11
  31. data/spec/norma43/line_parsers/account_end_spec.rb +1 -1
  32. data/spec/norma43/line_parsers/account_start_spec.rb +1 -2
  33. data/spec/norma43/line_parsers/additional_currency_spec.rb +1 -1
  34. data/spec/norma43/line_parsers/additional_items_spec.rb +1 -1
  35. data/spec/norma43/line_parsers/document_end_spec.rb +1 -2
  36. data/spec/norma43/line_parsers/document_start_spec.rb +1 -1
  37. data/spec/norma43/line_parsers/transaction_spec.rb +1 -3
  38. data/spec/norma43/line_processors/account_end_spec.rb +6 -8
  39. data/spec/norma43/line_processors/account_start_spec.rb +8 -9
  40. data/spec/norma43/line_processors/additional_currency_spec.rb +6 -7
  41. data/spec/norma43/line_processors/additional_items_spec.rb +6 -7
  42. data/spec/norma43/line_processors/document_end_spec.rb +5 -6
  43. data/spec/norma43/line_processors/document_start_spec.rb +3 -4
  44. data/spec/norma43/line_processors/transaction_spec.rb +6 -7
  45. data/spec/norma43/parser_spec.rb +5 -4
  46. data/spec/norma43_spec.rb +2 -0
  47. data/spec/spec_helper.rb +1 -4
  48. data/spec/support/shared_examples_for_values_line_parsers.rb +2 -0
  49. metadata +41 -44
  50. data/lib/norma43/line_parsers/line_parsers.rb +0 -69
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4efe95db38bc547fc36d8f649c83f65a90a078b2
4
- data.tar.gz: 04d5b299d47f187c907aa2b15725a9adf96fe20b
2
+ SHA256:
3
+ metadata.gz: 6ccff4e77819687eda82453ce15f9c206e748eacf51f691a56744483280bd884
4
+ data.tar.gz: e6fcfc73c153129b7039bf9d129cf03bceb4ef0fd3652d49cf1a11cfac551a80
5
5
  SHA512:
6
- metadata.gz: 972ca63a8512c51a2dd6100cf5570d942bf39ed5bb4d97856231ece753a8dcf1de916505ac3019aa163c1fe581ea59a69256d9a3f9ea927354c13dc4338f0147
7
- data.tar.gz: 4893dd41e41ccd78f47f63fa10007b989576be1826fad09f9eb017a3604667ebb3dc33ea5a7a2f0b047638eab360e7c42535029bfaa3c0e085bf110f93f20a31
6
+ metadata.gz: 4ab4404a3c79574525fe743e5d794b8db610ab64b4a9581c839ca1c58fa8b80084c321a4b1e4d4dbe836834d8083de6f28c228e0edd3ff615ef2af290d532583
7
+ data.tar.gz: 4558ce5f03fda4bda0d267163d5aea24a3b21c11afee7ed7acef0de6e9f2a96d22b781092ff660b714585358b1859963743a854bf9b6a079d7e7933ecd7f13b1
@@ -0,0 +1,13 @@
1
+ ## Expected Behavior
2
+
3
+ ## Actual Behavior
4
+
5
+ ## Steps to Reproduce the Problem
6
+
7
+ 1.
8
+
9
+ ## Specifications
10
+
11
+ - Version:
12
+ - Platform:
13
+ - Subsystem:
@@ -0,0 +1,5 @@
1
+ Fixes #
2
+
3
+ ## Proposed Changes
4
+
5
+ -
@@ -0,0 +1,22 @@
1
+ name: Ruby CI
2
+
3
+ on: [ pull_request, push ]
4
+
5
+ jobs:
6
+ test:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ strategy:
11
+ matrix:
12
+ ruby-version: ["3.0", "2.7", "2.6", "jruby-head"]
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby ${{ matrix.ruby-version }}
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby-version }}
20
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
21
+ - name: Run tests
22
+ run: bundle exec rspec
data/.rubocop.yml ADDED
@@ -0,0 +1,234 @@
1
+ require:
2
+ - rubocop-performance
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.4
6
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
7
+ # to ignore them, so only the ones explicitly set in this file are enabled.
8
+ DisabledByDefault: true
9
+ Exclude:
10
+ - "**/tmp/**/*"
11
+ - "**/templates/**/*"
12
+ - "**/vendor/**/*"
13
+
14
+ Performance:
15
+ Exclude:
16
+ - "**/test/**/*"
17
+
18
+ # Prefer &&/|| over and/or.
19
+ Style/AndOr:
20
+ Enabled: true
21
+
22
+ # Align `when` with `case`.
23
+ Layout/CaseIndentation:
24
+ Enabled: true
25
+
26
+ # Align comments with method definitions.
27
+ Layout/CommentIndentation:
28
+ Enabled: true
29
+
30
+ Layout/ElseAlignment:
31
+ Enabled: true
32
+
33
+ # Align `end` with the matching keyword or starting expression except for
34
+ # assignments, where it should be aligned with the LHS.
35
+ Layout/EndAlignment:
36
+ Enabled: true
37
+ EnforcedStyleAlignWith: variable
38
+ AutoCorrect: true
39
+
40
+ Layout/EmptyLineAfterMagicComment:
41
+ Enabled: true
42
+
43
+ Layout/EmptyLinesAroundAccessModifier:
44
+ Enabled: true
45
+ EnforcedStyle: only_before
46
+
47
+ Layout/EmptyLinesAroundBlockBody:
48
+ Enabled: true
49
+
50
+ # In a regular class definition, no empty lines around the body.
51
+ Layout/EmptyLinesAroundClassBody:
52
+ Enabled: true
53
+
54
+ # In a regular method definition, no empty lines around the body.
55
+ Layout/EmptyLinesAroundMethodBody:
56
+ Enabled: true
57
+
58
+ # In a regular module definition, no empty lines around the body.
59
+ Layout/EmptyLinesAroundModuleBody:
60
+ Enabled: true
61
+
62
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
63
+ Style/HashSyntax:
64
+ Enabled: true
65
+
66
+ Layout/IndentFirstArgument:
67
+ Enabled: true
68
+
69
+ # Method definitions after `private` or `protected` isolated calls need one
70
+ # extra level of indentation.
71
+ Layout/IndentationConsistency:
72
+ Enabled: true
73
+ EnforcedStyle: indented_internal_methods
74
+
75
+ # Two spaces, no tabs (for indentation).
76
+ Layout/IndentationWidth:
77
+ Enabled: true
78
+
79
+ Layout/LeadingCommentSpace:
80
+ Enabled: true
81
+
82
+ Layout/SpaceAfterColon:
83
+ Enabled: true
84
+
85
+ Layout/SpaceAfterComma:
86
+ Enabled: true
87
+
88
+ Layout/SpaceAfterSemicolon:
89
+ Enabled: true
90
+
91
+ Layout/SpaceAroundEqualsInParameterDefault:
92
+ Enabled: true
93
+
94
+ Layout/SpaceAroundKeyword:
95
+ Enabled: true
96
+
97
+ Layout/SpaceBeforeComma:
98
+ Enabled: true
99
+
100
+ Layout/SpaceBeforeComment:
101
+ Enabled: true
102
+
103
+ Layout/SpaceBeforeFirstArg:
104
+ Enabled: true
105
+
106
+ Style/DefWithParentheses:
107
+ Enabled: true
108
+
109
+ # Defining a method with parameters needs parentheses.
110
+ Style/MethodDefParentheses:
111
+ Enabled: true
112
+
113
+ Style/FrozenStringLiteralComment:
114
+ Enabled: true
115
+ EnforcedStyle: always
116
+ Exclude:
117
+ - "actionview/test/**/*.builder"
118
+ - "actionview/test/**/*.ruby"
119
+ - "actionpack/test/**/*.builder"
120
+ - "actionpack/test/**/*.ruby"
121
+ - "activestorage/db/migrate/**/*.rb"
122
+ - "activestorage/db/update_migrate/**/*.rb"
123
+ - "actionmailbox/db/migrate/**/*.rb"
124
+ - "actiontext/db/migrate/**/*.rb"
125
+
126
+ Style/RedundantFreeze:
127
+ Enabled: true
128
+
129
+ # Use `foo {}` not `foo{}`.
130
+ Layout/SpaceBeforeBlockBraces:
131
+ Enabled: true
132
+
133
+ # Use `foo { bar }` not `foo {bar}`.
134
+ Layout/SpaceInsideBlockBraces:
135
+ Enabled: true
136
+ EnforcedStyleForEmptyBraces: space
137
+
138
+ # Use `{ a: 1 }` not `{a:1}`.
139
+ Layout/SpaceInsideHashLiteralBraces:
140
+ Enabled: true
141
+
142
+ Layout/SpaceInsideParens:
143
+ Enabled: true
144
+
145
+ # Check quotes usage according to lint rule below.
146
+ Style/StringLiterals:
147
+ Enabled: true
148
+ EnforcedStyle: double_quotes
149
+
150
+ # Detect hard tabs, no hard tabs.
151
+ Layout/Tab:
152
+ Enabled: true
153
+
154
+ # Blank lines should not have any spaces.
155
+ Layout/TrailingBlankLines:
156
+ Enabled: true
157
+
158
+ # No trailing whitespace.
159
+ Layout/TrailingWhitespace:
160
+ Enabled: true
161
+
162
+ # Use quotes for string literals when they are enough.
163
+ Style/UnneededPercentQ:
164
+ Enabled: true
165
+
166
+ Lint/AmbiguousOperator:
167
+ Enabled: true
168
+
169
+ Lint/AmbiguousRegexpLiteral:
170
+ Enabled: true
171
+
172
+ Lint/ErbNewArguments:
173
+ Enabled: true
174
+
175
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
176
+ Lint/RequireParentheses:
177
+ Enabled: true
178
+
179
+ Lint/ShadowingOuterLocalVariable:
180
+ Enabled: true
181
+
182
+ Lint/StringConversionInInterpolation:
183
+ Enabled: true
184
+
185
+ Lint/UriEscapeUnescape:
186
+ Enabled: true
187
+
188
+ Lint/UselessAssignment:
189
+ Enabled: true
190
+
191
+ Lint/DeprecatedClassMethods:
192
+ Enabled: true
193
+
194
+ Style/ParenthesesAroundCondition:
195
+ Enabled: true
196
+
197
+ Style/RedundantBegin:
198
+ Enabled: true
199
+
200
+ Style/RedundantReturn:
201
+ Enabled: true
202
+ AllowMultipleReturnValues: true
203
+
204
+ Style/Semicolon:
205
+ Enabled: true
206
+ AllowAsExpressionSeparator: true
207
+
208
+ # Prefer Foo.method over Foo::method
209
+ Style/ColonMethodCall:
210
+ Enabled: true
211
+
212
+ Style/TrivialAccessors:
213
+ Enabled: true
214
+
215
+ Performance/FlatMap:
216
+ Enabled: true
217
+
218
+ Performance/RedundantMerge:
219
+ Enabled: true
220
+
221
+ Performance/StartWith:
222
+ Enabled: true
223
+
224
+ Performance/EndWith:
225
+ Enabled: true
226
+
227
+ Performance/RegexpMatch:
228
+ Enabled: true
229
+
230
+ Performance/ReverseEach:
231
+ Enabled: true
232
+
233
+ Performance/UnfreezeString:
234
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,43 @@
1
+ ## 3.0.0 (2021-12-15)
2
+
3
+ ### Breaking Changes
4
+
5
+ - Drop Ruby support below 2.6.
6
+
7
+ ### Bug fixes
8
+
9
+ -
10
+
11
+ ### Enhancements
12
+
13
+ -
14
+
15
+ ## 2.1.1 (2020-03-13)
16
+
17
+ ### Breaking Changes
18
+
19
+ -
20
+
21
+ ### Bug fixes
22
+
23
+ -
24
+
25
+ ### Enhancements
26
+
27
+ - Update rake requirement from ~> 10.0 to ~> 13.0 (#6)
28
+
29
+ ## 2.0.0 (2019-10-17)
30
+
31
+ ### Breaking Changes
32
+
33
+ - Drop Ruby support below 2.4.
34
+
35
+ ### Bug fixes
36
+
37
+ -
38
+
39
+ ### Enhancements
40
+
41
+ - Clean up dependencies
42
+
43
+ ## 1.0.1 (2016-02-16)
data/Gemfile CHANGED
@@ -1,4 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
- gem "codeclimate-test-reporter", group: :test, require: nil
5
+ group :development, :test do
6
+ gem "rubocop", ">= 0.75", require: false
7
+ gem "rubocop-performance", require: false
8
+ end
9
+
4
10
  gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ norma43_parser (3.0.0)
5
+ virtus (~> 1.0)
6
+ zeitwerk (~> 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ axiom-types (0.1.1)
13
+ descendants_tracker (~> 0.0.4)
14
+ ice_nine (~> 0.11.0)
15
+ thread_safe (~> 0.3, >= 0.3.1)
16
+ coercible (1.0.0)
17
+ descendants_tracker (~> 0.0.1)
18
+ descendants_tracker (0.0.4)
19
+ thread_safe (~> 0.3, >= 0.3.1)
20
+ diff-lcs (1.3)
21
+ equalizer (0.0.11)
22
+ ice_nine (0.11.2)
23
+ jaro_winkler (1.5.3)
24
+ parallel (1.18.0)
25
+ parser (2.6.5.0)
26
+ ast (~> 2.4.0)
27
+ rainbow (3.0.0)
28
+ rake (13.0.1)
29
+ rspec (3.9.0)
30
+ rspec-core (~> 3.9.0)
31
+ rspec-expectations (~> 3.9.0)
32
+ rspec-mocks (~> 3.9.0)
33
+ rspec-core (3.9.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-expectations (3.9.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-mocks (3.9.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-support (3.9.0)
42
+ rubocop (0.75.0)
43
+ jaro_winkler (~> 1.5.1)
44
+ parallel (~> 1.10)
45
+ parser (>= 2.6)
46
+ rainbow (>= 2.2.2, < 4.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 1.7)
49
+ rubocop-performance (1.5.0)
50
+ rubocop (>= 0.71.0)
51
+ ruby-progressbar (1.10.1)
52
+ thread_safe (0.3.6)
53
+ unicode-display_width (1.6.0)
54
+ virtus (1.0.5)
55
+ axiom-types (~> 0.1)
56
+ coercible (~> 1.0)
57
+ descendants_tracker (~> 0.0, >= 0.0.3)
58
+ equalizer (~> 0.0, >= 0.0.9)
59
+ zeitwerk (2.5.1)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ norma43_parser!
66
+ rake (~> 13.0)
67
+ rspec (~> 3.0)
68
+ rubocop (>= 0.75)
69
+ rubocop-performance
70
+
71
+ BUNDLED WITH
72
+ 2.1.4
data/README.md CHANGED
@@ -1,20 +1,22 @@
1
1
  # Norma43 file parser
2
2
 
3
- Norma43 is the standard format file to extract bank transactions from the SEPA (Single euro payments area), format details and content is described in the [Norma43 format](https://github.com/sequra/norma43_parser/blob/master/doc/cuaderno_43_-_junio_2012.pdf).
3
+ [![Gem Version](https://badge.fury.io/rb/norma43_parser.svg)](https://badge.fury.io/rb/norma43_parser)
4
+
5
+ Norma43 is the standard format file to extract bank transactions from the SEPA (Single euro payments area), format details and content is described in the [Norma43 format](https://github.com/sequra/norma43_parser/blob/master/doc/cuaderno_43_-_junio_2012.pdf).
4
6
 
5
7
  ## Usage
6
8
 
7
9
  Include it in your Gemfile
10
+
8
11
  ```
9
12
  gem 'norma43_parser', git: "git@github.com:sequra/norma43_parser.git"
10
13
  ```
11
14
 
12
-
13
15
  ```ruby
14
16
  require "norma43"
15
17
 
16
18
  norma43_file_contents = File.open("path_to_file.n43", encoding: "iso-8859-1")
17
- document = norma43.parse norma43_file_contents
19
+ document = Norma43.parse norma43_file_contents
18
20
  ```
19
21
 
20
22
  ### Document
@@ -25,7 +27,7 @@ The parser returns a Norma43 Document that may include multiple accounts.
25
27
  accounts=[account1..accountN]
26
28
  ```
27
29
 
28
- ### Account
30
+ ### Account
29
31
 
30
32
  The account object has all the information described in the standard format and the transactions between **start_date** and **end_date**.
31
33
 
data/Rakefile CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
@@ -1,5 +1,4 @@
1
- require "norma43/line_parsers/line_parsers"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineHandlers
@@ -44,7 +43,7 @@ module Norma43
44
43
  end
45
44
 
46
45
  Handler = Struct.new :parser, :processor do
47
- def process line, contexts
46
+ def process(line, contexts)
48
47
  line_parser = self.parser.new(line)
49
48
 
50
49
  processor.call line_parser, contexts
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ class AccountEnd < LineParser
6
+ field :bank_code, 2..5, :integer
7
+ field :branch_code, 6..9, :integer
8
+ field :account_number, 10..19, :integer
9
+ field :debit_entries, 20..24, :integer
10
+ field :debit_amount, 25..38, :integer
11
+ field :credit_entries, 39..43, :integer
12
+ field :credit_amount, 44..57, :integer
13
+ field :balance_code, 58, :integer
14
+ field :balance_amount, 59..72, :integer
15
+ field :currency_code, 73..75, :integer
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ class AccountStart < LineParser
6
+ field :bank_code, 2..5, :integer
7
+ field :branch_code, 6..9, :integer
8
+ field :account_number, 10..19, :integer
9
+ field :start_date, 20..25, :date
10
+ field :end_date, 26..31, :date
11
+ field :balance_code, 32, :integer
12
+ field :balance_amount, 33..46, :integer
13
+ field :currency_code, 47..49, :integer
14
+ field :information_mode_code, 50, :integer
15
+ field :abbreviated_name, 51..76
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ class AdditionalCurrency < LineParser
6
+ field :data_code, 2..3, :integer
7
+ field :currency_code, 4..6, :integer
8
+ field :amount, 7..20, :integer
9
+ field :free, 21..79
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ class AdditionalItem < LineParser
6
+ field :data_code, 2..3, :integer
7
+ field :item_1, 4..41
8
+ field :item_2, 42..79
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ class DocumentEnd < LineParser
6
+ field :record_number, 20..25, :integer
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ class DocumentStart < LineParser
6
+ field :id, 2..13
7
+ field :created_at, 14..33, :time
8
+ field :delivery_number, 34..35, :integer
9
+ field :file_type, 36..38
10
+ field :name, 39..48
11
+ end
12
+ end
13
+ end
@@ -1,19 +1,22 @@
1
- require "norma43/line_parsers/line_parser"
1
+ # frozen_string_literal: true
2
+
2
3
  module Norma43
3
- class FileFormatValidator < LineParser
4
- field :record_type, 0..1, :raw
5
- field :file_type, 36..38
4
+ module LineParsers
5
+ class FileFormatValidator < LineParser
6
+ field :record_type, 0..1, :raw
7
+ field :file_type, 36..38
6
8
 
7
- def has_document?; file_type=="00" end
9
+ def has_document?; file_type == "00" end
8
10
 
9
- def valid?
10
- errors.empty?
11
- end
11
+ def valid?
12
+ errors.empty?
13
+ end
12
14
 
13
- def errors
14
- errors = []
15
- %w(11 00).include? record_type or errors << "Must start with 00 (was ”#{record_type}”)"
16
- errors
15
+ def errors
16
+ errors = []
17
+ %w(11 00).include?(record_type) || errors << ("Must start with 00 (was ”#{record_type}”)")
18
+ errors
19
+ end
17
20
  end
18
21
  end
19
22
  end
@@ -1,42 +1,43 @@
1
- require "norma43/utils/typecaster"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
- class LineParser
5
- attr_reader :line
6
- def initialize line
7
- @line = line
8
- end
4
+ module LineParsers
5
+ class LineParser
6
+ attr_reader :line
7
+ def initialize(line)
8
+ @line = line
9
+ end
9
10
 
10
- def attributes
11
- self.class.field_names.each_with_object({}) do |field, attrs|
12
- attrs[field] = self.public_send(field)
11
+ def attributes
12
+ self.class.field_names.each_with_object({}) do |field, attrs|
13
+ attrs[field] = self.public_send(field)
14
+ end
13
15
  end
14
- end
15
16
 
16
- def self.field name, range, type = :string
17
- self.field_names.push name
17
+ def self.field(name, range, type = :string)
18
+ self.field_names.push name
18
19
 
19
- define_method name do
20
- if range.is_a?(Array) # let multivalued attribute
21
- range.map { |r| value_at_position(r, type) }.compact
22
- else
23
- value_at_position range, type
20
+ define_method name do
21
+ if range.is_a?(Array) # let multivalued attribute
22
+ range.map { |r| value_at_position(r, type) }.compact
23
+ else
24
+ value_at_position range, type
25
+ end
24
26
  end
25
27
  end
26
- end
27
-
28
- private
29
28
 
30
- def self.field_names
31
- @field_names ||= []
32
- end
29
+ private
30
+ def self.field_names
31
+ @field_names ||= []
32
+ end
33
33
 
34
- def value_at_position range, type
35
- typecast line[range].to_s.strip, type
36
- end
34
+ def value_at_position(range, type)
35
+ typecast line[range].to_s.strip, type
36
+ end
37
37
 
38
- def typecast value, type
39
- Typecaster.cast value, type
38
+ def typecast(value, type)
39
+ Norma43::Utils::Typecaster.cast value, type
40
+ end
40
41
  end
41
42
  end
42
43
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ class Transaction < LineParser
6
+ field :origin_branch_code, 6..9, :integer
7
+ field :transaction_date, 10..15, :date
8
+ field :value_date, 16..21, :date
9
+ field :shared_item, 22..23, :integer
10
+ field :own_item, 24..26, :integer
11
+ field :amount_code, 27, :integer
12
+ field :amount, 28..41, :integer
13
+ field :document_number, 42..51, :integer
14
+ field :reference_1, 52..63, :integer
15
+ field :reference_2, 64..79
16
+ end
17
+ end
18
+ end