lib/eventbrite_sdk/resource/attributes.rb in eventbrite_sdk-3.0.3 vs lib/eventbrite_sdk/resource/attributes.rb in eventbrite_sdk-3.0.4

- old
+ new

@@ -1,8 +1,13 @@ module EventbriteSDK class Resource class Attributes + SISTER_FIELDS = { + 'timezone' => 'utc', + 'utc' => 'timezone' + }.freeze + attr_reader :attrs, :changes def self.build(attrs, schema) new({}, schema).tap do |instance| instance.assign_attributes(attrs) @@ -106,15 +111,15 @@ # Since we use dirty checking to determine what the payload is # you can run into a case where a "rich media" field needs other attrs # Namely timezone, so if a rich date changed, add the tz with it. def add_rich_value(attribute_key) - if changes[attribute_key] && attribute_key =~ /\A(.+)\.utc\z/ + if changes[attribute_key] && attribute_key =~ /\A(.+)\.(utc|timezone)\z/ + field = Regexp.last_match(2) key_prefix = Regexp.last_match(1) - tz = attrs.dig(key_prefix, 'timezone') - changes["#{key_prefix}.timezone"] = [tz, tz] + handle_sister_field(key_prefix, field) end end def bury(attribute_key, value, hash = {}) keys = attribute_key.split '.' @@ -138,9 +143,18 @@ end end def respond_to_missing?(method_name, _include_private = false) attrs.has_key?(method_name.to_s) || super + end + + def handle_sister_field(key_prefix, field) + sister_field = SISTER_FIELDS[field] + stale_value = attrs.dig(key_prefix, sister_field) + + unless changes["#{key_prefix}.#{sister_field}"] + changes["#{key_prefix}.#{sister_field}"] = [stale_value, stale_value] + end end def handle_requested_attr(value) if value.is_a?(Hash) self.class.new(value)