Class: FlatOut

Inherits:
Object
  • Object
show all
Defined in:
lib/flat_out.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rec_length, options = {}) ⇒ FlatOut

attr_accessor :flat_out, :flat_length, :base, :format, :template



5
6
7
# File 'lib/flat_out.rb', line 5

def initialize(rec_length, options={})
  self.reset(rec_length, options)
end

Class Method Details

.digits_only(fld) ⇒ Object



26
27
28
# File 'lib/flat_out.rb', line 26

def self.digits_only(fld)
  fld.gsub(/[^\d]/,"")
end

.phone_squeeze(fld) ⇒ Object



22
23
24
# File 'lib/flat_out.rb', line 22

def self.phone_squeeze(fld)
  fld.tr("-)( " , "")
end

Instance Method Details

#put(p1, p2 = 1, p3 = '') ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/flat_out.rb', line 30

def put p1, p2=1, p3=''
  if @template.size > 0 && p1.class == Array
    fld = p1
  else
    case @format
    when :len_pos_fld then (len = p1; pos = p2; fld = p3)
    when :len_fld_pos then (len = p1; fld = p2; pos = p3)
    when :pos_len_fld then (pos = p1; len = p2; fld = p3)
    when :pos_fld_len then (pos = p1; fld = p2; len = p3)
    when :fld_pos_len then (fld = p1; pos = p2; len = p3)
    when :fld_len_pos then (fld = p1; len = p2; pos = p3)
    when :fld_len     then (fld = p1)
    when :len_fld     then (fld = p1)
    else                   (len = p1; pos = p2; fld = p3)
    end
  end

  case fld
  when Array
    if @template.size > 0 and @template[0].class == Array    # :template => [[5,1],[5,6],[4.2,11]]
      fld.each_with_index do |field,idx|
        len = @template[idx][0]
        pos = @template[idx][1]
        put len, pos, field
      end
    elsif @template.size > 0                                 # :template => [5,5,4.2,3]
      pos = @base
      fld.each_with_index do |field, idx|
        len = @template[idx]
        put len, pos, field
        pos = pos + real_len(len)
      end
    elsif @format == :fld_len                                # val = ['ABCD', 5, 10, 5]
      @format = :len_pos_fld
      pos = @base
      fld.each_slice(2) do |arr|
        field = arr[0]
        len = arr[1]
        put len, pos, field
        pos = pos + real_len(len)
      end
    elsif @format == :len_fld                                # val = [5, 'ABCD', 4.2, 10.23]
      @format = :len_pos_fld
      pos = @base
      fld.each_slice(2) do |arr|
        len = arr[0]
        field = arr[1]
        put len, pos, field
        pos = pos + real_len(len)
      end
    end
  when String
    put_alpha len, pos, fld
  when Integer
    put_integer len, pos, fld
  when Float
    if len.to_s.match(/\./)
      put_float len, pos, fld
    else
      put_integer len, pos, fld
    end
  end
  return self.to_s
end

#reset(rec_length = @flat_length, options = {}) ⇒ Object



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

def reset(rec_length=@flat_length, options={})
  @base = options[:base] ||= @base ||= 1
  @format = options[:format] ||= @format ||= :len_pos_fld
  @template = options[:template] ||= @template ||= []
  @flat_length = rec_length
  @flat_out = " " * rec_length
  return @flat_out
end

#to_sObject



18
19
20
# File 'lib/flat_out.rb', line 18

def to_s
  (@flat_out + (' ' * @flat_length))[0...@flat_length]
end