Module: Slices::PositionHelper
- Included in:
- Slice
- Defined in:
- lib/slices/position_helper.rb
Instance Method Summary collapse
-
#alone_in_adjacent_of_type? ⇒ Boolean
Are both the next and previous slices different types?.
-
#alone_in_container? ⇒ Boolean
Is this the only slice in the container?.
-
#first_adjacent_of_type? ⇒ Boolean
Is the previous slice of the same type?.
-
#first_in_container? ⇒ Boolean
Is this the first slice in the container?.
-
#last_adjacent_of_type? ⇒ Boolean
Is the next slice the same type?.
-
#last_in_container? ⇒ Boolean
Is this the last slice in the container?.
-
#next_slice ⇒ Slice
Returns the next slice in the container, or
nilif this is the last slice. -
#peers ⇒ Array
Returns an array of slices in the same container, including self.
-
#position_in_adjacent_of_type ⇒ Integer
The position of the slice in a group of the same slices.
-
#position_in_container ⇒ Integer
The position of the slice in the container, the first slice has a position of 1.
-
#previous_slice ⇒ Slice
Returns the previous slice in the container, or
nilif this is the first slice.
Instance Method Details
#alone_in_adjacent_of_type? ⇒ Boolean
Are both the next and previous slices different types?
65 66 67 |
# File 'lib/slices/position_helper.rb', line 65 def alone_in_adjacent_of_type? first_adjacent_of_type? && last_adjacent_of_type? end |
#alone_in_container? ⇒ Boolean
Is this the only slice in the container?
59 60 61 |
# File 'lib/slices/position_helper.rb', line 59 def alone_in_container? peers.size == 1 end |
#first_adjacent_of_type? ⇒ Boolean
Is the previous slice of the same type?
41 42 43 |
# File 'lib/slices/position_helper.rb', line 41 def first_adjacent_of_type? previous_slice.try(:class) != self.class end |
#first_in_container? ⇒ Boolean
Is this the first slice in the container?
35 36 37 |
# File 'lib/slices/position_helper.rb', line 35 def first_in_container? self == peers.first end |
#last_adjacent_of_type? ⇒ Boolean
Is the next slice the same type?
53 54 55 |
# File 'lib/slices/position_helper.rb', line 53 def last_adjacent_of_type? next_slice.try(:class) != self.class end |
#last_in_container? ⇒ Boolean
Is this the last slice in the container?
47 48 49 |
# File 'lib/slices/position_helper.rb', line 47 def last_in_container? self == peers.last end |
#next_slice ⇒ Slice
Returns the next slice in the container, or nil if this is the last slice.
29 30 31 |
# File 'lib/slices/position_helper.rb', line 29 def next_slice peers[peers.index(self) + 1] end |
#peers ⇒ Array
Returns an array of slices in the same container, including self.
8 9 10 11 12 |
# File 'lib/slices/position_helper.rb', line 8 def peers @peers ||= normal_or_set_page.ordered_slices.select do |slice| slice.container == self.container end end |
#position_in_adjacent_of_type ⇒ Integer
The position of the slice in a group of the same slices
For example, given the following slices
[TitleSlice, CopySlice, CopySlice, CopySlice]
The second CopySlice would have a position_in_adjacent_of_type of 2
87 88 89 90 91 92 93 94 95 |
# File 'lib/slices/position_helper.rb', line 87 def position_in_adjacent_of_type slices = [] reversed = peers.reverse reversed[reversed.index(self)..-1].each do |slice| break unless slice.class == self.class slices << slice end slices.length end |
#position_in_container ⇒ Integer
The position of the slice in the container, the first slice has a position of 1
74 75 76 |
# File 'lib/slices/position_helper.rb', line 74 def position_in_container peers.index(self) + 1 end |
#previous_slice ⇒ Slice
Returns the previous slice in the container, or nil if this is the first slice.
19 20 21 22 |
# File 'lib/slices/position_helper.rb', line 19 def previous_slice index = peers.index(self) - 1 index < 0 ? nil : peers[index] end |