Module: Yt::Associations::HasMany

Included in:
Models::Base
Defined in:
lib/yt/associations/has_many.rb

Overview

Associations are a set of macro-like class methods to express relationship between YouTube resources like “Channel has many Videos” or “Account has one Id”. They are inspired by ActiveRecord::Associations.

Instance Method Summary collapse

Instance Method Details

#has_many(attributes) ⇒ Object

Examples:

Adds the videos method to the Channel resource.

class Channel < Resource
  has_many :videos
end


11
12
13
14
15
16
17
18
19
# File 'lib/yt/associations/has_many.rb', line 11

def has_many(attributes)
  require 'active_support' # does not load anything by default
  require 'active_support/core_ext/string/inflections' # for camelize ...
  require "yt/collections/#{attributes}"

  collection_name = attributes.to_s.sub(/.*\./, '').camelize.pluralize
  collection = "Yt::Collections::#{collection_name}".constantize
  define_memoized_method(attributes) { collection.of self }
end