Class: InOrder::Purge

Inherits:
Object
  • Object
show all
Includes:
Aux::VarKeys
Defined in:
app/models/in_order/purge.rb

Overview

Removes unwanted subjects from a list. Identified (i.e. keyed) by an Owner and/or Scope.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Aux::VarKeys

included, #initialize

Class Method Details

.delete_by_subject(subject) ⇒ Object



40
41
42
43
44
45
46
# File 'app/models/in_order/purge.rb', line 40

def self.delete_by_subject(subject)
  InOrder::Element.transaction do
    InOrder::Element.by_subject(subject).each do |element|
      Remove.new(element).call
    end
  end
end

Instance Method Details

#call(keep_last: false) ⇒ Object Also known as: uniq

keep_last determines whether the first or last occurrence remains



9
10
11
12
13
14
15
16
17
# File 'app/models/in_order/purge.rb', line 9

def call(keep_last: false)
  elements = get_elements

  elements.reverse! if keep_last

  traverse elements do |element, found_subjects, subject|
    Remove.new(element).call if found_subjects.include?(subject)
  end
end

#exists?(subject) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/in_order/purge.rb', line 36

def exists?(subject)
  InOrder::Element.has_subject?(keys) { subject }
end

#remove(*subjects, elements: get_elements) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/in_order/purge.rb', line 20

def remove(*subjects, elements: get_elements)
  did_remove = false

  subjects.flatten! if Array === subjects.first

  traverse elements do |element|
    if subjects.include?(element.subject)
      Remove.new(element).call

      did_remove = true
    end
  end

  did_remove
end