class Shiftable::Single

Inheriting from Module is a powerful pattern. If you like it checkout the debug_logging gem!

Public Class Methods

new(belongs_to:, has_one:, method_prefix: nil, precheck: true, before_shift: nil, wrapper: nil) click to toggle source
Calls superclass method
# File lib/shiftable/single.rb, line 24
def initialize(belongs_to:, has_one:, method_prefix: nil, precheck: true, before_shift: nil, wrapper: nil)
  # Ruby's Module initializer doesn't take any arguments
  super()

  @signature = ModSignature.new(
    # For the following, imagine you are a Spaceship Captain, the Spaceship belongs_to you, and it has only one Captain.
    # But you have to sell it to your nemesis!
    associations: {
      # The name of the belongs_to association, defined on the shifting model, e.g. Spaceship
      # Normally a camel-cased, symbolized, version of the class name.
      # In the case where Spaceship belongs_to: :captain, this is :captain.
      belongs_to: belongs_to.to_s.to_sym,
      # The name of the has_one association, defined on the shift_to/shift_from model, e.g. Captain.
      # Normally a camel-cased, symbolized, version of the class name.
      # In the case where Captain has_one: :spaceship, this is :spaceship.
      has_one: has_one.to_s.to_sym
    },
    options: {
      # Do not move record if a record already exists (we are shifting a "has_one" association, after all)
      precheck: precheck,
      method_prefix: method_prefix,
      # will prevent the save if it returns false
      # allows for any custom logic to be run, such as setting attributes, prior to the shift (save).
      before_shift: before_shift,
      wrapper: wrapper
    },
    type: :sg
  )
end

Public Instance Methods

extended(base) click to toggle source

NOTE: Possible difference in how inheritance works when using extend vs include

with Shiftable::Single.new
# File lib/shiftable/single.rb, line 56
def extended(base)
  shift_single_modulizer = ShiftSingleModulizer.to_mod(@signature.add_base(base))
  base.singleton_class.send(:prepend, shift_single_modulizer)
end
included(base) click to toggle source

NOTE: Possible difference in how inheritance works when using extend vs include

with Shiftable::Single.new
# File lib/shiftable/single.rb, line 63
def included(base)
  shift_single_modulizer = ShiftSingleModulizer.to_mod(@signature.add_base(base))
  base.send(:prepend, shift_single_modulizer)
end