Class: ParamsReady::Value::DateTimeCoder

Inherits:
Coder show all
Defined in:
lib/params_ready/value/coder.rb

Class Method Summary collapse

Methods inherited from Coder

instance

Methods included from Coercion

#strict_default?, #try_coerce

Methods inherited from AbstractCoder

value_class_name

Methods included from Extensions::ClassReaderWriter

#class_reader_writer

Class Method Details

.coerce(input, _) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/params_ready/value/coder.rb', line 194

def self.coerce(input, _)
  return nil if input.nil? || input == ''
  if input.is_a?(Numeric)
    Time.at(input).to_datetime
  elsif input.is_a?(String)
    DateTime.parse(input)
  elsif input.respond_to?(:to_datetime)
    input.to_datetime
  else
    raise ParamsReadyError, "Unimplemented for type #{input.class.name}"
  end
end

.format(value, format) ⇒ Object



207
208
209
# File 'lib/params_ready/value/coder.rb', line 207

def self.format(value, format)
  value.to_s
end