#-- # ORMSupport # # Copyright (c) 2004,2005 Thomas Sawyer # # Ruby License # # This module is free software. You may use, modify, and/or redistribute this # software under the same terms as Ruby. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. # # ========================================================================== # Revision History # ========================================================================== # # 5.8.25 Trans # - Created based on a set of methods from Rails. Spruced up to support # Multiple frameworks, mainly Nitro. # # ========================================================================== #++ #:title: ORMSupport # # Provides a number of basc convenience methods for working with Object # Relational Mappings. # # == Usage # # TODO # # == Author(s) # # * D.H.H. # * George Moschovitis # * Thomas Sawyer # require 'nano/module/basename' require 'nano/string/to_const' require 'nano/inflect' module ORMSupport ################## # class level ################## def self.default_key @default_key ||= '_id' end def self.default_key=(x) @default_key = x.to_s end ################## # instance level ################## # Converts a class-esque name into a table name. # def tablename( type=:nitro ) case type when :rails, :ar, :activerecord self.to_s.underscore.plural when :nitro self.to_s.gsub(/::/, '_').downcase else self.to_s.gsub(/::/, '_').downcase end end alias_method :table_name, :tablename # a la Rails alias_method :tableize, :tablename # a la Rails # Converts a table name into a class name. # def classname( type=:nitro ) case type when :rails, :ar, :activerecord self.to_s.singular.camelize when :nitro self.to_s.gsub(/_/, '::').camelize else self.to_s.gsub(/_/, '::').camelize end end alias_method :calss_name, :classname # a la Rails # Like classname, but returns the actual class. # def classify( type=:nitro ) classname( type ).to_const end # This is good for generating database key/id field names from class names. # In essense, it demodulizes, underscores and appends 'id'. # def keyname( type=nil ) case type when :_id self.basename.underscore + "_id" when :id self.basename.underscore + "id" when :_key self.basename.underscore + "_key" when :key self.basename.underscore + "key" else self.basename.underscore + ORMSupport.default_key end end alias_method :key_name, :keyname # to be consistant alias_method :foreign_key, :keyname # a la Rails end #-- class String include ORMSupport end class Class include ORMSupport alias_method :demodulize, :basename # a la Rails end #++