Sha256: f448a1cae33700c60fa3acf3a8ba65f941501c9d8fab569ead1453b113f5ea1a

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

class Module
  alias :normal_const_missing :const_missing

  def const_missing(class_id)
    begin
      return normal_const_missing(class_id)
    rescue
    end
    @magic_schema ||= DrNicMagicModels::Schema.new self
    unless table_name = @magic_schema.models[class_id]
      raise NameError.new("uninitialized constant #{class_id}") if @magic_schema.models.enquired? class_id
    end
    superklass = @magic_schema.superklass || ActiveRecord::Base
    klass = create_class(class_id, superklass) do
      set_table_name table_name
      # include DrNicMagicModels::MagicModel
      # extend DrNicMagicModels::Validations
    end
    klass.generate_validations # need to call this AFTER the class name has been assigned
    @magic_schema.inflector.post_class_creation klass
    klass
  end

  def magic_module(options)
    self.instance_variable_set "@table_name_prefix", options[:table_name_prefix] if options[:table_name_prefix]
  end

  private
  def create_class(class_name, superclass, &block)
    klass = Class.new superclass, &block
    self.const_set class_name, klass
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
voraz-dr_nic_magic_models-0.9.2 lib/module.rb
voraz-dr_nic_magic_models-0.9.3 lib/module.rb