class EDI::E::UNA
Class UNA
is a model of UN/EDIFACT’s UNA
pseudo-segment. It provides getters and setters that let you manipulate the six special characters of UN/EDIFACT. Note that the chars are passed as integers, i.e. ASCII codes.
Attributes
Public Class Methods
Generates the UNA
object
-
Requires that “version” and “charset” of parent/root (
Interchange
) be already defined. -
Sets the UN/EDIFACT defaults if source string ‘UNA.…..’ not given
EDI::Object::new
# File lib/edi4r/edifact.rb, line 258 def initialize( root, source=nil ) super( root, root, 'UNA') raise "UNA.new requires 'version' in the interchange" unless root.version raise "UNA.new requires 'charset' in the interchange" unless root.charset if source =~ /^UNA(......)$/ # Take what's given @chars = $1 elsif (source == nil or source.empty?) # Use EDIFACT default rules if root.version==2 and root.charset=='UNOB' @chars = "\x11\x12.? \x14" elsif root.version==4 @chars = ":+.?*'" else @chars = ":+.? '" end else raise "This is not a valid UNA source string: #{source}" end @ce_sep, @de_sep, @decimal_sign, @esc_char, @rep_sep, @seg_term = @chars.split('').map{|c| c[0]} set_patterns end
Public Instance Methods
Sets the composite data element separator. Default is ?:
# File lib/edi4r/edifact.rb, line 208 def ce_sep= (chr) chr = chr[0] if chr.is_a? String @ce_sep = chr set_chars # Update derived Regexp objects! end
Sets the data element separator. Default is ?+
# File lib/edi4r/edifact.rb, line 217 def de_sep= (chr) chr = chr[0] if chr.is_a? String @de_sep = chr set_chars # Update derived Regexp objects! end
Sets the decimal sign. UN/EDIFACT allows only ?, and ?.
# File lib/edi4r/edifact.rb, line 198 def decimal_sign= (chr) chr = chr[0] if chr.is_a? String raise "Illegal decimal sign: #{chr}" unless chr==?. || chr==?, @decimal_sign = chr set_chars end
Sets the escape character. Default is ??
# File lib/edi4r/edifact.rb, line 246 def esc_char= (chr) chr = chr[0] if chr.is_a? String @esc_char = chr set_chars # Update derived Regexp objects! end
Sets the repetition separator. Default is ?* . Only applicable to Syntax Version 4 !
# File lib/edi4r/edifact.rb, line 227 def rep_sep= (chr) raise NoMethodError, "Syntax version 4 required" unless root.version==4 chr = chr[0] if chr.is_a? String @rep_sep = chr set_chars # Update derived Regexp objects! end
Sets the segment terminator. Default is ?‘
# File lib/edi4r/edifact.rb, line 237 def seg_term= (chr) chr = chr[0] if chr.is_a? String @seg_term = chr set_chars # Update derived Regexp objects! end
# File lib/edi4r/edifact.rb, line 285 def to_s 'UNA'+@chars end
Private Instance Methods
# File lib/edi4r/edifact.rb, line 291 def set_chars @chars=[@ce_sep, @de_sep, @decimal_sign, @esc_char, @rep_sep, @seg_term ] @chars=@chars.map{|c| c.chr}.join('') # Prevent duplicates raise "Must not assign special char more than once!" if @chars=~/(.).*\1/ set_patterns end
Adjust match patterns anew when one of the UNA
separators / special characters is changed.
# File lib/edi4r/edifact.rb, line 303 def set_patterns special_chars = [ @ce_sep, @de_sep, @esc_char, @seg_term ] special_chars.push @rep_sep if root.version == 4 special_chars = special_chars.map{|c| c.chr} @pattern_esc = Regexp.new( [ '([', special_chars, '])' ].flatten.join) esc_str = @esc_char.chr esc_str << '\\' if esc_str == '\\' # Must escape '\' in a regex @pattern_unesc = Regexp.new( [ '([^', esc_str, ']?)', '[', esc_str, ']([', special_chars,'])' ].flatten.join ) root.show_una = true end