class EventbriteSDK::Resource::Field

Constants

SIBLING_KEYMAP
SIBLING_REGEX

Attributes

datetime[R]
initial_value[R]
key[R]
schema[R]
value[R]

Public Class Methods

new(key, value, prefix: nil, schema: NullSchemaDefinition.new) click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 9
def initialize(key, value, prefix: nil, schema: NullSchemaDefinition.new)
  @key = (prefix && "#{prefix}.#{key}") || key
  @schema = schema
  @value = value
  @datetime = false
end

Public Instance Methods

apply(attrs, existing_changes) click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 30
def apply(attrs, existing_changes)
  if writeable?
    changes(attrs, existing_changes).tap { bury(attrs) }
  else
    {}
  end
end
bury(hash = {}) click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 38
def bury(hash = {})
  nested_keys = keys
  # Hand rolling #bury, hopefully we get it in the next release of Ruby.
  # UPDATE: we won't https://bugs.ruby-lang.org/issues/11747
  nested_keys.each_cons(2).reduce(hash) do |prev_attrs, (nkey, _)|
    prev_attrs[nkey] ||= {}
  end[nested_keys.last] = value

  hash
end
changes(attrs, changes) click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 16
def changes(attrs, changes)
  %i[basic_changes rich_changes].reduce(changes) do |diff, method|
    send(method, attrs, diff)
  end
end
keys() click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 22
def keys
  key.split('.')
end
writeable?() click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 26
def writeable?
  schema.writeable?(key)
end

Private Instance Methods

basic_changes(attrs, changes) click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 53
def basic_changes(attrs, changes)
  @initial_value = attrs.dig(*keys)
  comp_value = schema.dirty_comparable(self)

  changes.merge(
    (comp_value != value && { key => [initial_value, value] }) || {}
  )
end
rich_changes(attrs, changes) click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 62
def rich_changes(attrs, changes)
  if key =~ SIBLING_REGEX && changes[key] && !changes[sister_field]
    @datetime = true
    changes.merge(sister_field => sister_change(attrs))
  else
    changes
  end
end
sister() click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 71
def sister
  if key =~ SIBLING_REGEX
    key_prefix = Regexp.last_match(1)
    sister_field = SIBLING_KEYMAP[Regexp.last_match(2)]

    [key_prefix, sister_field]
  end
end
sister_change(attrs) click to toggle source

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.

# File lib/eventbrite_sdk/resource/field.rb, line 87
def sister_change(attrs)
  Array.new(2) { attrs.dig(*sister) }
end
sister_field() click to toggle source
# File lib/eventbrite_sdk/resource/field.rb, line 80
def sister_field
  sister.join('.')
end