Class: Remi::Transform::ParseDate
Overview
Public: Parses a string and converts it to a date. This transform is metadata aware and will use :in_format metadata from the source
type - Specify either :date, or :datetime type (default: date) in_format - The date format to use to convert the string (default: uses :in_format
from the source metadata. If that is not defined, use '%Y-%m-%d').
if_blank - Value to use if the the incoming value is blank (default: uses :if_blank
from the source metadata. If that is not defined, use nil). If set to
:high, then use the largest date, if set to :ow, use the lowest date.
Examples:
ParseDate.new(in_format: '%m/%d/%Y').to_proc.call('02/22/2013')
tform = ParseDate.new
tform.source_metadata = { in_format: '%m/%d/%Y' }
tform.to_proc.call('02/22/2013')
Instance Attribute Summary
#multi_args, #source_metadata, #target_metadata
Instance Method Summary
collapse
#call, #to_proc
Constructor Details
#initialize(*args, type: nil, in_format: nil, if_blank: nil, **kargs, &block) ⇒ ParseDate
Returns a new instance of ParseDate.
236
237
238
239
240
241
|
# File 'lib/remi/transform.rb', line 236
def initialize(*args, type: nil, in_format: nil, if_blank: nil, **kargs, &block)
super
@type = type
@in_format = in_format
@if_blank = if_blank
end
|
Instance Method Details
#blank_handler(value) ⇒ Object
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/remi/transform.rb', line 285
def blank_handler(value)
if if_blank == :low
class_type.new(1900,01,01)
elsif if_blank == :high
class_type.new(2999,12,31)
elsif if_blank.respond_to? :call
if_blank.call(value)
else
if_blank
end
end
|
#class_type ⇒ Object
277
278
279
|
# File 'lib/remi/transform.rb', line 277
def class_type
@class_type ||= type == :datetime ? Time : Date
end
|
255
256
257
258
259
260
261
|
# File 'lib/remi/transform.rb', line 255
def default_date_format
if type == :datetime
'%Y-%m-%d %H:%M:%S'
else
'%Y-%m-%d'
end
end
|
#if_blank ⇒ Object
251
252
253
|
# File 'lib/remi/transform.rb', line 251
def if_blank
@if_blank ||= @source_metadata.fetch(:if_blank, nil)
end
|
247
248
249
|
# File 'lib/remi/transform.rb', line 247
def in_format
@in_format ||= @source_metadata.fetch(:in_format, default_date_format)
end
|
#string_to_date(value) ⇒ Object
281
282
283
|
# File 'lib/remi/transform.rb', line 281
def string_to_date(value)
class_type.strptime(value, in_format)
end
|
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/remi/transform.rb', line 263
def transform(value)
begin
if value.respond_to?(:strftime)
value
elsif value.blank? then
blank_handler(value)
else
string_to_date(value)
end
rescue ArgumentError => err
raise err, "Error parsing date (#{value.class}): '#{value}' with format #{in_format})"
end
end
|
#type ⇒ Object
243
244
245
|
# File 'lib/remi/transform.rb', line 243
def type
@type ||= @source_metadata.fetch(:type, :date)
end
|