Class: Eancom::Edifact::Segment

Inherits:
Object
  • Object
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

#tagObject

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

#arrayObject



33
34
35
# File 'lib/eancom/edifact/segment.rb', line 33

def array
  @array ||= structure.build_array(to_hash)
end

#group_nameObject



92
93
94
# File 'lib/eancom/edifact/segment.rb', line 92

def group_name
  nil
end

#is_body?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/eancom/edifact/segment.rb', line 72

def is_body?
  segment_type == :body
end

#is_footer?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/eancom/edifact/segment.rb', line 76

def is_footer?
  segment_type == :footer
end

#is_header?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/eancom/edifact/segment.rb', line 68

def is_header?
  segment_type == :header
end

#item_group_nameObject



96
97
98
# File 'lib/eancom/edifact/segment.rb', line 96

def item_group_name
  nil
end

#segment_typeObject

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/eancom/edifact/segment.rb', line 60

def segment_type
  raise NotImplementedError
end

#starts_item?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/eancom/edifact/segment.rb', line 88

def starts_item?
  false
end

#starts_message?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/eancom/edifact/segment.rb', line 84

def starts_message?
  false
end

#structureObject



37
38
39
# File 'lib/eancom/edifact/segment.rb', line 37

def structure
  @structure ||= Eancom.find_structure(tag: tag)
end

#tag?(other) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/eancom/edifact/segment.rb', line 80

def tag?(other)
  tag == other
end

#to_ediObject



52
53
54
# File 'lib/eancom/edifact/segment.rb', line 52

def to_edi
  to_s
end

#to_hashObject



64
65
66
# File 'lib/eancom/edifact/segment.rb', line 64

def to_hash
  attributes
end

#to_json_hashObject

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/eancom/edifact/segment.rb', line 56

def to_json_hash
  raise NotImplementedError
end

#to_sObject



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_structureObject



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