Class: Eancom::Edifact::Segment
- Inherits:
-
Object
- Object
- Eancom::Edifact::Segment
show all
- Defined in:
- lib/eancom/edifact/segment.rb
Direct Known Subclasses
AJT, ALC, ALI, BGM, CAV, CCI, CDI, CNT, CPS, CTA, CUX, DGS, DOC, DTM, EFI, EQD, FTX, GIN, IMD, INV, LIN, LOC, MEA, MOA, NAD, PAC, PAT, PCD, PCI, PGI, PIA, PRI, QTY, RFF, RTE, TAX, TDT, TOD, TRU, UNA, UNB, UNH, UNS, UNT, UNZ
Defined Under Namespace
Classes: SegmentParserError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tag:) ⇒ Segment
Returns a new instance of Segment.
8
9
10
11
|
# File 'lib/eancom/edifact/segment.rb', line 8
def initialize(tag:)
@tag = tag
validate_structure
end
|
Instance Attribute Details
#tag ⇒ Object
Returns the value of attribute tag.
4
5
6
|
# File 'lib/eancom/edifact/segment.rb', line 4
def tag
@tag
end
|
Class Method Details
.initialize_by_components(structure, array) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/eancom/edifact/segment.rb', line 13
def self.initialize_by_components(structure, array)
begin
new(**structure.build_hash(array))
rescue StandardError => e
raise SegmentParserError.new(
"'Parser Error in structure #{structure.tag} with array #{array}.\n \
'Inside Structure: \n \ '#{structure.to_s}.\n \
'Error:\n \
'#{e}"
)
end
end
|
Instance Method Details
#array ⇒ Object
33
34
35
|
# File 'lib/eancom/edifact/segment.rb', line 33
def array
@array ||= structure.build_array(to_hash)
end
|
#group_name ⇒ Object
92
93
94
|
# File 'lib/eancom/edifact/segment.rb', line 92
def group_name
nil
end
|
#is_body? ⇒ Boolean
72
73
74
|
# File 'lib/eancom/edifact/segment.rb', line 72
def is_body?
segment_type == :body
end
|
76
77
78
|
# File 'lib/eancom/edifact/segment.rb', line 76
def
segment_type == :footer
end
|
68
69
70
|
# File 'lib/eancom/edifact/segment.rb', line 68
def
segment_type == :header
end
|
#item_group_name ⇒ Object
96
97
98
|
# File 'lib/eancom/edifact/segment.rb', line 96
def item_group_name
nil
end
|
#segment_type ⇒ Object
60
61
62
|
# File 'lib/eancom/edifact/segment.rb', line 60
def segment_type
raise NotImplementedError
end
|
#starts_item? ⇒ Boolean
88
89
90
|
# File 'lib/eancom/edifact/segment.rb', line 88
def starts_item?
false
end
|
#starts_message? ⇒ Boolean
84
85
86
|
# File 'lib/eancom/edifact/segment.rb', line 84
def starts_message?
false
end
|
#structure ⇒ Object
37
38
39
|
# File 'lib/eancom/edifact/segment.rb', line 37
def structure
@structure ||= Eancom.find_structure(tag: tag)
end
|
#tag?(other) ⇒ Boolean
80
81
82
|
# File 'lib/eancom/edifact/segment.rb', line 80
def tag?(other)
tag == other
end
|
#to_edi ⇒ Object
52
53
54
|
# File 'lib/eancom/edifact/segment.rb', line 52
def to_edi
to_s
end
|
#to_hash ⇒ Object
64
65
66
|
# File 'lib/eancom/edifact/segment.rb', line 64
def to_hash
attributes
end
|
#to_json_hash ⇒ Object
56
57
58
|
# File 'lib/eancom/edifact/segment.rb', line 56
def to_json_hash
raise NotImplementedError
end
|
#to_s ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/eancom/edifact/segment.rb', line 41
def to_s
string = array.map do |e|
e = e.compact
next if e.empty?
[e.join(component_delimiter)]
end.join(data_delimiter)
clean_end_string(string)
string << segment_delimiter
string
end
|
#validate_structure ⇒ Object
26
27
28
29
30
31
|
# File 'lib/eancom/edifact/segment.rb', line 26
def validate_structure
attributes.each do |key, value|
next if value.nil?
structure.validate!(key, value)
end
end
|