Class: Eancom::Edifact::Segment

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

#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
# 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

#arrayObject



30
31
32
# File 'lib/eancom/edifact/segment.rb', line 30

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

#group_nameObject



94
95
96
# File 'lib/eancom/edifact/segment.rb', line 94

def group_name
  nil
end

#is_body?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/eancom/edifact/segment.rb', line 70

def is_body?
  segment_type == :body
end

#is_footer?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/eancom/edifact/segment.rb', line 74

def is_footer?
  segment_type == :footer
end

#is_header?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/eancom/edifact/segment.rb', line 66

def is_header?
  segment_type == :header
end

#item_group_nameObject



98
99
100
# File 'lib/eancom/edifact/segment.rb', line 98

def item_group_name
  nil
end

#segment_typeObject

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/eancom/edifact/segment.rb', line 58

def segment_type
  raise NotImplementedError
end

#starts_item?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/eancom/edifact/segment.rb', line 90

def starts_item?
  false
end

#starts_location?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/eancom/edifact/segment.rb', line 86

def starts_location?
  false
end

#starts_message?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/eancom/edifact/segment.rb', line 82

def starts_message?
  false
end

#structureObject



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

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

#tag?(other) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/eancom/edifact/segment.rb', line 78

def tag?(other)
  tag == other
end

#to_ediObject



50
51
52
# File 'lib/eancom/edifact/segment.rb', line 50

def to_edi
  to_s
end

#to_hashObject



62
63
64
# File 'lib/eancom/edifact/segment.rb', line 62

def to_hash
  attributes
end

#to_json_hashObject

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/eancom/edifact/segment.rb', line 54

def to_json_hash
  raise NotImplementedError
end

#to_sObject



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_structureObject



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