Class: Sequel::Model

Inherits:
Object show all
Extended by:
Enumerable, Inflections, Metaprogramming
Includes:
Metaprogramming
Defined in:
lib/sequel/lib/sequel/model.rb,
lib/sequel/lib/sequel/model/base.rb,
lib/sequel/lib/sequel/model/errors.rb,
lib/sequel/lib/sequel/model/plugins.rb,
lib/sequel/lib/sequel/model/associations.rb

Overview

Sequel::Model 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, which have fairly standard ORM instance behavior.

Sequel::Model is built completely out of plugins, the only method not part of a plugin is the plugin method itself. Plugins can override any class, instance, or dataset method defined by a previous plugin and call super to get the default behavior.

You can set the SEQUEL_NO_ASSOCIATIONS constant or environment variable to make Sequel not load the associations plugin by default.

Defined Under Namespace

Modules: Associations, ClassMethods, DatasetMethods, InstanceMethods Classes: Errors

Constant Summary collapse

ANONYMOUS_MODEL_CLASSES =

Map that stores model classes created with Sequel::Model(), to allow the reopening of classes when dealing with code reloading.

{}
DATASET_METHODS =

Class methods added to model that call the method of the same name on the dataset

%w'<< add_graph_aliases all avg count delete distinct
each each_page eager eager_graph empty? except exclude filter first from from_self
full_outer_join get graph grep group group_and_count group_by having import
inner_join insert insert_multiple intersect interval invert join join_table
last left_outer_join limit map max min multi_insert naked order order_by
order_more paginate print qualify query range reverse reverse_order right_outer_join
select select_all select_more server set set_defaults set_graph_aliases set_overrides
single_value sum to_csv to_hash truncate unfiltered ungraphed ungrouped union unlimited unordered 
update where with with_recursive with_sql'.map{|x| x.to_sym}
EMPTY_INSTANCE_VARIABLES =

Class instance variables to set to nil when a subclass is created, for -w compliance

[:@overridable_methods_module, :@db]
HOOKS =

Empty instance methods to create that the user can override to get hook/callback behavior. Just like any other method defined by Sequel, if you override one of these, you should call super to get the default behavior (while empty by default, they can also be defined by plugins).

[:after_initialize, :before_create, :after_create, :before_update,
:after_update, :before_save, :after_save, :before_destroy, :after_destroy,
:before_validation, :after_validation]
INHERITED_INSTANCE_VARIABLES =

Class instance variables that are inherited in subclasses. If the value is :dup, dup is called on the superclass's instance variable when creating the instance variable in the subclass. If the value is nil, the superclass's instance variable is used directly in the subclass.

{:@allowed_columns=>:dup, :@dataset_methods=>:dup, 
:@dataset_method_modules=>:dup, :@primary_key=>nil, :@use_transactions=>nil,
:@raise_on_save_failure=>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}
NORMAL_METHOD_NAME_REGEXP =

Regexp that determines if a method name is normal in the sense that it could be called directly in ruby code without using send. Used to avoid problems when using eval with a string to define methods.

/\A[A-Za-z_][A-Za-z0-9_]*\z/
RESTRICTED_SETTER_METHODS =

The setter methods (methods ending with =) that are never allowed to be called automatically via set/update/new/etc..

%w"== === []= taguri= typecast_empty_string_to_nil= typecast_on_assignment= strict_param_setting= raise_on_save_failure= raise_on_typecast_failure="
SETTER_METHOD_REGEXP =

Regular expression that determines if the method is a valid setter name (i.e. it ends with =).

/=\z/

Constants included from Inflections

Inflections::CAMELIZE_CONVERT_REGEXP, Inflections::CAMELIZE_MODULE_REGEXP, Inflections::DASH, Inflections::DEMODULIZE_CONVERT_REGEXP, Inflections::EMPTY_STRING, Inflections::SLASH, Inflections::UNDERSCORE, Inflections::UNDERSCORE_CONVERT_REGEXP1, Inflections::UNDERSCORE_CONVERT_REGEXP2, Inflections::UNDERSCORE_CONVERT_REPLACE, Inflections::UNDERSCORE_MODULE_REGEXP, Inflections::VALID_CONSTANT_NAME_REGEXP

Class Method Summary collapse

Methods included from Metaprogramming

meta_def

Methods included from Inflections

clear, irregular, plural, singular, uncountable

Methods included from Enumerable

fourth, second, third

Class Method Details

.columns(*cols) ⇒ Object


80
81
82
83
84
85
86
87
88
# File 'lib/sequel/spec/model/spec_helper.rb', line 80

def columns(*cols)
  return if cols.empty?
  define_method(:columns){cols}
  @dataset.instance_variable_set(:@columns, cols) if @dataset
  def_column_accessor(*cols)
  @columns = cols
  @db_schema = {}
  cols.each{|c| @db_schema[c] = {}}
end

.orig_columnsObject


79
80
81
82
83
84
85
86
87
# File 'lib/sequel/spec/model/spec_helper.rb', line 79

def columns(*cols)
  return if cols.empty?
  define_method(:columns){cols}
  @dataset.instance_variable_set(:@columns, cols) if @dataset
  def_column_accessor(*cols)
  @columns = cols
  @db_schema = {}
  cols.each{|c| @db_schema[c] = {}}
end

.plugin(plugin, *args, &blk) ⇒ Object

Loads a plugin for use with the model class, passing optional arguments to the plugin. If the plugin is a module, load it directly. Otherwise, require the plugin from either sequel/plugins/#plugin or sequel_#plugin, and then attempt to load the module using a the camelized plugin name under Sequel::Plugins.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sequel/lib/sequel/model/plugins.rb', line 28

def self.plugin(plugin, *args, &blk)
  arg = args.first
  block = args.length > 1 ? lambda{args} : lambda{arg}
  m = plugin.is_a?(Module) ? plugin : plugin_module(plugin)
  unless @plugins.include?(m)
    @plugins << m
    m.apply(self, *args, &blk) if m.respond_to?(:apply)
    if m.const_defined?("InstanceMethods")
      define_method(:"#{plugin}_opts", &block)
      include(m::InstanceMethods)
    end
    if m.const_defined?("ClassMethods")
      meta_def(:"#{plugin}_opts", &block)
      extend(m::ClassMethods)
    end
    if m.const_defined?("DatasetMethods")
      if @dataset
        dataset.meta_def(:"#{plugin}_opts", &block)
        dataset.extend(m::DatasetMethods)
      end
      dataset_method_modules << m::DatasetMethods
      meths = m::DatasetMethods.public_instance_methods.reject{|x| NORMAL_METHOD_NAME_REGEXP !~ x.to_s}
      def_dataset_method(*meths) unless meths.empty?
    end
  end
  m.configure(self, *args, &blk) if m.respond_to?(:configure)
end

.simple_tableObject


89
90
91
# File 'lib/sequel/spec/model/spec_helper.rb', line 89

def simple_table
  nil
end