class Shiftable::ModSignature

Constants

DEFAULT_BEFORE_SHIFT
VALID_ASSOCIATIONS
VALID_TYPES

Attributes

associations[R]
base[R]
options[R]
type[R]

Public Class Methods

new(associations:, type:, options: {}) click to toggle source

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!

# File lib/shiftable/mod_signature.rb, line 12
def initialize(associations:, type:, options: {})
  @associations = associations
  @options = options
  @type = type
  # See: https://maximomussini.com/posts/practical-applications-of-the-singleton-class/
  singleton_class.send(:prepend, Object.const_get("Shiftable::ModSignature::#{type.capitalize}Methods", false))
  validate
end

Public Instance Methods

add_base(base) click to toggle source

@note Chainable @return self

# File lib/shiftable/mod_signature.rb, line 53
def add_base(base)
  @base = base
  self
end
before_shift() click to toggle source

will prevent the save if it returns false allows for any custom logic to be run, such as setting shift_from attributes, prior to the shift is saved.

# File lib/shiftable/mod_signature.rb, line 161
def before_shift
  options[:before_shift] || DEFAULT_BEFORE_SHIFT
end
belongs_to() click to toggle source

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.

# File lib/shiftable/mod_signature.rb, line 151
def belongs_to
  associations[:belongs_to]
end
has_rel_name() click to toggle source
# File lib/shiftable/mod_signature.rb, line 58
def has_rel_name
  VALID_ASSOCIATIONS[type][1]
end
invalid_association_key_type?() click to toggle source
# File lib/shiftable/mod_signature.rb, line 43
def invalid_association_key_type?
  associations.keys.detect { |key| !key.is_a?(Symbol) }
end
invalid_number_of_associations?() click to toggle source
# File lib/shiftable/mod_signature.rb, line 47
def invalid_number_of_associations?
  associations.keys.uniq.length != 2
end
invalid_type?() click to toggle source
# File lib/shiftable/mod_signature.rb, line 39
def invalid_type?
  !VALID_TYPES.include?(type)
end
method_prefix() click to toggle source
# File lib/shiftable/mod_signature.rb, line 155
def method_prefix
  options[:method_prefix]
end
polymorphic_as() click to toggle source
# File lib/shiftable/mod_signature.rb, line 35
def polymorphic_as
  options.dig(:polymorphic, :as)
end
polymorphic_type() click to toggle source
# File lib/shiftable/mod_signature.rb, line 31
def polymorphic_type
  options.dig(:polymorphic, :type)
end
shift_column() click to toggle source
# File lib/shiftable/mod_signature.rb, line 169
def shift_column
  reflection = base.reflect_on_association(belongs_to).klass.reflect_on_association(has_rel)
  reflection.foreign_key
end
Also aliased as: shift_sg_column, shift_cx_column
shift_cx_column()
Alias for: shift_column
shift_pcx_column() click to toggle source
# File lib/shiftable/mod_signature.rb, line 165
def shift_pcx_column
  "#{polymorphic_as}_id"
end
shift_sg_column()
Alias for: shift_column
validate() click to toggle source
# File lib/shiftable/mod_signature.rb, line 21
def validate
  raise ArgumentError, "type must be one of: #{VALID_TYPES}, provided: #{type}" if invalid_type?
  raise ArgumentError, "associations must be symbols" if invalid_association_key_type?
  raise ArgumentError, "exactly two distinct associations must be provided" if invalid_number_of_associations?
end
validate_relationships() click to toggle source
# File lib/shiftable/mod_signature.rb, line 62
def validate_relationships
  bt_reflection = base.reflect_on_association(belongs_to)
  raise ArgumentError, "Unable to find belongs_to: :#{belongs_to} in #{base}" unless bt_reflection
  # We can't validate any further if the reflection is polymorphic
  return true if bt_reflection.polymorphic?

  klass = bt_reflection.klass
  has_reflection = klass.reflect_on_association(has_rel)
  raise ArgumentError, "Unable to find #{has_rel_name}: :#{has_rel} in #{klass}" unless has_reflection
end
wrapper() click to toggle source
# File lib/shiftable/mod_signature.rb, line 27
def wrapper
  options[:wrapper] || {}
end