Class: Field::Definition
- Inherits:
-
Object
- Object
- Field::Definition
- 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
-
#aggressive ⇒ Object
:nodoc:.
-
#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
:nodoc:.
-
#padding ⇒ Object
:nodoc:.
-
#parent ⇒ Object
:nodoc:.
-
#width ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#add_filter(filter = nil, &block) ⇒ Object
Add a filter.
-
#add_formatter(formatter = nil, &block) ⇒ Object
Add a formatter.
- #aggressive? ⇒ Boolean
-
#filter(value) ⇒ Object
Passes value through the filters defined on this Field::Definition.
-
#initialize(name = null, options = {}, parent = {}) ⇒ Definition
constructor
Create a new FieldDef, having name and width.
- #padding? ⇒ Boolean
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, = {}, parent = {} @parent, @name = parent, name @width = .fetch(:width, 10) @padding = .fetch(:padding, false) @aggressive = .fetch(:aggressive, false) @filters = @formatters = Array.new add_filter [:filter] add_formatter [:formatter] @map_in_proc = [:map_in_proc] end |
Instance Attribute Details
#aggressive ⇒ Object
:nodoc:
89 90 91 |
# File 'lib/flat/field.rb', line 89 def aggressive @aggressive end |
#filters ⇒ Object
Returns the value of attribute filters.
90 91 92 |
# File 'lib/flat/field.rb', line 90 def filters @filters end |
#formatters ⇒ Object
Returns the value of attribute formatters.
90 91 92 |
# File 'lib/flat/field.rb', line 90 def formatters @formatters end |
#map_in_proc ⇒ Object
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 |
#name ⇒ Object
:nodoc:
89 90 91 |
# File 'lib/flat/field.rb', line 89 def name @name end |
#padding ⇒ Object
:nodoc:
89 90 91 |
# File 'lib/flat/field.rb', line 89 def padding @padding end |
#parent ⇒ Object
:nodoc:
89 90 91 |
# File 'lib/flat/field.rb', line 89 def parent @parent end |
#width ⇒ Object
:nodoc:
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
129 130 131 132 |
# File 'lib/flat/field.rb', line 129 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.
138 139 140 141 |
# File 'lib/flat/field.rb', line 138 def add_formatter formatter = nil, &block @formatters.push( formatter ) unless formatter.blank? @formatters.push( block ) if block_given? end |
#aggressive? ⇒ 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/flat/field.rb', line 146 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 |
#padding? ⇒ Boolean
116 117 118 |
# File 'lib/flat/field.rb', line 116 def padding? @padding end |