Module: FieldMaskedModel::AttributeConverter

Defined in:
lib/field_masked_model/attribute_converter.rb

Constant Summary collapse

NULLABLE_TYPES =
[
  Google::Protobuf::DoubleValue,
  Google::Protobuf::FloatValue,
  Google::Protobuf::Int64Value,
  Google::Protobuf::UInt64Value,
  Google::Protobuf::Int32Value,
  Google::Protobuf::UInt32Value,
  Google::Protobuf::BoolValue,
  Google::Protobuf::StringValue,
  Google::Protobuf::BytesValue,
].freeze

Class Method Summary collapse

Class Method Details

.convert(value) ⇒ Object



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

def convert(value)
  case value
  when Google::Protobuf::Timestamp
    timestamp_to_time(value)
  when *NULLABLE_TYPES
    value.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)


33
34
35
36
37
38
39
40
41
42
# File 'lib/field_masked_model/attribute_converter.rb', line 33

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