Class: Sequel::Model

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

Overview

Model has some methods that are added via metaprogramming:

  • All of the methods in DATASET_METHODS have class methods created that call the Model’s dataset with the method of the same name with the given arguments.

  • All of the methods in HOOKS have class methods created that accept either a method name symbol or an optional tag and a block. These methods run the code as a callback at the specified time. For example:

    Model.before_save :do_something
    Model.before_save(:do_something_else){ self.something_else = 42}
    object = Model.new
    object.save
    

    Would run the object’s :do_something method following by the code block related to :do_something_else. Note that if you specify a block, a tag is optional. If the tag is not nil, it will overwrite a previous block with the same tag. This allows hooks to work with systems that reload code.

  • All of the methods in HOOKS also create instance methods, but you should not override these instance methods.

  • The following instance_methods all call the class method of the same name: columns, dataset, db, primary_key, db_schema.

  • The following class level attr_readers are created: allowed_columns, dataset_methods, primary_key, and restricted_columns. You should not usually need to access these directly.

  • All validation methods also accept the options specified in #validates_each, in addition to the options specified in the RDoc for that method.

  • The following class level attr_accessors are created: raise_on_typecast_failure, raise_on_save_failure, strict_param_setting, typecast_empty_string_to_nil, and typecast_on_assignment:

    # Don't raise an error if a validation attempt fails in
    # save/create/save_changes/etc.
    Model.raise_on_save_failure = false
    Model.before_save{false}
    Model.new.save # => nil
    # Don't raise errors in new/set/update/etc. if an attempt to
    # access a missing/restricted method occurs (just silently
    # skip it)
    Model.strict_param_setting = false
    Model.new(:id=>1) # No Error
    # Don't typecast attribute values on assignment
    Model.typecast_on_assignment = false
    m = Model.new
    m.number = '10'
    m.number # => '10' instead of 10
    # Don't typecast empty string to nil for non-string, non-blob columns.
    Model.typecast_empty_string_to_nil = false
    m.number = ''
    m.number # => '' instead of nil
    # Don't raise if unable to typecast data for a column
    Model.typecast_empty_string_to_nil = true
    Model.raise_on_typecast_failure = false
    m.not_null_column = '' # => nil
    m.number = 'A' # => 'A'
    
  • The following class level method aliases are defined:

    • Model.dataset= => set_dataset

    • Model.is_a => is

Defined Under Namespace

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

Constant Summary collapse

DATASET_METHODS =

Dataset methods to proxy via metaprogramming

%w'<< all avg count delete distinct eager eager_graph each each_page 
empty? except exclude filter first from from_self full_outer_join get graph 
group group_and_count group_by having inner_join insert 
insert_multiple intersect interval join join_table last 
left_outer_join limit map multi_insert naked order order_by order_more 
paginate print query range reverse_order right_outer_join select 
select_all select_more server set set_graph_aliases single_value to_csv to_hash
transform union unfiltered unordered update where with_sql'.map{|x| x.to_sym}
DATASET_METHOD_RE =

Regular expression that much match for a public instance method of a plugin dataset to have a model method created that calls it

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

Empty instance variables, for -w compliance

[:@overridable_methods_module, :@transform, :@db, :@skip_superclass_validations]
HOOKS =

Hooks that are safe for public use

[: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 =

Instance variables that are inherited in subclasses

{:@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, :@association_reflections=>:dup}
RESTRICTED_SETTER_METHODS =

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

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

Class Method Summary collapse

Methods included from Enumerable

send_each

Methods included from Inflections

clear, irregular, plural, singular, uncountable

Methods included from Metaprogramming

meta_def

Class Method Details

.plugin(plugin, *args) ⇒ Object

Loads a plugin for use with the model class, passing optional arguments to the plugin. If the plugin has a DatasetMethods module and the model doesn’t have a dataset, raise an Error.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sequel/model/plugins.rb', line 21

def self.plugin(plugin, *args)
  arg = args.first
  block = lambda{arg}
  m = plugin.is_a?(Module) ? plugin : plugin_module(plugin)
  if m.respond_to?(:apply)
    m.apply(self, *args)
  end
  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
    def_dataset_method(*m::DatasetMethods.public_instance_methods.reject{|x| DATASET_METHOD_RE !~ x.to_s})
  end
end