Module: Slices::PositionHelper

Included in:
Slice
Defined in:
lib/slices/position_helper.rb

Instance Method Summary collapse

Instance Method Details

#alone_in_adjacent_of_type?Boolean

Are both the next and previous slices different types?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


47
48
49
# File 'lib/slices/position_helper.rb', line 47

def last_in_container?
  self == peers.last
end

#next_sliceSlice

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

Returns:



29
30
31
# File 'lib/slices/position_helper.rb', line 29

def next_slice
  peers[peers.index(self) + 1]
end

#peersArray

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

Returns:

  • (Array)


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_typeInteger

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

Returns:

  • (Integer)


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_containerInteger

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

Returns:

  • (Integer)


74
75
76
# File 'lib/slices/position_helper.rb', line 74

def position_in_container
  peers.index(self) + 1
end

#previous_sliceSlice

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

Returns:



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