baldr 0.3.17 → 0.3.18

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b548c93e742ee6bd486ef79470d4a34936af2978
4
- data.tar.gz: b0f55945aaaeedacb4dd8b37a75fdb72dead4837
3
+ metadata.gz: 9c26c11785b0b55caeee6ed6ff6cdb36d74c3760
4
+ data.tar.gz: 6b51462b8ab2585827b32835a5a57ac4b2028714
5
5
  SHA512:
6
- metadata.gz: 297a42812b59a8bbdae1f65e35cd630d2b899fea18aa36b00ef2ecd3be597345c46674be0de654b2677a132d0df1a1bfd46f56562a106de169d7e1a692ee44f6
7
- data.tar.gz: cadcba49c7eb0246b76ec12db50f4f20eb9080648b2ae36e2dd76b8a8d40411662f3f72f336a7c7f14c08ea78b7c8133de2cc23272ca9378583daeb8822abdbf
6
+ metadata.gz: 445a6079b0d3c44bd39c5a1a0f25987d673f913f8d5f98645370d1888ca6ecc2c0277e3a21c26b85c695c29996692776949337d3fb880b3394265efd1db04dc2
7
+ data.tar.gz: 172a1e6f0a406865f846aa13d1b6dcdd0d8def5aa89e6f701567f03d2fc79a64de5cc000feca4c7f1b5850bb4555646b30386f4340fa6257e199c2ba0b766b9d
@@ -30,8 +30,8 @@ class Baldr::FunctionalGroup < Baldr::Segment
30
30
  trailer['GE02'] = group_control_number
31
31
  end
32
32
 
33
- def sub_version
34
- Baldr::Grammar.for_standard_version(version_release_industry_code)
33
+ def sub_version(grammar)
34
+ grammar.for_standard_version(version_release_industry_code)
35
35
  end
36
36
 
37
37
  def transactions
@@ -10,6 +10,12 @@ module Baldr::Grammar::Envelope
10
10
  RECORD_DEFS
11
11
  end
12
12
 
13
+ def sub_grammar(segment)
14
+ if segment.id == 'GS'
15
+ Baldr::Grammar.for_standard_version(segment.version_release_industry_code)
16
+ end
17
+ end
18
+
13
19
  def validate_st!(segment)
14
20
  trailer = segment.children.last.segments.first
15
21
  total_number = segment.number_of_segments
@@ -40,20 +46,17 @@ module Baldr::Grammar::Envelope
40
46
  def validate_isa!(segment)
41
47
  trailer = segment.children.last.segments.first
42
48
  if trailer['IEA01'].to_i != segment.func_group_loop.segments.count
43
- raise "wrong functional groups number: #{trailer['IEA01']} in IEA01, but real number is #{segment.func_group_loop.segments.count}"
49
+ raise Baldr::Error, "wrong functional groups number: #{trailer['IEA01']} in IEA01, but real number is #{segment.func_group_loop.segments.count}"
44
50
  end
45
51
  if trailer['IEA02'] != segment.interchange_control_number
46
- raise "interchange control numbers don't match: #{trailer['IEA02']} in IEA02 and #{segment.interchange_control_number} in ISA13"
52
+ raise Baldr::Error, "interchange control numbers don't match: #{trailer['IEA02']} in IEA02 and #{segment.interchange_control_number} in ISA13"
47
53
  end
48
54
  end
49
55
 
50
56
  STRUCTURE = {
51
57
  id: 'ISA', min: 0, max: 99999, class: :envelope, level: [
52
58
  {id: 'TA1', min: 0, max: 99999},
53
- {id: 'GS', min: 0, class: :functional_group, max: 99999, level: [
54
- {id: 'ST', min: 0, class: :transaction, max: 99999},
55
- {id: 'GE', min: 1, max: 1},
56
- ]},
59
+ {id: 'GS', min: 0, class: :functional_group, max: 99999},
57
60
  {id: 'IEA', min: 1, max: 1}
58
61
  ]
59
62
  }.freeze
@@ -91,10 +94,10 @@ module Baldr::Grammar::Envelope
91
94
  {id: 'GE01', required: true, max: 6, type: :number, decimals: 0},
92
95
  {id: 'GE02', required: true, max: 9, type: :number, decimals: 0},
93
96
  ],
