Module: Field::ClassMethods

Defined in:
lib/flat/field.rb

Overview

Class Methods

Defines behavior for subclasses of Flat::File regarding the specification of the line structure contained in a flat file.

Instance Method Summary collapse

Instance Method Details

#add_field(name = nil, options = {}) {|field_def| ... } ⇒ Object

Add a field to the Flat::File subclass. Options can include

:width - number of characters in field (default 10) :filter - callack, lambda or code block for processing during reading :formatter - callback, lambda, or code block for processing during writing

class SomeFile < Flat::File
  add_field :some_field_name, :width => 35
end

Options

Yields:

  • (field_def)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flat/field.rb', line 34

def add_field name = nil, options = {}, &block
  fields << field_def = Field::Definition.new(name, options, self)

  yield field_def if block_given?

  pack_format << field_def.pack_format
  self.width += field_def.width

  # TODO: Add a check here to ensure the Field has a name specified; it can be a String or Symbol
  return field_def
end

#pad(name, options = {}) ⇒ Object

Add a pad field. To have the name auto generated, use :autoname for the name parameter. For options see add_field.



50
51
52
# File 'lib/flat/field.rb', line 50

def pad name, options = {}
  add_field ( name == :autoname ? new_pad_name : name ), options.merge( padding: true )
end