Class: FlatFile::FieldDef

Inherits:
Object
  • Object
show all
Defined in:
lib/flat_file/field_def.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.

The option :default => ‘value’ may be used to specify a default value to be mapped into a model field provided the flat filer record is empty.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flat_file/field_def.rb', line 32

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.



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

def aggressive
  @aggressive
end

#defaultObject

Returns the value of attribute default.



19
20
21
# File 'lib/flat_file/field_def.rb', line 19

def default
  @default
end

#file_klassObject

Returns the value of attribute file_klass.



15
16
17
# File 'lib/flat_file/field_def.rb', line 15

def file_klass
  @file_klass
end

#filtersObject

Returns the value of attribute filters.



13
14
15
# File 'lib/flat_file/field_def.rb', line 13

def filters
  @filters
end

#formattersObject

Returns the value of attribute formatters.



14
15
16
# File 'lib/flat_file/field_def.rb', line 14

def formatters
  @formatters
end

#map_in_procObject

Returns the value of attribute map_in_proc.



17
18
19
# File 'lib/flat_file/field_def.rb', line 17

def map_in_proc
  @map_in_proc
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/flat_file/field_def.rb', line 11

def name
  @name
end

#paddingObject

Returns the value of attribute padding.



16
17
18
# File 'lib/flat_file/field_def.rb', line 16

def padding
  @padding
end

#widthObject

Returns the value of attribute width.



12
13
14
# File 'lib/flat_file/field_def.rb', line 12

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



57
58
59
60
# File 'lib/flat_file/field_def.rb', line 57

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.



64
65
66
67
# File 'lib/flat_file/field_def.rb', line 64

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)


106
107
108
# File 'lib/flat_file/field_def.rb', line 106

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)


112
113
114
# File 'lib/flat_file/field_def.rb', line 112

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)


51
52
53
# File 'lib/flat_file/field_def.rb', line 51

def is_padding?
  @padding
end

#pass_through(what, value) ⇒ Object

protected



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/flat_file/field_def.rb', line 83

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.



71
72
73
# File 'lib/flat_file/field_def.rb', line 71

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.



77
78
79
# File 'lib/flat_file/field_def.rb', line 77

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