Module: Hippo::Separator

Included in:
Parser, Hippo::Segments::Base, TransactionSets::Base
Defined in:
lib/hippo/separator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#composite_separatorObject

Returns the value of attribute composite_separator.



8
9
10
# File 'lib/hippo/separator.rb', line 8

def composite_separator
  @composite_separator
end

#field_separatorObject

Returns the value of attribute field_separator.



8
9
10
# File 'lib/hippo/separator.rb', line 8

def field_separator
  @field_separator
end

#repetition_separatorObject

Returns the value of attribute repetition_separator.



8
9
10
# File 'lib/hippo/separator.rb', line 8

def repetition_separator
  @repetition_separator
end

#segment_separatorObject

Returns the value of attribute segment_separator.



8
9
10
# File 'lib/hippo/separator.rb', line 8

def segment_separator
  @segment_separator
end

Instance Method Details

#empty_field_regexpObject



64
65
66
67
68
69
# File 'lib/hippo/separator.rb', line 64

def empty_field_regexp
  %r{([#{regexp_escaped_separators}])
      \s+
      ([#{regexp_escaped_separators}])
    }x
end

#initialize(options = {}) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/hippo/separator.rb', line 10

def initialize(options = {})
  [:field_separator, :repetition_separator, :composite_separator, :segment_separator].each do |sym|
    value = options[sym] || parent_or_default_separator(sym)

    self.send(:"#{sym}=", value)
  end
end

#parent_or_default_separator(separator_type) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/hippo/separator.rb', line 18

def parent_or_default_separator(separator_type)
  if defined?(parent) && parent
    parent.send(separator_type.to_sym)
  else
    Hippo.const_get(:"DEFAULT_#{separator_type.to_s.upcase}")
  end
end

#parse_separators(input) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/hippo/separator.rb', line 26

def parse_separators(input)
  if input =~ /\AISA/
    @field_separator      = input[3,1]
    @repetition_separator = input[82,1]
    @composite_separator  = input[104,1]
    @segment_separator    = input[105,1]
  end
end

#regexp_escaped_separatorsObject



58
59
60
61
62
# File 'lib/hippo/separator.rb', line 58

def regexp_escaped_separators
  Regexp.escape(@composite_separator) +
    Regexp.escape(@field_separator)   +
    Regexp.escape(@segment_separator)
end

#remove_empty_fields(input) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/hippo/separator.rb', line 71

def remove_empty_fields(input)
  while input =~ empty_field_regexp
    input = input.gsub(empty_field_regexp, '\1\2')
  end

  input
end

#repeating_composite_separator_regexpObject



51
52
53
54
55
56
# File 'lib/hippo/separator.rb', line 51

def repeating_composite_separator_regexp
  %r{
      #{Regexp.escape(@composite_separator)}+
      #{Regexp.escape(@field_separator)}
    }x
end

#repeating_field_separator_at_end_of_segment_regexpObject



44
45
46
47
48
49
# File 'lib/hippo/separator.rb', line 44

def repeating_field_separator_at_end_of_segment_regexp
  %r{
      #{Regexp.escape(@field_separator)}+
      #{Regexp.escape(@segment_separator)}
    }x
end

#separatorsObject



35
36
37
38
39
40
41
42
# File 'lib/hippo/separator.rb', line 35

def separators
  {
    :field_separator      => @field_separator,
    :composite_separator  => @composite_separator,
    :segment_separator    => @segment_separator,
    :repetition_separator => @repetition_separator
  }
end