Class: FlatFile::FieldDef
- Inherits:
-
Object
- Object
- FlatFile::FieldDef
- 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
-
#aggressive ⇒ Object
Returns the value of attribute aggressive.
-
#default ⇒ Object
Returns the value of attribute default.
-
#file_klass ⇒ Object
Returns the value of attribute file_klass.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#formatters ⇒ Object
Returns the value of attribute formatters.
-
#map_in_proc ⇒ Object
Returns the value of attribute map_in_proc.
-
#name ⇒ Object
Returns the value of attribute name.
-
#padding ⇒ Object
Returns the value of attribute padding.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#add_filter(filter = nil, &block) ⇒ Object
Add a filter.
-
#add_formatter(formatter = nil, &block) ⇒ Object
Add a formatter.
-
#filter_block?(filter) ⇒ Boolean
Test to see if filter is a filter block.
-
#filter_class?(filter) ⇒ Boolean
Test to see if a class is a filter class.
-
#initialize(name = null, options = {}, klass = {}) ⇒ FieldDef
constructor
Create a new FeildDef, having name and width.
-
#is_padding? ⇒ Boolean
Will return true if the field is a padding field.
-
#pass_through(what, value) ⇒ Object
protected.
-
#pass_through_filters(v) ⇒ Object
Filters a value based on teh filters associated with a FieldDef.
-
#pass_through_formatters(v) ⇒ Object
Filters a value based on the filters associated with a FieldDef.
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,={},klass={}) @name = name @width = 10 @filters = Array.new @formatters = Array.new @file_klass = klass @padding = .delete(:padding) @default = .has_key?(:default) ? .delete(:default) : "" add_filter([:filter]) if .has_key?(:filter) add_formatter([:formatter]) if .has_key?(:formatter) @map_in_proc = [:map_in_proc] @width = [:width] if .has_key?(:width) @aggressive = [:aggressive] || false end |
Instance Attribute Details
#aggressive ⇒ Object
Returns the value of attribute aggressive.
18 19 20 |
# File 'lib/flat_file/field_def.rb', line 18 def aggressive @aggressive end |
#default ⇒ Object
Returns the value of attribute default.
19 20 21 |
# File 'lib/flat_file/field_def.rb', line 19 def default @default end |
#file_klass ⇒ Object
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 |
#filters ⇒ Object
Returns the value of attribute filters.
13 14 15 |
# File 'lib/flat_file/field_def.rb', line 13 def filters @filters end |
#formatters ⇒ Object
Returns the value of attribute formatters.
14 15 16 |
# File 'lib/flat_file/field_def.rb', line 14 def formatters @formatters end |
#map_in_proc ⇒ Object
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 |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/flat_file/field_def.rb', line 11 def name @name end |
#padding ⇒ Object
Returns the value of attribute padding.
16 17 18 |
# File 'lib/flat_file/field_def.rb', line 16 def padding @padding end |
#width ⇒ Object
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
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).
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.
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 |