Class: Hippo::Segments::Base

Inherits:
Object
  • Object
show all
Includes:
Outputters::HTML::Segment, Outputters::PDF::Segment, Outputters::PrettyString::Segment, Parser::Segment, Hippo::Separator
Defined in:
lib/hippo/segments/base.rb

Direct Known Subclasses

AAA, AK1, AK2, AK3, AK4, AK5, AK9, AMT, BHT, BPR, CAS, CL1, CLM, CLP, CN1, CR1, CR2, CR3, CR4, CR5, CR6, CR7, CR8, CRC, CTP, CTX, CUR, DMG, DN1, DN2, DSB, DTM, DTP, EB, EM, EQ, FRM, GE, GS, HCP, HI, HL, HSD, IEA, III, IK3, IK4, IK5, IMM, INS, ISA, K3, LE, LIN, LQ, LS, LX, MEA, MIA, MOA, MPI, MSG, N1, N2, N3, N4, NM1, NTE, OI, PAT, PCT, PDP, PDR, PER, PID, PKD, PLB, PRV, PS1, PWK, QTY, RDM, REF, SBR, SD1, SE, ST, STC, SV1, SV2, SV3, SV4, SV5, SV6, SV7, SVC, SVD, TA1, TOO, TRN, TS2, TS3, UR, VEH

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Hippo::Separator

#composite_separator, #field_separator, #repetition_separator, #segment_separator

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Outputters::PrettyString::Segment

#to_pretty_string

Methods included from Outputters::PDF::Segment

#to_pdf

Methods included from Outputters::HTML::Segment

#to_html

Methods included from Parser::Segment

#parse

Methods included from Hippo::Separator

#empty_field_regexp, #parent_or_default_separator, #parse_separators, #regexp_escaped_separators, #remove_empty_fields, #repeating_composite_separator_regexp, #repeating_field_separator_at_end_of_segment_regexp, #separators

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



92
93
94
95
96
# File 'lib/hippo/segments/base.rb', line 92

def initialize(options = {})
  @parent = options.delete(:parent)

  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/hippo/segments/base.rb', line 164

def method_missing(method_name, *args)
  values

  field = get_field(get_field_name(method_name.to_s))

  if field.nil? || field.class == Array
     raise Hippo::Exceptions::InvalidField.new("Invalid field '#{method_name.to_s}' specified.")
  end

  if method_name.to_s =~ /=\z/
    if field.composite
      self.values[field.composite_sequence] ||= {}
      self.values[field.composite_sequence][field.sequence] = field.formatted_value(args[0])
    else
      self.values[field.sequence] = field.formatted_value(args[0])
    end
  else
    if field.composite
      self.values[field.composite_sequence] ||= {}
      self.values[field.composite_sequence][field.sequence]
    else
      self.values[field.sequence]
    end
  end
end

Class Attribute Details

.fieldsObject

Returns the value of attribute fields.



10
11
12
# File 'lib/hippo/segments/base.rb', line 10

def fields
  @fields
end

.fixed_widthObject

Returns the value of attribute fixed_width.



10
11
12
# File 'lib/hippo/segments/base.rb', line 10

def fixed_width
  @fixed_width
end

.identifierObject

Returns the value of attribute identifier.



10
11
12
# File 'lib/hippo/segments/base.rb', line 10

def identifier
  @identifier
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



69
70
71
# File 'lib/hippo/segments/base.rb', line 69

def parent
  @parent
end

#valuesObject

Returns the value of attribute values.



69
70
71
# File 'lib/hippo/segments/base.rb', line 69

def values
  @values
end

Class Method Details

.composite_field(field_name) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/hippo/segments/base.rb', line 50

def composite_field(field_name)
  @composite_block = true
  @default_separator = :composite_separator
  fields << []
  yield
  @default_separator = :field_separator
  @composite_block = false
end

.field(field) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hippo/segments/base.rb', line 20

def field(field)
  f              = Hippo::Field.new
  f.sequence     = fields.length + 1
  f.name         = field[:name]
  f.datatype     = field[:datatype]
  f.minimum      = field[:minimum]
  f.maximum      = field[:maximum]
  f.options      = field[:options]
  f.restrictions = field[:restrictions]
  f.required     = field[:required]  || false
  f.separator    = field[:separator] || @default_separator || :field_separator

  if @composite_block
    f.composite = true
    f.composite_sequence = fields.length
    f.sequence = fields.last.length + 1
    fields.last << f
  else
    fields << f
  end
end

.segment?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/hippo/segments/base.rb', line 42

def segment?
  true
end

.segment_fixed_widthObject



64
65
66
# File 'lib/hippo/segments/base.rb', line 64

def segment_fixed_width
  @fixed_width = true
end

.segment_identifier(id) ⇒ Object



59
60
61
62
# File 'lib/hippo/segments/base.rb', line 59

def segment_identifier(id)
  @identifier = id
  @fields     = []
end

.transaction_set?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/hippo/segments/base.rb', line 46

def transaction_set?
  false
end

Instance Method Details

#ancestorsObject



79
80
81
82
83
84
85
# File 'lib/hippo/segments/base.rb', line 79

def ancestors
  if parent
    parent.ancestors.flatten
  else
    []
  end
end

#get_field(field) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/hippo/segments/base.rb', line 106

def get_field(field)
  if field =~ /\A#{self.class.identifier}(?:(\d+)(?:_(\d+)){0,1})\z/
    if $2 && self.class.fields[$1.to_i - 1].class == Array
      self.class.fields[$1.to_i - 1][$2.to_i - 1]
    else
      self.class.fields[$1.to_i - 1]
    end
  else
    fields = self.class.fields.flatten.select{|f| f.name == field.gsub(/_\d{1,2}\z/,'')}

    if field =~ /_(\d{1,2})\z/
      fields[$1.to_i - 1]
    else
      fields.first
    end
  end
end

#get_field_name(text) ⇒ Object



102
103
104
# File 'lib/hippo/segments/base.rb', line 102

def get_field_name(text)
  text.to_s.gsub(' ','').gsub('=','')
end

#identifierObject



160
161
162
# File 'lib/hippo/segments/base.rb', line 160

def identifier
  self.class.identifier
end

#segment_countObject



75
76
77
# File 'lib/hippo/segments/base.rb', line 75

def segment_count
  segments.count
end

#segmentsObject



71
72
73
# File 'lib/hippo/segments/base.rb', line 71

def segments
  [self]
end

#to_aryObject Also known as: to_a



87
88
89
# File 'lib/hippo/segments/base.rb', line 87

def to_ary
  nil
end

#to_sObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hippo/segments/base.rb', line 124

def to_s
  output = self.class.identifier + @field_separator

  self.class.fields.each_with_index do |field, index|
    if field.class == Array # this is a composite field
      field.each do |comp_field|
        field_value = if values[comp_field.composite_sequence]
                        # some values exist for this composite field
                        comp_field.string_value(values[comp_field.composite_sequence][comp_field.sequence])
                      else
                        # no values exist for the entire composite field
                        ''
                      end

        output += field_value + @composite_separator
      end

      output += @field_separator
    else # standard field
      field_value = field.string_value(values[field.sequence])

      output += field_value + @field_separator
    end
  end

  unless self.class.fixed_width
    output = remove_empty_fields(output)
    output = output.gsub(repeating_composite_separator_regexp, @field_separator)
  end

  output += @segment_separator
  output = output.gsub(repeating_field_separator_at_end_of_segment_regexp, @segment_separator)

  output =~ /\A#{self.class.identifier}~/ ? '' : output
end