Class: Tabledata::ColumnDefinition::DateProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/tabledata/column_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DateProcessor


117
118
# File 'lib/tabledata/column_definition.rb', line 117

def initialize(options)
end

Instance Method Details

#call(value, errors) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tabledata/column_definition.rb', line 119

def call(value, errors)
  case value
    when Date
      processed = value
    when Time
      errors << [:not_a_date] unless value.hour == 0 && value.min == 0 && value.sec == 0
      processed = Date.civil(value.year, value.month, value.day)
    when DateTime
      errors << [:not_a_date] unless value.hour == 0 && value.min == 0 && value.sec == 0
      processed = Date.civil(value.year, value.month, value.day)
    when /\A(\d{4})-(\d\d)-(\d\d)[T ](\d\d)(?::(\d\d)(?::(\d\d))?)?\z/
      errors << [:not_a_date] unless $4.to_i == 0 && $5.to_i == 0 && $6.to_i == 0
      processed = Date.civil($1.to_i, $2.to_i, $3.to_i)
    when /\A(\d{4})-(\d\d)-(\d\d)\z/
      processed = Date.civil($1.to_i, $2.to_i, $3.to_i)
    when /\A\s*\z/
      processed = nil
    else
      errors << [:invalid_input, {value: value}]
  end

  if processed
    # validate
  end

  processed
end