Class: Hippo::Segments::Base

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

Direct Known Subclasses

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, FRM, GE, GS, HCP, HI, HL, HSD, IEA, IK3, IK4, IK5, IMM, ISA, K3, LIN, LQ, LX, MEA, MIA, MOA, N1, N2, N3, N4, NM1, NTE, OI, PAT, PER, PLB, PRV, PS1, PWK, QTY, RDM, REF, SBR, SE, ST, STC, SV1, SV2, SV3, SV4, SV5, SV6, SV7, SVC, SVD, TA1, TOO, TRN, TS2, TS3, UR

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::PDF::Segment

#to_pdf

Methods included from Outputters::HTML::Segment

#to_html

Methods included from Parser::Segment

#parse

Methods included from Hippo::Separator

#parent_or_default_separator, #parse_separators, #separators

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



83
84
85
86
87
# File 'lib/hippo/segments/base.rb', line 83

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/hippo/segments/base.rb', line 153

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.



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

def fields
  @fields
end

.fixed_widthObject

Returns the value of attribute fixed_width.



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

def fixed_width
  @fixed_width
end

.identifierObject

Returns the value of attribute identifier.



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

def identifier
  @identifier
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

Class Method Details

.composite_field(field_name) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/hippo/segments/base.rb', line 41

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



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

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_fixed_widthObject



55
56
57
# File 'lib/hippo/segments/base.rb', line 55

def segment_fixed_width
  @fixed_width = true
end

.segment_identifier(id) ⇒ Object



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

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

Instance Method Details

#ancestorsObject



70
71
72
73
74
75
76
# File 'lib/hippo/segments/base.rb', line 70

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

#get_field(field) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hippo/segments/base.rb', line 97

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



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

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

#identifierObject



149
150
151
# File 'lib/hippo/segments/base.rb', line 149

def identifier
  self.class.identifier
end

#segment_countObject



66
67
68
# File 'lib/hippo/segments/base.rb', line 66

def segment_count
  segments.count
end

#segmentsObject



62
63
64
# File 'lib/hippo/segments/base.rb', line 62

def segments
  [self]
end

#to_aryObject Also known as: to_a



78
79
80
# File 'lib/hippo/segments/base.rb', line 78

def to_ary
  nil
end

#to_sObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/hippo/segments/base.rb', line 115

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

  # remove extra field separators that aren't needed
  unless self.class.identifier == 'ISA'
    output = output.gsub(/:{2,}\*/,'*').gsub(/:\*/,'*')
  end

  output += @segment_separator
  output = output.gsub(/\*+~/,'~')
end