Class: Google::Ads::GoogleAds::FieldMaskUtil
- Inherits:
-
Object
- Object
- Google::Ads::GoogleAds::FieldMaskUtil
- Defined in:
- lib/google/ads/google_ads/field_mask_util.rb
Overview
Utility for constructing FieldMask objects.
Copied from C# implementation courtesy of jonskeet@google.com
Constant Summary collapse
- WRAPPER_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
-
.all_set_fields_of(obj) ⇒ Object
Construct a field mask containing any fields set on the given object.
-
.compare(original, modified) ⇒ Google::Protobuf::FieldMask
Creates Google::Protobuf::FieldMask objects based on the difference between two objects.
-
.is_clearing_message?(original, modified) ⇒ Boolean
Checks if we’ve updated to a blank message in an attempt to clear an existing value.
- .is_empty?(object) ⇒ Boolean
- .is_empty_value?(value) ⇒ Boolean
-
.with(obj) { ... } ⇒ Google::Protobuf::FieldMask
Construct a field mask containing any changes to the object made in the given block.
Class Method Details
.all_set_fields_of(obj) ⇒ Object
Construct a field mask containing any fields set on the given object.
Example:
obj = MyObject.new
obj.some_property = 1
obj.other_property = 2
mask = FieldMaskUtil.all_set_fields_of obj
# the mask will contain the present properties "some_property" and
# "other_property"
api_client.mutate obj, mask
return [Google::Protobuf::FieldMask] the computed mask
70 71 72 73 |
# File 'lib/google/ads/google_ads/field_mask_util.rb', line 70 def self.all_set_fields_of(obj) new_instance = obj.class.new compare_obj(Google::Protobuf::FieldMask.new, '', new_instance, obj) end |
.compare(original, modified) ⇒ Google::Protobuf::FieldMask
Creates Google::Protobuf::FieldMask objects based on the difference between two objects.
81 82 83 84 85 |
# File 'lib/google/ads/google_ads/field_mask_util.rb', line 81 def self.compare(original, modified) raise 'nil cannot be compared' if original.nil? || modified.nil? raise 'objects of different types cannot be compared' if original.class != modified.class compare_obj(Google::Protobuf::FieldMask.new, '', original, modified) end |
.is_clearing_message?(original, modified) ⇒ Boolean
Checks if we’ve updated to a blank message in an attempt to clear an existing value.
153 154 155 156 |
# File 'lib/google/ads/google_ads/field_mask_util.rb', line 153 def self.(original, modified) (modified.nil? && !original.nil? && is_empty?(original)) || (original.nil? && !modified.nil? && is_empty?(modified)) end |
.is_empty?(object) ⇒ Boolean
158 159 160 161 162 163 164 165 166 |
# File 'lib/google/ads/google_ads/field_mask_util.rb', line 158 def self.is_empty?(object) fields = object.class.descriptor.entries fields.each do |field| if !is_empty_value? object[field.name] return false end end true end |
.is_empty_value?(value) ⇒ Boolean
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/google/ads/google_ads/field_mask_util.rb', line 168 def self.is_empty_value?(value) # Some types throw errors if we try to do an == comparison, in # particular repeated fields that have data. If anything goes wrong, # just assume it's not empty. begin return value.nil? || (value.respond_to?(:empty?) && value.empty?) || value == false || value == 0 || value == :UNSPECIFIED rescue return false end end |
.with(obj) { ... } ⇒ Google::Protobuf::FieldMask
Construct a field mask containing any changes to the object made in the given block.
Example:
obj = MyObject.new
obj.some_property = 1
mask = FieldMaskUtil.with obj do
obj.other_property = 2
end
# the mask will contain the changed property "other_property"
api_client.mutate obj, mask
47 48 49 50 51 52 |
# File 'lib/google/ads/google_ads/field_mask_util.rb', line 47 def self.with(obj) raise 'nil cannot be compared' if obj.nil? original = obj.class.decode(obj.class.encode(obj)) yield obj compare original, obj end |