Class: Eancom::Edifact::Segment
- Inherits:
-
Object
- Object
- Eancom::Edifact::Segment
show all
- Defined in:
- lib/eancom/edifact/segment.rb
Direct Known Subclasses
ALC, ALI, BGM, CAV, CCI, CNT, CPS, CUX, DTM, FTX, IMD, LIN, LOC, MEA, MOA, NAD, PAC, PCD, PGI, PIA, PRI, QTY, QVR, RFF, TAX, TDT, 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
|
# File 'lib/eancom/edifact/segment.rb', line 13
def self.initialize_by_components(structure, array)
new(**structure.build_hash(array))
rescue StandardError => e
raise SegmentParserError, "'Parser Error in structure #{structure.tag} with array #{array}.\n \
'Inside Structure: \n \ '#{structure}.\n \
'Error:\n \
'#{e}"
end
|
Instance Method Details
#array ⇒ Object
30
31
32
|
# File 'lib/eancom/edifact/segment.rb', line 30
def array
@array ||= structure.build_array(to_hash)
end
|
#group_name ⇒ Object
94
95
96
|
# File 'lib/eancom/edifact/segment.rb', line 94
def group_name
nil
end
|
#is_body? ⇒ Boolean
70
71
72
|
# File 'lib/eancom/edifact/segment.rb', line 70
def is_body?
segment_type == :body
end
|
74
75
76
|
# File 'lib/eancom/edifact/segment.rb', line 74
def
segment_type == :footer
end
|
66
67
68
|
# File 'lib/eancom/edifact/segment.rb', line 66
def
segment_type == :header
end
|
#item_group_name ⇒ Object
98
99
100
|
# File 'lib/eancom/edifact/segment.rb', line 98
def item_group_name
nil
end
|
#segment_type ⇒ Object
58
59
60
|
# File 'lib/eancom/edifact/segment.rb', line 58
def segment_type
raise NotImplementedError
end
|
#starts_item? ⇒ Boolean
90
91
92
|
# File 'lib/eancom/edifact/segment.rb', line 90
def starts_item?
false
end
|
#starts_location? ⇒ Boolean
86
87
88
|
# File 'lib/eancom/edifact/segment.rb', line 86
def starts_location?
false
end
|
#starts_message? ⇒ Boolean
82
83
84
|
# File 'lib/eancom/edifact/segment.rb', line 82
def starts_message?
false
end
|
#structure ⇒ Object
34
35
36
|
# File 'lib/eancom/edifact/segment.rb', line 34
def structure
@structure ||= Eancom.find_structure(tag: tag)
end
|
#tag?(other) ⇒ Boolean
78
79
80
|
# File 'lib/eancom/edifact/segment.rb', line 78
def tag?(other)
tag == other
end
|
#to_edi ⇒ Object
50
51
52
|
# File 'lib/eancom/edifact/segment.rb', line 50
def to_edi
to_s
end
|
#to_hash ⇒ Object
62
63
64
|
# File 'lib/eancom/edifact/segment.rb', line 62
def to_hash
attributes
end
|
#to_json_hash ⇒ Object
54
55
56
|
# File 'lib/eancom/edifact/segment.rb', line 54
def to_json_hash
raise NotImplementedError
end
|
#to_s ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/eancom/edifact/segment.rb', line 38
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
22
23
24
25
26
27
28
|
# File 'lib/eancom/edifact/segment.rb', line 22
def validate_structure
attributes.each do |key, value|
next if value.nil?
structure.validate!(key, value)
end
end
|