Class: Slither::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/slither/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  assert_valid_options(options)
  @name = name
  @length = length
  @options = options
  @alignment = options[:align] || :right
  @type = options[:type] || :string
  @padding = options[:padding] || :space
  @truncate = options[:truncate] || false
  # Only used with floats, this determines the decimal places
  @precision = options[:precision] 
end

Instance Attribute Details

#alignmentObject (readonly)

Returns the value of attribute alignment.



7
8
9
# File 'lib/slither/column.rb', line 7

def alignment
  @alignment
end

#lengthObject (readonly)

Returns the value of attribute length.



7
8
9
# File 'lib/slither/column.rb', line 7

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/slither/column.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/slither/column.rb', line 7

def options
  @options
end

#paddingObject (readonly)

Returns the value of attribute padding.



7
8
9
# File 'lib/slither/column.rb', line 7

def padding
  @padding
end

#precisionObject (readonly)

Returns the value of attribute precision.



7
8
9
# File 'lib/slither/column.rb', line 7

def precision
  @precision
end

#typeObject (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

#unpackerObject



22
23
24
# File 'lib/slither/column.rb', line 22

def unpacker
  "A#{@length}"
end