class Eancom::Edifact::Segment

Attributes

tag[RW]

Public Class Methods

initialize_by_components(structure, array) click to toggle source
# 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
new(tag:) click to toggle source
# File lib/eancom/edifact/segment.rb, line 8
def initialize(tag:)
  @tag = tag
  validate_structure
end

Public Instance Methods

array() click to toggle source
# File lib/eancom/edifact/segment.rb, line 33
def array
  @array ||= structure.build_array(to_hash)
end
group_name() click to toggle source
# File lib/eancom/edifact/segment.rb, line 92
def group_name
  nil
end
is_body?() click to toggle source
# File lib/eancom/edifact/segment.rb, line 72
def is_body?
  segment_type == :body
end
is_header?() click to toggle source
# File lib/eancom/edifact/segment.rb, line 68
def is_header?
  segment_type == :header
end
item_group_name() click to toggle source
# File lib/eancom/edifact/segment.rb, line 96
def item_group_name
  nil
end
segment_type() click to toggle source
# File lib/eancom/edifact/segment.rb, line 60
def segment_type
  raise NotImplementedError
end
starts_item?() click to toggle source
# File lib/eancom/edifact/segment.rb, line 88
def starts_item?
  false
end
starts_message?() click to toggle source
# File lib/eancom/edifact/segment.rb, line 84
def starts_message?
  false
end
structure() click to toggle source
# File lib/eancom/edifact/segment.rb, line 37
def structure
  @structure ||= Eancom.find_structure(tag: tag)
end
tag?(other) click to toggle source
# File lib/eancom/edifact/segment.rb, line 80
def tag?(other)
  tag == other
end
to_edi() click to toggle source
# File lib/eancom/edifact/segment.rb, line 52
def to_edi
  to_s
end
to_hash() click to toggle source
# File lib/eancom/edifact/segment.rb, line 64
def to_hash
  attributes
end
to_json_hash() click to toggle source
# File lib/eancom/edifact/segment.rb, line 56
def to_json_hash
  raise NotImplementedError
end
to_s() click to toggle source
# 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() click to toggle source
# 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

Private Instance Methods

attributes() click to toggle source
# File lib/eancom/edifact/segment.rb, line 114
def attributes
  hash = {}
  instance_variables.each do |var|
    key = var.to_s.delete('@').to_sym
    next if key == :structure
    next if key == :array
    value = instance_variable_get(var)
    hash[key] = value
  end
  hash
end
clean_end_string(string) click to toggle source
# File lib/eancom/edifact/segment.rb, line 136
def clean_end_string(string)
  while (string[-1] == data_delimiter)
    string.chop!
  end
  string
end
component_delimiter() click to toggle source
# File lib/eancom/edifact/segment.rb, line 106
def component_delimiter
  DELIMITERS[:component]
end
data_delimiter() click to toggle source
# File lib/eancom/edifact/segment.rb, line 102
def data_delimiter
  DELIMITERS[:data]
end
find_identifier(key) click to toggle source
# File lib/eancom/edifact/segment.rb, line 126
def find_identifier(key)
  dictionary = structure.find(key).dictionary
  var = instance_variable_get('@' + key.to_s)
  if dictionary[var]
    dictionary[var][:identifier]
  else
    var
  end
end
segment_delimiter() click to toggle source
# File lib/eancom/edifact/segment.rb, line 110
def segment_delimiter
  DELIMITERS[:segment]
end