Module: FieldMaskedModel::AttributeConverter

Defined in:
lib/field_masked_model/attribute_converter.rb

Class Method Summary collapse

Class Method Details

.convert(value) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/field_masked_model/attribute_converter.rb', line 6

def convert(value)
  case value
  when Google::Protobuf::Timestamp
    timestamp_to_time(value)
  else
    # TODO(south37) Add conversion logic of other classes
    value
  end
end

.timestamp_to_time(timestamp) ⇒ Time, ActiveSupport::TimeWithZone

Parameters:

  • timestamp (Google::Protobuf::Timestamp)

Returns:

  • (Time, ActiveSupport::TimeWithZone)


18
19
20
21
22
23
24
25
26
27
# File 'lib/field_masked_model/attribute_converter.rb', line 18

def timestamp_to_time(timestamp)
  v = timestamp.nanos * (10 ** -9) + timestamp.seconds

  if Time.respond_to?(:zone) && Time.zone.respond_to?(:at)
    # Use ActiveSupport::TimeWithZone when it is available.
    Time.zone.at(v)
  else
    Time.at(v)
  end
end