Class: FlatFile::FieldDef

Inherits:
Object show all
Defined in:
lib/flat_file.rb

Overview

A field definition tracks infomration that’s necessary for FlatFile to process a particular field. This is typically added to a subclass of FlatFile like so:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = null, options = {}, klass = {}) ⇒ FieldDef

Create a new FeildDef, having name and width. klass is a reference to the FlatFile subclass that contains this field definition. This reference is needed when calling filters if they are specified using a symbol.

Options can be :padding (if present and a true value, field is marked as a pad field), :width, specify the field width, :formatter, specify a formatter, :filter, specify a filter.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/flat_file.rb', line 139

def initialize(name=null,options={},klass={})
    @name = name
    @width = 10
    @filters = Array.new
    @formatters = Array.new
    @file_klass = klass
    @padding = options.delete(:padding)
    @default = options.has_key?(:default) ? options.delete(:default) :  ""
    
    add_filter(options[:filter]) if options.has_key?(:filter)
    add_formatter(options[:formatter]) if options.has_key?(:formatter)
    @map_in_proc = options[:map_in_proc]
    @width = options[:width] if options.has_key?(:width)
    @aggressive = options[:aggressive] || false
end

Instance Attribute Details

#aggressiveObject

Returns the value of attribute aggressive.



129
130
131
# File 'lib/flat_file.rb', line 129

def aggressive
  @aggressive
end

#defaultObject

Returns the value of attribute default.



130
131
132
# File 'lib/flat_file.rb', line 130

def default
  @default
end

#file_klassObject

Returns the value of attribute file_klass.



126
127
128
# File 'lib/flat_file.rb', line 126

def file_klass
  @file_klass
end

#filtersObject

Returns the value of attribute filters.



124
125
126
# File 'lib/flat_file.rb', line 124

def filters
  @filters
end

#formattersObject

Returns the value of attribute formatters.



125
126
127
# File 'lib/flat_file.rb', line 125

def formatters
  @formatters
end

#map_in_procObject

Returns the value of attribute map_in_proc.



128
129
130
# File 'lib/flat_file.rb', line 128

def map_in_proc
  @map_in_proc
end

#nameObject

Returns the value of attribute name.



122
123
124
# File 'lib/flat_file.rb', line 122

def name
  @name
end

#paddingObject

Returns the value of attribute padding.



127
128
129
# File 'lib/flat_file.rb', line 127

def padding
  @padding
end

#widthObject

Returns the value of attribute width.



123
124
125
# File 'lib/flat_file.rb', line 123

def width
  @width
end

Instance Method Details

#add_filter(filter = nil, &block) ⇒ Object

Add a filter. Filters are used for processing field data when a flat file is being processed. For fomratting the data when writing a flat file, see add_formatter



164
165
166
167
# File 'lib/flat_file.rb', line 164

def add_filter(filter=nil,&block) #:nodoc:
    @filters.push(filter) unless filter.nil?
    @filters.push(block) if block_given?
end

#add_formatter(formatter = nil, &block) ⇒ Object

Add a formatter. Formatters are used for formatting a field for rendering a record, or writing it to a file in the desired format.



171
172
173
174
# File 'lib/flat_file.rb', line 171

def add_formatter(formatter=nil,&block) #:nodoc:
    @formatters.push(formatter) if formatter
    @formatters.push(block) if block_given?
end

#filter_block?(filter) ⇒ Boolean

Test to see if filter is a filter block. A filter block can be called (using call) and takes one parameter

Returns:

  • (Boolean)


213
214
215
# File 'lib/flat_file.rb', line 213

def filter_block?(filter) #:nodoc:
    filter.respond_to?('call') && ( filter.arity >= 1 || filter.arity <= -1 )
end

#filter_class?(filter) ⇒ Boolean

Test to see if a class is a filter class. A filter class responds to the filter signal (you can call filter on it).

Returns:

  • (Boolean)


219
220
221
# File 'lib/flat_file.rb', line 219

def filter_class?(filter) #:nodoc:
    filter.respond_to?('filter')
end

#is_padding?Boolean

Will return true if the field is a padding field. Padding fields are ignored when doing various things. For example, when you’re populating an ActiveRecord model with a record, padding fields are ignored.

Returns:

  • (Boolean)


158
159
160
# File 'lib/flat_file.rb', line 158

def is_padding?
    @padding
end

#pass_through(what, value) ⇒ Object

protected



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/flat_file.rb', line 190

def pass_through(what,value) #:nodoc:
    #puts "PASS THROUGH #{what.inspect} => #{value}"
    what.each do |filter|
       value  = case
            when filter.is_a?(Symbol)
                #puts "filter is a symbol"
                @file_klass.send(filter,value)
            when filter_block?(filter)
                #puts "filter is a block"
                filter.call(value)
            when filter_class?(filter)
                #puts "filter is a class"
                filter.filter(value)
            else
                #puts "filter not valid, preserving"
                value
            end
    end
    value
end

#pass_through_filters(v) ⇒ Object

Filters a value based on teh filters associated with a FieldDef.



178
179
180
# File 'lib/flat_file.rb', line 178

def pass_through_filters(v) #:nodoc:
    pass_through(@filters,v)
end

#pass_through_formatters(v) ⇒ Object

Filters a value based on the filters associated with a FieldDef.



184
185
186
# File 'lib/flat_file.rb', line 184

def pass_through_formatters(v) #:nodoc:
    pass_through(@formatters,v)
end