Class: Slither::Column
- Inherits:
-
Object
- Object
- Slither::Column
- Defined in:
- lib/slither/column.rb
Instance Attribute Summary collapse
-
#alignment ⇒ Object
readonly
Returns the value of attribute alignment.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #format(value) ⇒ Object
-
#initialize(name, length, options = {}) ⇒ Column
constructor
A new instance of Column.
- #parse(value) ⇒ Object
- #unpacker ⇒ Object
Constructor Details
#initialize(name, length, options = {}) ⇒ Column
Returns a new instance of Column.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/slither/column.rb', line 9 def initialize(name, length, = {}) () @name = name @length = length @options = @alignment = [:align] || :right @type = [:type] || :string @padding = [:padding] || :space @truncate = [:truncate] || false # Only used with floats, this determines the decimal places @precision = [:precision] end |
Instance Attribute Details
#alignment ⇒ Object (readonly)
Returns the value of attribute alignment.
7 8 9 |
# File 'lib/slither/column.rb', line 7 def alignment @alignment end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
7 8 9 |
# File 'lib/slither/column.rb', line 7 def length @length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/slither/column.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/slither/column.rb', line 7 def @options end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
7 8 9 |
# File 'lib/slither/column.rb', line 7 def padding @padding end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
7 8 9 |
# File 'lib/slither/column.rb', line 7 def precision @precision end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/slither/column.rb', line 7 def type @type end |
Instance Method Details
#format(value) ⇒ Object
46 47 48 49 50 |
# File 'lib/slither/column.rb', line 46 def format(value) pad(formatter % to_s(value)) rescue puts "Could not format column '#{@name}' as a '#{@type}' with formatter '#{formatter}' and value of '#{value}' (formatted: '#{to_s(value)}'). #{$!}" end |
#parse(value) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/slither/column.rb', line 26 def parse(value) case @type when :integer value.to_i when :float, :money value.to_f when :money_with_implied_decimal value.to_f / 100 when :date if @options[:format] Date.strptime(value, @options[:format]) else Date.strptime(value) end else value.strip end rescue raise ParserError, "The value '#{value}' could not be converted to type #{@type}: #{$!}" end |
#unpacker ⇒ Object
22 23 24 |
# File 'lib/slither/column.rb', line 22 def unpacker "A#{@length}" end |