module Slices::PositionHelper

Public Instance Methods

alone_in_adjacent_of_type?() click to toggle source

Are both the next and previous slices different types?

# 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?() click to toggle source

Is this the only slice in the container?

# File lib/slices/position_helper.rb, line 59
def alone_in_container?
  peers.size == 1
end
first_adjacent_of_type?() click to toggle source

Is the previous slice of the same type?

# File lib/slices/position_helper.rb, line 41
def first_adjacent_of_type?
  previous_slice.try(:class) != self.class
end
first_in_container?() click to toggle source

Is this the first slice in the container?

# File lib/slices/position_helper.rb, line 35
def first_in_container?
  self == peers.first
end
last_adjacent_of_type?() click to toggle source

Is the next slice the same type?

# File lib/slices/position_helper.rb, line 53
def last_adjacent_of_type?
  next_slice.try(:class) != self.class
end
last_in_container?() click to toggle source

Is this the last slice in the container?

# File lib/slices/position_helper.rb, line 47
def last_in_container?
  self == peers.last
end
next_slice() click to toggle source

Returns the next slice in the container, or nil if this is the last slice.

@return [Slice]

# File lib/slices/position_helper.rb, line 29
def next_slice
  peers[peers.index(self) + 1]
end
peers() click to toggle source

Returns an array of slices in the same container, including self.

@return [Array]

# 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() click to toggle source

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

@return [Integer]

# 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() click to toggle source

The position of the slice in the container, the first slice has a position of 1

@return [Integer]

# File lib/slices/position_helper.rb, line 74
def position_in_container
  peers.index(self) + 1
end
previous_slice() click to toggle source

Returns the previous slice in the container, or nil if this is the first slice.

@return [Slice]

# File lib/slices/position_helper.rb, line 19
def previous_slice
  index = peers.index(self) - 1
  index < 0 ? nil : peers[index]
end