class GreyscaleRecord::Associations::Base

Attributes

key[RW]
name[RW]
opts[RW]

Public Class Methods

new( base_class, name, opts = {} ) click to toggle source
# File lib/greyscale_record/associations/base.rb, line 7
def initialize( base_class, name, opts = {} )
  self.name = name
  self.opts = opts
  self.key  = idify base_class

  sanitize_opts( base_class )
end

Public Instance Methods

klass( instance = nil ) click to toggle source
# File lib/greyscale_record/associations/base.rb, line 15
def klass( instance = nil )
  @klass ||= association_class name, opts
end

Protected Instance Methods

association_class( name, opts = {} ) click to toggle source
# File lib/greyscale_record/associations/base.rb, line 21
def association_class( name, opts = {} )
  a_class       = opts[:class]                   if opts.has_key? :class
  a_class_name  = opts[:class_name].constantize  if opts.has_key? :class_name

  a_class || a_class_name || derived_class_for( name )
end
derived_class_for( name ) click to toggle source
# File lib/greyscale_record/associations/base.rb, line 28
def derived_class_for( name )
  name.to_s.singularize.classify.constantize
end
idify( class_name ) click to toggle source
# File lib/greyscale_record/associations/base.rb, line 32
def idify( class_name )
  "#{class_name.to_s.demodulize.underscore}_id"
end
sanitize_opts( base_class ) click to toggle source
# File lib/greyscale_record/associations/base.rb, line 40
def sanitize_opts( base_class )
  if name.to_s == "object"
    raise GreyscaleRecord::Errors::AssociationError, "#{base_class}: 'object_id' is a reserved keyword in ruby and cannot be overridden"
  end
  invalid_options = (opts.keys - supported_options)
  if invalid_options.present?
    list = invalid_options.map{|o|"'#{o}'"}.to_sentence
    plural = invalid_options.count > 1
    raise GreyscaleRecord::Errors::AssociationError, "#{base_class}: #{list} #{plural ? "are" : "is"} not supported. Please see README for full details: http://www.github.com/greyscale-io/greyscale_record"
  end
end
supported_options() click to toggle source
# File lib/greyscale_record/associations/base.rb, line 36
def supported_options
  [ :class, :class_name ] + additional_options
end