module Collectible::Collection::MaintainSortOrder

Public Instance Methods

disallow_when_sorted(*methods) click to toggle source
Calls superclass method
# File lib/collectible/collection/maintain_sort_order.rb, line 43
def disallow_when_sorted(*methods)
  methods.each do |method_name|
    around_method(method_name, prevent_double_wrapping_for: "MaintainSortingOrder") do |*args, **opts, &block|
      raise Collectible::MethodNotAllowedError, "cannot call #{method_name} when sorted" if maintain_sort_order?

      # TODO: replace with `super(...)` when <= 2.6 support is dropped
      if RUBY_VERSION < "2.7" && opts.blank?
        super(*args, &block)
      else
        super(*args, **opts, &block)
      end
    end
  end
end
inherited(base) click to toggle source
Calls superclass method
# File lib/collectible/collection/maintain_sort_order.rb, line 16
def inherited(base)
  base.maintain_sort_order if maintain_sort_order?
  super
end
maintain_sort_order() click to toggle source
# File lib/collectible/collection/maintain_sort_order.rb, line 27
def maintain_sort_order
  @maintain_sort_order = true
end
maintain_sort_order?() click to toggle source
# File lib/collectible/collection/maintain_sort_order.rb, line 21
def maintain_sort_order?
  @maintain_sort_order.present?
end
maintain_sorted_order_after(*methods) click to toggle source
Calls superclass method
# File lib/collectible/collection/maintain_sort_order.rb, line 33
def maintain_sorted_order_after(*methods)
  methods.each do |method_name|
    around_method(method_name, prevent_double_wrapping_for: "MaintainSortingOrder") do |*items|
      super(*items).tap do
        sort! if maintain_sort_order?
      end
    end
  end
end