Class: ParamsReady::Value::DateTimeCoder

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

Class Method Summary collapse

Methods inherited from Coder

value_class_name

Methods included from Extensions::ClassReaderWriter

#class_reader_writer

Methods included from Coercion

#strict_default?, #try_coerce

Class Method Details

.coerce(input, _) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/params_ready/value/coder.rb', line 166

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



179
180
181
# File 'lib/params_ready/value/coder.rb', line 179

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