class FixedWidthDSL::FormatString

Constants

Field

Attributes

format_string[R]

Public Class Methods

new() click to toggle source
# File lib/fixed_width_dsl.rb, line 7
def initialize
  @fields = []
  @format_string = ''
end

Public Instance Methods

apply(attributes) click to toggle source
# File lib/fixed_width_dsl.rb, line 30
def apply attributes
  values = @fields.map do |field|
    attributes.fetch(field.name)
  end
  format(format_string, *values)
end
debug() click to toggle source
# File lib/fixed_width_dsl.rb, line 12
def debug
  output = "field                | wi | fro |  to | type    | flags\n"
  lines = [] 
  current_column = 1
  @fields.each do |field|
    end_column = current_column + field.width - 1
    output.concat format "%-20s | %2i | %3i | %3i | %-7s | %3s\n",
      field.name,
      field.width,
      current_column,
      end_column,
      field.type,
      field.flags
    current_column += field.width
  end
  output
end

Private Instance Methods

field(name, width, type, flags = '') click to toggle source
# File lib/fixed_width_dsl.rb, line 39
def field name, width, type, flags = ''
  type_code = case type
              when :string then 's'
              when :integer then 'i'
              else raise ArgumentError, "Invalid type #{ type }"
              end
  @format_string += "%#{ flags }#{ width }#{ type_code }"
  @fields << Field.new(name, width, type, flags)
end