94
- 'ST' => [
95
- {id: 'ST01', required: true, max: 3, type: :id},
96
- {id: 'ST02', required: true, min: 4, max: 9, type: :string},
97
- ],
97
+ # 'ST' => [
98
+ # {id: 'ST01', required: true, max: 3, type: :id},
99
+ # {id: 'ST02', required: true, min: 4, max: 9, type: :string},
100
+ # ],
98
101
  'IEA' => [
99
102
  {id: 'IEA01', required: true, max: 5, type: :number, decimals: 0},
100
103
  {id: 'IEA02', required: true, max: 9, type: :number, decimals: 0},
@@ -1,6 +1,14 @@
1
1
  module Baldr::Grammar::Version4010
2
2
 
3
- def self.for_transaction_set(id)
3
+ extend self
4
+
5
+ def sub_grammar(segment)
6
+ if segment.id == 'ST'
7
+ for_transaction_set(segment.transaction_set_code)
8
+ end
9
+ end
10
+
11
+ def for_transaction_set(id)
4
12
  const_name = "Set#{id}"
5
13
  if self.const_defined?(const_name)
6
14
  self.const_get(const_name)
@@ -9,6 +17,21 @@ module Baldr::Grammar::Version4010
9
17
  end
10
18
  end
11
19
 
20
+ def record_defs
21
+ RECORD_DEFS
22
+ end
23
+
24
+ def structure
25
+ STRUCTURE
26
+ end
27
+
28
+ STRUCTURE = {
29
+ id: 'GS', min: 0, class: :functional_group, max: 99999, level: [
30
+ {id: 'ST', min: 0, class: :transaction, max: 99999},
31
+ {id: 'GE', min: 1, max: 1},
32
+ ]
33
+ }.freeze
34
+
12
35
  RECORD_DEFS = {
13
36
  'AAA' => [
14
37
  {id: 'AAA01', required: true, max: 1, type: :id},
data/lib/baldr/parser.rb CHANGED
@@ -1,14 +1,15 @@
1
1
  class Baldr::Parser
2
2
 
3
- attr_reader :error, :separators, :input, :root_type
3
+ attr_reader :error, :separators, :input, :root_type, :grammar
4
4
 
5
5
  def initialize(input, params = {})
6
- @params = params
7
6
  @input = input
8
-
7
+ @grammar = params[:grammar]
8
+ @separators = params[:separators]
9
+
9
10
  parse
10
11
 
11
- if @params[:skip_validation] != true && successful?
12
+ if params[:skip_validation] != true && successful?
12
13
  validate
13
14
  end
14
15
  end
@@ -35,8 +36,8 @@ class Baldr::Parser
35
36
 
36
37
  def validate
37
38
  if @roots
38
- @roots.each { |e| Baldr::Validator.validate!(e, @grammar, @version) }
39
- @roots.each { |e| Baldr::Types.convert_after_parse!(e, @grammar, @version) }
39
+ @roots.each { |e| Baldr::Validator.validate!(e, @grammar) }
40
+ @roots.each { |e| Baldr::Types.convert_after_parse!(e, @grammar) }
40
41
  end
41
42
  rescue Baldr::Error => e
42
43
  @error = e.message
@@ -45,30 +46,26 @@ class Baldr::Parser
45
46
  protected
46
47
 
47
48
  def parse
48
- detect_first_segment
49
+ @root_type = detect_first_segment
49
50
  if @root_type == :transaction
50
- raise Baldr::Error::ParsingError, "separators must be manually defined for parsing transactions" unless @params[:separators]
51
- @separators = @params[:separators]
52
- @grammar = @params[:grammar] || Baldr::Grammar::Transaction
53
- raise Baldr::Error::ParsingError, "grammar version must be manually defined for parsing transactions" unless @params[:version]
54
- @version = @params[:version]
51
+ raise Baldr::Error::ParsingError, "separators must be manually defined for parsing transactions" unless @separators
52
+ @grammar ||= Baldr::Grammar::Transaction
53
+ elsif @root_type == :envelope
54
+ @separators ||= detect_separators
55
+ @grammar ||= Baldr::Grammar::Envelope
55
56
  else
56
- detect_separators
57
- @grammar = @params[:grammar] || Baldr::Grammar::Envelope
58
- @version = nil
57
+ raise Baldr::Error::ParsingError, "doesn't begin with ISA or ST..."
59
58
  end
60
- @roots = build_tree(split_segments, @grammar, @version)
59
+ @roots = build_tree(split_segments(@separators), @grammar)
61
60
  rescue Baldr::Error => e
62
61
  @error = e.message
63
62
  end
64
63
 
65
64
  def detect_first_segment
66
65
  if @input[0..2] == 'ISA'
67
- @root_type = :envelope
66
+ :envelope
68
67
  elsif @input[0..1] == 'ST'
69
- @root_type = :transaction
70
- else
71
- raise Baldr::Error::ParsingError, "doesn't begin with ISA or ST..."
68
+ :transaction
72
69
  end
73
70
  end
74
71
 
@@ -87,14 +84,14 @@ class Baldr::Parser
87
84
  segment << b
88
85
  end
89
86
 
90
- @separators = {
87
+ {
91
88
  element: element.chr,
92
89
  segment: segment,
93
90
  component: component.chr
94
91
  }
95
92
  end
96
93
 
97
- def split_segments
94
+ def split_segments(separators)
98
95
  segments = []
99
96
  buffer = []
100
97
  skip = 0
@@ -104,9 +101,9 @@ class Baldr::Parser
104
101
  if skip > 0
105
102
  skip -= 1
106
103
  else
107
- if b == @separators[:segment].first
108
- segments << buffer.pack('c*').split(@separators[:element])
109
- skip = @separators[:segment].length - 1
104
+ if b == separators[:segment].first
105
+ segments << buffer.pack('c*').split(separators[:element])
106
+ skip = separators[:segment].length - 1
110
107
  buffer = []
111
108
  else
112
109
  buffer << b
@@ -119,12 +116,12 @@ class Baldr::Parser
119
116
  segments
120
117
  end
121
118
 
122
- def build_tree(source, grammar, version = nil)
123
- loop = build_segment(source.to_enum, grammar.structure, version)
119
+ def build_tree(source, grammar)
120
+ loop = build_segment(source.to_enum, grammar, grammar.structure)
124
121
  loop.segments
125
122
  end
126
123
 
127
- def build_segment(enumerator, structure, version)
124
+ def build_segment(enumerator, grammar, structure)
128
125
  current = enumerator.peek
129
126
 
130
127
  while structure[:id] == current[0]
@@ -135,17 +132,16 @@ class Baldr::Parser
135
132
 
136
133
  enumerator.next
137
134
 
138
- version ||= segment.sub_version
139
- sub_grammar = segment.sub_grammar(version)
135
+ sub_grammar = grammar.sub_grammar(segment) if grammar.respond_to?(:sub_grammar)
140
136
 
141
137
  if sub_grammar
142
138
  sub_grammar.structure.fetch(:level, []).each do |s|
143
- child = build_segment(enumerator, s, version)
139
+ child = build_segment(enumerator, sub_grammar, s)
144
140
  segment.children << child if child
145
141
  end
146
142
  else
147
143
  structure.fetch(:level, []).each do |s|
148
- child = build_segment(enumerator, s, version)
144
+ child = build_segment(enumerator, grammar, s)
149
145
  segment.children << child if child
150
146
  end
151
147
  end
data/lib/baldr/segment.rb CHANGED
@@ -102,11 +102,11 @@ class Baldr::Segment
102
102
  end
103
103
  end
104
104
 
105
- def sub_version
105
+ def sub_version(grammar)
106
106
 
107
107
  end
108
108
 
109
- def sub_grammar(version)
109
+ def sub_grammar(grammar, version)
110
110
 
111
111
  end
112
112
 
@@ -18,8 +18,8 @@ class Baldr::Transaction < Baldr::Segment
18
18
  trailer['SE02'] = transaction_control_number
19
19
  end
20
20
 
21
- def sub_grammar(version)
22
- version.for_transaction_set(transaction_set_code)
21
+ def sub_grammar(grammar, version)
22
+ grammar.for_transaction_set(transaction_set_code)
23
23
  end
24
24
 
25
25
  def functional_group(version)
data/lib/baldr/types.rb CHANGED
@@ -2,9 +2,8 @@ module Baldr::Types
2
2
 
3
3
  extend self
4
4
 
5
- def convert_after_load!(envelope, grammar = nil, version = nil)
6
- grammar ||= Baldr::Grammar::Envelope
7
- convert_tree!(envelope, grammar, version) do |record_def, element|
5
+ def convert_after_load!(envelope, grammar)
6
+ convert_tree!(envelope, grammar) do |record_def, element|
8
7
  case record_def[:type]
9
8
  when :number
10
9
  if element.is_a? String
@@ -22,9 +21,8 @@ module Baldr::Types
22
21
  end
23
22
  end
24
23
 
25
- def convert_after_parse!(envelope, grammar = nil, version = nil)
26
- grammar ||= Baldr::Grammar::Envelope
27
- convert_tree!(envelope, grammar, version) do |record_def, element|
24
+ def convert_after_parse!(envelope, grammar)
25
+ convert_tree!(envelope, grammar) do |record_def, element|
28
26
  case record_def[:type]
29
27
  when :number
30
28
  if element.is_a? String
@@ -42,9 +40,8 @@ module Baldr::Types
42
40
  end
43
41
  end
44
42
 
45
- def convert_before_render!(envelope, grammar = nil, version = nil)
46
- grammar ||= Baldr::Grammar::Envelope
47
- convert_tree!(envelope, grammar, version) do |record_def, element|
43
+ def convert_before_render!(envelope, grammar)
44
+ convert_tree!(envelope, grammar) do |record_def, element|
48
45
  case record_def[:type]
49
46
  when :number
50
47
  if element.is_a? String
@@ -64,7 +61,7 @@ module Baldr::Types
64
61
 
65
62
  protected
66
63
 
67
- def convert_tree!(segment, grammar, version, &block)
64
+ def convert_tree!(segment, grammar, &block)
68
65
  record_defs = grammar.record_defs
69
66
 
70
67
  record_defs[segment.id].each.with_index do |r, i|
@@ -73,12 +70,11 @@ module Baldr::Types
73
70
  end
74
71
  end
75
72
 
76
- version ||= segment.sub_version
77
- sub_grammar = segment.sub_grammar(version)
73
+ sub_grammar = grammar.sub_grammar(segment) if grammar.respond_to?(:sub_grammar)
78
74
 
79
75
  segment.children.each do |loop|
80
76
  loop.segments.each do |child|
81
- convert_tree!(child, sub_grammar || grammar, version, &block)
77
+ convert_tree!(child, sub_grammar || grammar, &block)
82
78
  end
83
79
  end
84
80
  end
@@ -2,14 +2,13 @@ module Baldr::Validator
2
2
 
3
3
  extend self
4
4
 
5
- def validate!(envelope, grammar = nil, version = nil)
6
- grammar ||= Baldr::Grammar::Envelope
7
- validate_tree!(envelope, grammar, grammar.structure, version)
5
+ def validate!(envelope, grammar)
6
+ validate_tree!(envelope, grammar, grammar.structure)
8
7
  end
9
8
 
10
9
  protected
11
10
 
12
- def validate_tree!(segment, grammar, structure, version)
11
+ def validate_tree!(segment, grammar, structure)
13
12
  record_defs = grammar.record_defs
14
13
  raise Baldr::Error::ValidationError, "unknown segment #{segment.id}" unless record_defs[segment.id]
15
14
 
@@ -19,8 +18,7 @@ module Baldr::Validator
19
18
  self.send("check_#{r[:type]}", r, element) unless element.nil?
20
19
  end
21
20
 
22
- version ||= segment.sub_version
23
- sub_grammar = segment.sub_grammar(version)
21
+ sub_grammar = grammar.sub_grammar(segment) if grammar.respond_to?(:sub_grammar)
24
22
  structure = sub_grammar.structure if sub_grammar
25
23
 
26
24
  l = 0
@@ -29,7 +27,7 @@ module Baldr::Validator
29
27
  if loop && loop.id.to_s == s[:id]
30
28
  check_loop_count(loop, s)
31
29
 
32
- loop.segments.each { |child| validate_tree!(child, sub_grammar || grammar, s, version) }
30
+ loop.segments.each { |child| validate_tree!(child, sub_grammar || grammar, s) }
33
31
 
34
32
  l += 1
35
33
  elsif loop
@@ -49,9 +47,9 @@ module Baldr::Validator
49
47
  end
50
48
  end
51
49
 
52
- def check_loop_count(loop, grammar)
53
- raise Baldr::Error::ValidationError, "#{loop.id} loop is too long: #{loop.count} segments, maximum #{grammar[:max]}" if loop.count > grammar[:max]
54
- raise Baldr::Error::ValidationError, "#{loop.id} loop is too short: #{loop.count} segments, minimum #{grammar[:min]}" if loop.count < grammar[:min]
50
+ def check_loop_count(loop, s)
51
+ raise Baldr::Error::ValidationError, "#{loop.id} loop is too long: #{loop.count} segments, maximum #{s[:max]}" if loop.count > s[:max]
52
+ raise Baldr::Error::ValidationError, "#{loop.id} loop is too short: #{loop.count} segments, minimum #{s[:min]}" if loop.count < s[:min]
55
53
  end
56
54
 
57
55
  def check_required(r, element)
data/lib/baldr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Baldr
2
- VERSION = '0.3.17'
2
+ VERSION = '0.3.18'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baldr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.17
4
+ version: 0.3.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stanislav Spiridonov