Class: Field::Definition

Inherits:
Object show all
Defined in:
lib/flat/field.rb

Overview

Definition

Used in Flat::File subclasses to define how a Record is defined.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = null, options = {}, parent = {}) ⇒ Definition

Create a new FieldDef, having name and width. parent is a reference to the Flat::File 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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/flat/field.rb', line 101

def initialize name = null, options = {}, parent = {}
  @parent, @name = parent, name

  @width      = options.fetch(:width, 10)
  @padding    = options.fetch(:padding, false)
  @aggressive = options.fetch(:aggressive, false)

  @filters = @formatters = Array.new

  add_filter options[:filter]
  add_formatter options[:formatter]

  @map_in_proc = options[:map_in_proc]
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



90
91
92
# File 'lib/flat/field.rb', line 90

def filters
  @filters
end

#formattersObject

Returns the value of attribute formatters.



90
91
92
# File 'lib/flat/field.rb', line 90

def formatters
  @formatters
end

#map_in_procObject

Returns the value of attribute map_in_proc.



90
91
92
# File 'lib/flat/field.rb', line 90

def map_in_proc
  @map_in_proc
end

#nameObject

Returns the value of attribute name.



89
90
91
# File 'lib/flat/field.rb', line 89

def name
  @name
end

#parentObject (readonly)

:nodoc:



88
89
90
# File 'lib/flat/field.rb', line 88

def parent
  @parent
end

#widthObject

Returns the value of attribute width.



89
90
91
# File 'lib/flat/field.rb', line 89

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



137
138
139
140
# File 'lib/flat/field.rb', line 137

def add_filter filter = nil, &block
  @filters.push( filter ) unless filter.blank?
  @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.



146
147
148
149
# File 'lib/flat/field.rb', line 146

def add_formatter formatter = nil, &block
  @formatters.push( formatter ) unless formatter.blank?
  @formatters.push( block ) if block_given?
end

#aggressive?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/flat/field.rb', line 120

def aggressive?
  @aggressive
end

#filter(value) ⇒ Object

Passes value through the filters defined on this Field::Definition



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/flat/field.rb', line 154

def filter value
  @filters.each do |filter|
    value = case filter
        when Symbol
          @parent.public_send filter, value
        when Proc
          if filter.arity == 0
            value
          else
            filter.call value
          end
        when Class, Object
          unless filter.respond_to? 'filter'
            value
          else
            filter.filter value
          end
        else
          value
        end
  end
  value
end

#pack_formatObject

TODO: Find out what’s capable with this pack foramat; String#pack, String#unpack



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

def pack_format
  "a#{width}"
end

#padding?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/flat/field.rb', line 116

def padding?
  @padding
end