File: after_initialize.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (39 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen-string-literal: true

module Sequel
  module Plugins
    # Adds an after_initialize hook to models, called after initializing
    # both new objects and ones loaded from the database.
    #
    # Usage:
    #
    #   # Make all model subclasses support the after_initialize hook
    #   Sequel::Model.plugin :after_initialize
    #
    #   # Make the Album class support the after_initialize hook
    #   Album.plugin :after_initialize
    module AfterInitialize
      module ClassMethods
        # Call after_initialize for model objects loaded from the database.
        def call(_)
          v = super
          v.after_initialize
          v
        end
      end

      module InstanceMethods
        # Call after_initialize for new model objects.
        def initialize(h={})
          super
          after_initialize
        end
       
        # An empty after_initialize hook, so that plugins that use this
        # can always call super to get the default behavior.
        def after_initialize
        end
      end
    end
  end
end