Method: DataTools::Hash#cleanse

Defined in:
lib/data_tools/hash.rb

#cleanse(options = {}) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/data_tools/hash.rb', line 183

def cleanse(options = {})
  each_with_object({}) do |(k,v), out|
    out[k] = DataTools.scour(v, options)

    if dateformat = options[:datefields][k]
      begin
        out[k] = v && DateTime.strptime(v, dateformat).to_date
      rescue ArgumentError
        warn "expected '#{dateformat}' in #{k} = '#{v}' at [#{options[:line]}]: #{self}"
        out[k] = nil
      end
    end

    if timeformat = options[:timefields][k]
      begin
        out[k] = v && DateTime.strptime(v, timeformat).to_time.utc
      rescue ArgumentError
        warn "expected '#{timeformat}' in #{k} = '#{v}' at [#{options[:line]}]: #{self}"
        out[k] = nil
      end
    end
  end
end