Class: Gaps
- Inherits:
-
Object
- Object
- Gaps
- Defined in:
- lib/gaps.rb
Overview
This is used for finding the gaps between a subset of elements in an array and the original layout. We use this in Discourse to find gaps between posts.
Note that we will only return a gap as ‘before’ or ‘after’, not both. We only want to display the gap once.
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #find_gaps ⇒ Object
-
#initialize(subset, original) ⇒ Gaps
constructor
A new instance of Gaps.
Constructor Details
#initialize(subset, original) ⇒ Gaps
Returns a new instance of Gaps.
13 14 15 16 17 18 19 20 |
# File 'lib/gaps.rb', line 13 def initialize(subset, original) @before = {} @after = {} @subset = subset @original = original find_gaps end |
Instance Attribute Details
#after ⇒ Object (readonly)
Returns the value of attribute after.
11 12 13 |
# File 'lib/gaps.rb', line 11 def after @after end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
11 12 13 |
# File 'lib/gaps.rb', line 11 def before @before end |
Instance Method Details
#empty? ⇒ Boolean
22 23 24 |
# File 'lib/gaps.rb', line 22 def empty? @before.size == 0 && @after.size == 0 end |
#find_gaps ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gaps.rb', line 26 def find_gaps return if @subset.nil? || @original.nil? i = j = 0 gaps = {} current_gap = [] while e1 = @subset[i] e2 = @original[j] if (e1 == e2) if current_gap.size > 0 @before[e1] = current_gap.dup current_gap = [] end i = i + 1 else current_gap << e2 end j = j + 1 break if (i == @subset.size) || (j == @original.size) end @after[@subset[i - 1]] = @original[j..-1] if j < @original.size end |