Module: LLT::Helpers::Positions

Defined in:
lib/llt/helpers/positions.rb

Instance Method Summary collapse

Instance Method Details

#-(other) ⇒ Object

used by Rating



32
33
34
# File 'lib/llt/helpers/positions.rb', line 32

def - other   # used by Rating
  @position - other.position
end

#<(other) ⇒ Object



12
13
14
# File 'lib/llt/helpers/positions.rb', line 12

def < other
  @position < other.position
end

#<=(other) ⇒ Object



24
25
26
# File 'lib/llt/helpers/positions.rb', line 24

def <= other
  @position >= other.position
end

#<=>(other) ⇒ Object



28
29
30
# File 'lib/llt/helpers/positions.rb', line 28

def <=> other
  @position <=> other.position
end

#===(other) ⇒ Object

Adds Comparable-like methods and more to an object, that holds an instance variable @position While the Comparable module itself could be included, defining these methods manually comes with a significant performance gain.



8
9
10
# File 'lib/llt/helpers/positions.rb', line 8

def === other
  @position == other.position
end

#>(other) ⇒ Object



16
17
18
# File 'lib/llt/helpers/positions.rb', line 16

def > other
  @position > other.position
end

#>=(other) ⇒ Object



20
21
22
# File 'lib/llt/helpers/positions.rb', line 20

def >= other
  @position >= other.position
end

#at_sentence_start(sos) ⇒ Object



61
62
63
# File 'lib/llt/helpers/positions.rb', line 61

def at_sentence_start(sos)
  @position == sos || @position == sos + 1
end

#behind?(other, steps = 1) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/llt/helpers/positions.rb', line 73

def behind?(other, steps = 1)
  (self - other).between?(1, steps + 1)   # (self - other) < steps + 1 not valid => true for negatives!
end

#behind_any?(arr, steps = 1) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/llt/helpers/positions.rb', line 69

def behind_any?(arr, steps = 1)
  arr.any? { |e| self.behind?(e, steps) }
end

#between?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/llt/helpers/positions.rb', line 81

def between? a, b
  @position.between?(a.position, b.position)
end

#close_to?(other, steps = 1) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/llt/helpers/positions.rb', line 77

def close_to?(other, steps = 1)
  (self - other).abs < steps + 1
end

#distance_to(other, absolute = false) ⇒ Object



40
41
42
43
# File 'lib/llt/helpers/positions.rb', line 40

def distance_to(other, absolute = false)
  d = other - self
  absolute ? d.abs : d
end

#in_front_of?(other, steps = 1) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/llt/helpers/positions.rb', line 49

def in_front_of?(other, steps = 1)
  (other - self).between?(1, steps + 1)
end

#in_front_of_any?(arr, steps = 1) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/llt/helpers/positions.rb', line 65

def in_front_of_any?(arr, steps = 1)
  arr.any? { |e| self.in_front_of?(e, steps) }
end

#nearest_in(arr) ⇒ Object



45
46
47
# File 'lib/llt/helpers/positions.rb', line 45

def nearest_in(arr)
  arr.sort_by { |el| distance_to(el, true) }.first
end

#next_in(arr, &blk) ⇒ Object



53
54
55
# File 'lib/llt/helpers/positions.rb', line 53

def next_in(arr, &blk)
  arr.next_elem(self, &blk)
end

#prev_in(arr, &blk) ⇒ Object



57
58
59
# File 'lib/llt/helpers/positions.rb', line 57

def prev_in(arr, &blk)
  arr.prev_elem(self, &blk)
end

#range_with(other, to_range = false) ⇒ Object



85
86
87
88
89
90
# File 'lib/llt/helpers/positions.rb', line 85

def range_with(other, to_range = false)
  a, b = [@position, other.position].sort

  range = ((a + 1)...b)
  to_range ? range : range.to_a
end

#surroundingObject



36
37
38
# File 'lib/llt/helpers/positions.rb', line 36

def surrounding
  [@position - 1, @position + 1]
end