lib/sequel/model.rb in sequel-3.33.0 vs lib/sequel/model.rb in sequel-3.34.0
- old
+ new
@@ -33,17 +33,22 @@
# # Using a database
# class Comment < Sequel::Model(DB1)
# dataset # => DB1[:comments]
# end
def self.Model(source)
- Model::ANONYMOUS_MODEL_CLASSES[source] ||= if source.is_a?(Database)
+ if Sequel::Model.cache_anonymous_models && (klass = Model::ANONYMOUS_MODEL_CLASSES[source])
+ return klass
+ end
+ klass = if source.is_a?(Database)
c = Class.new(Model)
c.db = source
c
else
Class.new(Model).set_dataset(source)
end
+ Model::ANONYMOUS_MODEL_CLASSES[source] = klass if Sequel::Model.cache_anonymous_models
+ klass
end
# <tt>Sequel::Model</tt> is an object relational mapper built on top of Sequel core. Each
# model class is backed by a dataset instance, and many dataset methods can be
# called directly on the class. Model datasets return rows as model instances,
@@ -56,10 +61,13 @@
# for the associations support.
#
# You can set the +SEQUEL_NO_ASSOCIATIONS+ constant or environment variable to
# make Sequel not load the associations plugin by default.
class Model
+ # Cache anonymous models created by Sequel::Model()
+ @cache_anonymous_models = true
+
# Map that stores model classes created with <tt>Sequel::Model()</tt>, to allow the reopening
# of classes when dealing with code reloading.
ANONYMOUS_MODEL_CLASSES = {}
# Class methods added to model that call the method of the same name on the dataset
@@ -103,10 +111,11 @@
:@raise_on_save_failure=>nil, :@require_modification=>nil,
:@restricted_columns=>:dup, :@restrict_primary_key=>nil,
:@simple_pk=>nil, :@simple_table=>nil, :@strict_param_setting=>nil,
:@typecast_empty_string_to_nil=>nil, :@typecast_on_assignment=>nil,
:@raise_on_typecast_failure=>nil, :@plugins=>:dup, :@setter_methods=>nil,
- :@use_after_commit_rollback=>nil}
+ :@use_after_commit_rollback=>nil, :@fast_pk_lookup_sql=>nil,
+ :@fast_instance_delete_sql=>nil}
# Regular expression that determines if a method name is normal in the sense that
# it could be used literally in ruby code without using send. Used to
# avoid problems when using eval with a string to define methods.
NORMAL_METHOD_NAME_REGEXP = /\A[A-Za-z_][A-Za-z0-9_]*\z/