Class: Remi::Transform::EnforceType
Overview
Public: Enforces the type declared in the :type metadata field (if it exists)
Examples:
tform = EnforceType.new
tform.source_metadata = { type: :date, in_format: '%m/%d/%Y' }
tform.to_proc.call('02/22/2013')
tform = EnforceType.new
tform.source_metadata = { type: :integer }
tform.to_proc.call('12')
tform = EnforceType.new
tform.source_metadata = { type: :integer }
tform.to_proc.call('12A')
Instance Attribute Summary
#multi_arg, #source_metadata, #target_metadata
Instance Method Summary
collapse
#call, #to_proc
Constructor Details
#initialize(*args, **kargs, &block) ⇒ EnforceType
440
441
442
|
# File 'lib/remi/transform.rb', line 440
def initialize(*args, **kargs, &block)
super
end
|
Instance Method Details
#blank_handler(value) ⇒ Object
462
463
464
465
466
467
468
469
470
|
# File 'lib/remi/transform.rb', line 462
def blank_handler(value)
return value unless value.blank?
if if_blank.respond_to? :to_proc
if_blank.to_proc.call(value)
else
if_blank
end
end
|
#if_blank ⇒ Object
456
457
458
459
460
|
# File 'lib/remi/transform.rb', line 456
def if_blank
return @if_blank if @if_blank_set
@if_blank_set = true
@if_blank = @source_metadata.fetch(:if_blank, nil)
end
|
448
449
450
|
# File 'lib/remi/transform.rb', line 448
def in_format
@in_format ||= @source_metadata.fetch(:in_format, '')
end
|
#scale ⇒ Object
452
453
454
|
# File 'lib/remi/transform.rb', line 452
def scale
@scale ||= @source_metadata.fetch(:scale, 0)
end
|
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
# File 'lib/remi/transform.rb', line 472
def transform(value)
if value.blank?
blank_handler(value)
else
case type
when :string
value
when :integer
Integer(value)
when :float
Float(value)
when :decimal
Float("%.#{scale}f" % Float(value))
when :date
Date.strptime(value, in_format)
when :datetime
Time.strptime(value, in_format)
else
raise ArgumentError, "Unknown type enforcement: #{type}"
end
end
end
|
#type ⇒ Object
444
445
446
|
# File 'lib/remi/transform.rb', line 444
def type
@type ||= @source_metadata.fetch(:type, :string)
end
|