Class: Field::Definition
Overview
Instance Attribute Summary collapse
-
#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.
-
#parent ⇒ Object
readonly
:nodoc:.
-
#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.
- #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.
-
#pack_format ⇒ Object
TODO: Find out what’s capable with this pack foramat; String#pack, String#unpack.
- #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
#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
Returns the value of attribute name.
89 90 91 |
# File 'lib/flat/field.rb', line 89 def name @name end |
#parent ⇒ Object (readonly)
:nodoc:
88 89 90 |
# File 'lib/flat/field.rb', line 88 def parent @parent end |
#width ⇒ Object
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
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_format ⇒ Object
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
116 117 118 |
# File 'lib/flat/field.rb', line 116 def padding? @padding end |