Class: InOrder::Trim
- Inherits:
-
Object
- Object
- InOrder::Trim
- Includes:
- Aux::SortElements
- Defined in:
- app/models/in_order/trim.rb
Overview
Ensures a list does not exceed a specified length
Instance Attribute Summary collapse
-
#destroy ⇒ Object
(also: #destroy?)
Returns the value of attribute destroy.
-
#max ⇒ Object
Returns the value of attribute max.
-
#take_from ⇒ Object
Returns the value of attribute take_from.
Class Method Summary collapse
Instance Method Summary collapse
- #call(elements, max_size = max) ⇒ Object
-
#initialize(_max = nil, destroy: true, take_from: :bottom, max: _max) ⇒ Trim
constructor
A new instance of Trim.
Methods included from Aux::SortElements
Constructor Details
#initialize(_max = nil, destroy: true, take_from: :bottom, max: _max) ⇒ Trim
Returns a new instance of Trim.
10 11 12 13 14 15 16 |
# File 'app/models/in_order/trim.rb', line 10 def initialize(_max=nil, destroy: true, take_from: :bottom, max: _max) self.destroy = destroy self.max = max&.to_i self.take_from = take_from end |
Instance Attribute Details
#destroy ⇒ Object Also known as: destroy?
Returns the value of attribute destroy.
7 8 9 |
# File 'app/models/in_order/trim.rb', line 7 def destroy @destroy end |
#max ⇒ Object
Returns the value of attribute max.
7 8 9 |
# File 'app/models/in_order/trim.rb', line 7 def max @max end |
#take_from ⇒ Object
Returns the value of attribute take_from.
7 8 9 |
# File 'app/models/in_order/trim.rb', line 7 def take_from @take_from end |
Class Method Details
.call(*keys) ⇒ Object
40 41 42 43 44 45 46 |
# File 'app/models/in_order/trim.rb', line 40 def call(*keys) elements = InOrder::Fetch.new(*keys).elements args = block_given? ? yield : [ maximum, destroy: false ] InOrder::Trim.new(*args).call(elements).map(&:subject) end |
.set_max(max) ⇒ Object
48 49 50 51 52 |
# File 'app/models/in_order/trim.rb', line 48 def set_max(max) self.maximum = max self end |
Instance Method Details
#call(elements, max_size = max) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/models/in_order/trim.rb', line 18 def call(elements, max_size=max) return unless max_size elements = elements.dup InOrder::Element.transaction do while elements.size > max_size.to_i if take_from == :bottom delete elements.pop unlink elements.last else delete elements.shift end end end elements end |