Class: Haecksler::Column

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

Constant Summary collapse

DEFAULT_OPTIONS =
{type: :string, date_format: nil}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {type: :string}) ⇒ Column

Returns a new instance of Column.



33
34
35
36
37
38
39
# File 'lib/haecksler/column.rb', line 33

def initialize(options = {type: :string})
  options = DEFAULT_OPTIONS.merge(options)
  @name = options[:name]
  @size = options[:size]
  @type = options[:type]
  @date_format = options[:date_format]
end

Instance Attribute Details

#date_formatObject (readonly)

Returns the value of attribute date_format.



29
30
31
# File 'lib/haecksler/column.rb', line 29

def date_format
  @date_format
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/haecksler/column.rb', line 29

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



29
30
31
# File 'lib/haecksler/column.rb', line 29

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/haecksler/column.rb', line 29

def type
  @type
end

Instance Method Details

#parse(string_value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/haecksler/column.rb', line 41

def parse(string_value)
  parsed = ParsedColumn.new(self)

  parsed.value = begin
    case type
    when :nil
      nil
    when :string
      String(string_value).rstrip
    when :integer
      Integer(string_value)
    when :float
      Float(string_value)
    when :date
      if date_format.nil?
        Date.parse(string_value)
      else
        Date.strptime(string_value, date_format)
      end
    when :datetime
      if date_format.nil?
        DateTime.parse(string_value)
      else
        DateTime.strptime(string_value, date_format)
      end
    else
      nil
    end
  rescue
    TypeError.new(string_value, self)
  end

  parsed
end