class EventbriteSDK::Resource::Attributes

Attributes

attrs[R]
changes[R]
schema[R]

Public Class Methods

build(attrs, schema) click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 6
def self.build(attrs, schema)
  new({}, schema).tap do |instance|
    instance.assign_attributes(attrs)
  end
end
new(hydrated_attrs = {}, schema = NullSchemaDefinition.new) click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 12
def initialize(hydrated_attrs = {}, schema = NullSchemaDefinition.new)
  @changes = {}
  @schema = schema

  # Build out initial hash based on schema's defined keys
  @attrs = schema.defined_keys.each_with_object({}) do |key, attrs|
    Field.new(key, nil).bury(attrs)
  end.merge(stringify_keys(hydrated_attrs))
end

Public Instance Methods

[](key) click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 22
def [](key)
  public_send(key)
end
assign_attributes(new_attrs) click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 26
def assign_attributes(new_attrs)
  stringify_keys(new_attrs).each do |attribute_key, value|
    value = Field.new(attribute_key, value, schema: schema)
    changes.merge! value.apply(attrs, changes)
  end

  nil
end
changed?() click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 35
def changed?
  changes.any?
end
inspect() click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 47
def inspect
  "#<#{self.class}: #{JSON.pretty_generate(@attrs.to_h)}>"
end
payload(prefix = nil) click to toggle source

Provides changeset in a format that can be thrown at an endpoint

prefix: This is needed due to inconsistencies in the EB API

Sometimes there's a prefix, sometimes there's not,
sometimes it's singular, sometimes it's plural.
Once the API gets a bit more normalized we can remove this
altogether and infer a prefix based
on the class name of the resource
# File lib/eventbrite_sdk/resource/attributes.rb, line 69
def payload(prefix = nil)
  changes.each_with_object({}) do |(attribute_key, (_, value)), payload|
    Field.new(attribute_key, value, prefix: prefix).bury(payload)
  end
end
reset!() click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 51
def reset!
  changes.each do |attribute_key, (old_value, _current_value)|
    Field.new(attribute_key, old_value).bury(attrs)
  end

  @changes = {}

  true
end
to_h() click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 39
def to_h
  attrs.to_h
end
to_json(opts = {}) click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 43
def to_json(opts = {})
  to_h.to_json(opts)
end
values() click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 75
def values
  attrs.values
end

Private Instance Methods

handle_requested_attr(value) click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 97
def handle_requested_attr(value)
  if value.is_a?(Hash)
    self.class.new(value)
  else
    value
  end
end
method_missing(method_name, *_args, &_block) click to toggle source
Calls superclass method
# File lib/eventbrite_sdk/resource/attributes.rb, line 83
def method_missing(method_name, *_args, &_block)
  requested_key = method_name.to_s

  if attrs.key?(requested_key)
    handle_requested_attr(attrs[requested_key])
  else
    super
  end
end
respond_to_missing?(method_name, _include_private = false) click to toggle source
Calls superclass method
# File lib/eventbrite_sdk/resource/attributes.rb, line 93
def respond_to_missing?(method_name, _include_private = false)
  attrs.key?(method_name.to_s) || super
end
stringify_keys(params) click to toggle source
# File lib/eventbrite_sdk/resource/attributes.rb, line 105
def stringify_keys(params)
  params.to_h.map { |key, value| [key.to_s, value] }.to_h
end