Module: EventbriteSDK::Resource::Operations::Relationships::ClassMethods

Defined in:
lib/eventbrite_sdk/resource/operations/relationships.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(rel_method, object_class:) ⇒ Object

Builds a memoized single relationship to another Resource by dynamically defining a method on the instance with the given rel_method

class Wheel

include Resource::Operations::Relationships

belongs_to :car, object_class: 'Car', mappings: { id: :car_id }

...

end

Wheel.new(‘id’ => 4, ‘car_id’ => 1).

car #=> EventbriteSDK::Car.retrieve('id' => 1)

rel_method: Symbol of the method we are defining on this instance

e.g. belongs_to :thing => defines self#thing

object_class: String representation of resource

e.g. 'Event' => EventbriteSDK::Event


39
40
41
42
43
44
45
# File 'lib/eventbrite_sdk/resource/operations/relationships.rb', line 39

def belongs_to(rel_method, object_class:)
  define_method(rel_method) do
    relationships[rel_method] ||= build_relative(
      rel_method, object_class
    )
  end
end

#has_many(rel_method, object_class: nil, key: nil) ⇒ Object

Builds a memoized ResourceList relationship, dynamically defining a method on the instance with the given rel_method

class Car
  include Resource::Operations::Relationships

  has_many :wheels, object: 'Wheel', key: 'wheels'

  def resource_path(postfix)
    "my_path/1/#{postfix}"
  end

  # ...
end

Car.new('id' => '1').wheels

Would instantiate a new ResourceList

ResourceList.new(
  url_base: 'my_path/1/wheels',
  object_class: Wheel,
  key: :wheels
)

rel_method: Symbol of the method we are defining on this instance object_class: String representation of the Class we will give to ResourceList

key: key to use when ResourceList is extracting objects from

a list payload, if nil then rel_method is used as a default


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/eventbrite_sdk/resource/operations/relationships.rb', line 80

def has_many(rel_method, object_class: nil, key: nil)
  define_method(rel_method) do
    key ||= rel_method

    # Don't memoize if it's a new instance
    if new?
      BlankResourceList.new(key: key)
    else
      relationships[rel_method] ||= list_class(rel_method).new(
        url_base: path(rel_method),
        object_class: resource_class_from_string(object_class),
        key: key
      )
    end
  end
end