Class: Rewrite::ByExample::UnionOfEntitiesSequence

Inherits:
Sequence
  • Object
show all
Defined in:
lib/rewrite/by_example/union_of_entities_sequence.rb

Overview

Acts like a union of sequences but is initialized with entities. Could be better!

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Sequence

#fold

Constructor Details

#initialize(*matchers) ⇒ UnionOfEntitiesSequence



16
17
18
19
20
21
# File 'lib/rewrite/by_example/union_of_entities_sequence.rb', line 16

def initialize(*matchers)
  self.matchers = matchers.map { |matcher| 
    LengthOne.new(ObjectToMatcher.from_object(matcher))
  }
  @length_range = @matchers.inject(0..0) { |range, matcher| range_union(range, matcher.length_range) }
end

Instance Attribute Details

#length_rangeObject

Returns the value of attribute length_range.



14
15
16
# File 'lib/rewrite/by_example/union_of_entities_sequence.rb', line 14

def length_range
  @length_range
end

#matchersObject

Returns the value of attribute matchers.



14
15
16
# File 'lib/rewrite/by_example/union_of_entities_sequence.rb', line 14

def matchers
  @matchers
end

Instance Method Details

#range_union(range1, range2) ⇒ Object



27
28
29
# File 'lib/rewrite/by_example/union_of_entities_sequence.rb', line 27

def range_union(range1, range2)
  ([range1.begin, range2.begin].min)..([range1.end, range2.end].max)
end

#to_sObject



31
32
33
# File 'lib/rewrite/by_example/union_of_entities_sequence.rb', line 31

def to_s
  "(#{matchers.map { |m| m.to_s }.join(' | ')})"
end

#unfolders_by_length(length) ⇒ Object



23
24
25
# File 'lib/rewrite/by_example/union_of_entities_sequence.rb', line 23

def unfolders_by_length(length)
  self.matchers.inject([]) { |unfolders, matcher| unfolders + matcher.unfolders_by_length(length) }
end