Sha256: a11b20528d38c797f36b56936d42e07a86cf77527bc35c7f8c682bbe6f8c4da3

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'active_support/core_ext/string/inflections'
require 'super_model/associations/belongs_to'
require 'super_model/associations/has_many'
require 'super_model/associations/has_one'
require 'super_model/error/invalid_plurality'

class SuperModel
  
  # SuperModel::Associations adds associations for standard Ruby objects and
  # provides a base for ORMs to add their own association functionality.
  # 
  # This module must be included in a Class.
  module Associations
    
    class << self
      
      def sanitize_resource_name(resource_name, plurality)
        Error::InvalidPlurality.check(plurality)
        
        resource_name.to_s.send("#{ plurality }ize").underscore
      end
      
      def foreign_key_for(resource_name, plurality)
        Error::InvalidPlurality.check(plurality)
        
        "#{resource_name}_id".send("#{ plurality }ize")
      end
      
      def extend_with_associations(receiver)
        receiver.extend(HasOne)
        receiver.extend(HasMany)
        receiver.extend(BelongsTo)
      end
      
      def included(receiver)
        extend_with_associations(receiver)
      end
      
      def extended(receiver)
        extend_with_associations(receiver)
      end
      
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
super_model-0.0.1 lib/super_model/associations.rb