Class: FixedWidth::Column
- Inherits:
-
Object
- Object
- FixedWidth::Column
- Defined in:
- lib/fixed_width/column.rb
Constant Summary collapse
- DEFAULT_PADDING =
' '
- DEFAULT_ALIGNMENT =
:right
- DEFAULT_TRUNCATE =
false
- DEFAULT_FORMATTER =
:to_s
Instance Attribute Summary collapse
-
#alignment ⇒ Object
readonly
Returns the value of attribute alignment.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#truncate ⇒ Object
readonly
Returns the value of attribute truncate.
Instance Method Summary collapse
- #format(value) ⇒ Object
-
#initialize(name, length, options = {}) ⇒ Column
constructor
A new instance of Column.
- #parse(value) ⇒ Object
Constructor Details
#initialize(name, length, options = {}) ⇒ Column
Returns a new instance of Column.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/fixed_width/column.rb', line 10 def initialize(name, length, ={}) () @name = name @length = length @alignment = [:align] || DEFAULT_ALIGNMENT @padding = [:padding] || DEFAULT_PADDING @truncate = [:truncate] || DEFAULT_TRUNCATE @group = [:group] @parser = [:parser] @parser = @parser.to_proc if @parser.is_a?(Symbol) @formatter = [:formatter] @formatter ||= DEFAULT_FORMATTER @formatter = @formatter.to_proc if @formatter.is_a?(Symbol) @nil_blank = [:nil_blank] end |
Instance Attribute Details
#alignment ⇒ Object (readonly)
Returns the value of attribute alignment.
8 9 10 |
# File 'lib/fixed_width/column.rb', line 8 def alignment @alignment end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
8 9 10 |
# File 'lib/fixed_width/column.rb', line 8 def group @group end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
8 9 10 |
# File 'lib/fixed_width/column.rb', line 8 def length @length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/fixed_width/column.rb', line 8 def name @name end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
8 9 10 |
# File 'lib/fixed_width/column.rb', line 8 def padding @padding end |
#truncate ⇒ Object (readonly)
Returns the value of attribute truncate.
8 9 10 |
# File 'lib/fixed_width/column.rb', line 8 def truncate @truncate end |
Instance Method Details
#format(value) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/fixed_width/column.rb', line 47 def format(value) pad( validate_size( @formatter.call(value) ) ) end |
#parse(value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fixed_width/column.rb', line 30 def parse(value) if @nil_blank && blank?(value) return nil elsif @parser @parser.call(value) else case @alignment when :right value.lstrip when :left value.rstrip end end rescue raise ParserError.new("The value '#{value}' could not be parsed: #{$!}") end |