class Superstore::Associations::Reflection

Attributes

macro[R]
model[R]
name[R]
options[R]

Public Class Methods

new(macro, name, model, options) click to toggle source
# File lib/superstore/associations/reflection.rb, line 5
def initialize(macro, name, model, options)
  @macro  = macro
  @name   = name
  @model  = model
  @options = options
end

Public Instance Methods

association_class() click to toggle source
# File lib/superstore/associations/reflection.rb, line 12
def association_class
  case macro
  when :belongs_to
    Superstore::Associations::BelongsTo
  when :has_many
    Superstore::Associations::HasMany
  when :has_one
    Superstore::Associations::HasOne
  end

end
belongs_to?() click to toggle source
# File lib/superstore/associations/reflection.rb, line 48
def belongs_to?; false; end
class_name() click to toggle source
# File lib/superstore/associations/reflection.rb, line 50
def class_name
  @class_name ||= (options[:class_name] || name.to_s.classify)
end
default_primary_key?() click to toggle source
# File lib/superstore/associations/reflection.rb, line 36
def default_primary_key?
  primary_key == "id"
end
foreign_key() click to toggle source
# File lib/superstore/associations/reflection.rb, line 28
def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key
end
instance_variable_name() click to toggle source
# File lib/superstore/associations/reflection.rb, line 24
def instance_variable_name
  "@#{name}"
end
inverse_name() click to toggle source
# File lib/superstore/associations/reflection.rb, line 54
def inverse_name
  options[:inverse_of]
end
polymorphic?() click to toggle source
# File lib/superstore/associations/reflection.rb, line 44
def polymorphic?
  options[:polymorphic]
end
polymorphic_column() click to toggle source
# File lib/superstore/associations/reflection.rb, line 40
def polymorphic_column
  "#{name}_type"
end
primary_key() click to toggle source
# File lib/superstore/associations/reflection.rb, line 32
def primary_key
  options[:primary_key] || "id"
end

Private Instance Methods

derive_foreign_key() click to toggle source
# File lib/superstore/associations/reflection.rb, line 60
def derive_foreign_key
  case macro
  when :has_many, :has_one
    model.name.foreign_key
  when :belongs_to
    "#{name}_id"
  end
end