Class: Rewrite::ByExample::LengthOne

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

Overview

Natches a sequence containing one item. Wraps an entity matcher, with the wrapped matcher matching the one item.

Confusing? Consider SymbolEntity.new(:foo). It matches a single entity, :foo. Whereas LengthOne.new(SymbolEntity.new(:foo)) matches a sequence of length one where that one thing happens to match :foo.

This distinction is important because sequences can only consist of collections of other sequences. So the LengthOnes are leaves of the sequence trees.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_matcher) ⇒ LengthOne

Returns a new instance of LengthOne.



21
22
23
24
# File 'lib/rewrite/by_example/length_one.rb', line 21

def initialize(entity_matcher)
  @matcher = entity_matcher
  @lambda = lambda { |arr| entity_matcher.unfold(arr.first) }
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



19
20
21
# File 'lib/rewrite/by_example/length_one.rb', line 19

def matcher
  @matcher
end

Instance Method Details

#fold(enum_of_bindings) ⇒ Object



42
43
44
# File 'lib/rewrite/by_example/length_one.rb', line 42

def fold(enum_of_bindings)
  matcher.fold(enum_of_bindings)
end

#length_rangeObject



34
35
36
# File 'lib/rewrite/by_example/length_one.rb', line 34

def length_range
  1..1
end

#to_sObject



38
39
40
# File 'lib/rewrite/by_example/length_one.rb', line 38

def to_s
  "<#{matcher.to_s}>"
end

#unfolders_by_length(length) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rewrite/by_example/length_one.rb', line 26

def unfolders_by_length(length)
  if length == 1
    [ @lambda ]
  else
    []
  end
end