Module: Google::Ads::GoogleAds::AutoboxingMappings

Defined in:
lib/google/ads/google_ads/autoboxing_mappings.rb

Constant Summary collapse

MAPPINGS =
wrap_once({
  Google::Protobuf::Int32Value => lambda { |x| Integer(x) },
  Google::Protobuf::Int64Value => lambda { |x| Integer(x) },
  Google::Protobuf::StringValue => lambda { |x| String(x) },
  Google::Protobuf::BoolValue => lambda { |x|
    case x
    when TrueClass, true, "true", "TRUE", "True", "1", 1
      true
    when FalseClass, false, "false", "FALSE", "False", "0", 0
      false
    else
      raise ArgumentError.new("Value #{x} is not boolish")
    end
  },
  Google::Protobuf::FloatValue => lambda { |x| Float(x) },
  Google::Protobuf::DoubleValue => lambda { |x| Float(x) },
  Google::Protobuf::BytesValue => lambda { |x| x.force_encoding("ASCII-8BIT") },
})

Class Method Summary collapse

Class Method Details

.has_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/google/ads/google_ads/autoboxing_mappings.rb', line 44

def self.has_type?(type)
  MAPPINGS.keys.include?(type)
end

.wrap_once(mappings) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/google/ads/google_ads/autoboxing_mappings.rb', line 8

def self.wrap_once(mappings)
  Hash[mappings.map { |field_type, mapping|
    [
      field_type,
      lambda { |value|
        if value == nil
          nil
        elsif field_type === value
          value
        else
          field_type.new(value: mapping.call(value))
        end
      }
    ]
  }]
end

.wrapped_mapping(type) ⇒ Object



48
49
50
# File 'lib/google/ads/google_ads/autoboxing_mappings.rb', line 48

def self.wrapped_mapping(type)
  MAPPINGS.fetch(type)
end