Class: Rewrite::ByExample::BindSequence

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

Overview

Matches any sequence of zero or more entities and binds the list to a name in the unfolded result. BindSequence.new(‘name’) binds whatever sequence of entities it matches to ‘name’.

Not isomorphic to AnyEntity: AnyEntity matches one entity, BindSequence matches a sequence of entities. If you want to match a single entity that happens to consist

LengthOne.new(Bind.new('name), AnyEntity.new)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ BindSequence

Returns a new instance of BindSequence.



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

def initialize(name)
  @name = name.to_sym
  @unfolder_lambda = lambda { |arr| { @name => s(*arr) } }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#unfolder_lambdaObject (readonly)

Returns the value of attribute unfolder_lambda.



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

def unfolder_lambda
  @unfolder_lambda
end

Instance Method Details

#fold(enum_of_bindings) ⇒ Object



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

def fold(enum_of_bindings)
  enum_of_bindings[name]
end

#length_rangeObject



30
31
32
# File 'lib/rewrite/by_example/bind_sequence.rb', line 30

def length_range
  0..10000
end

#to_sObject



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

def to_s
  "__* => #{name.inspect}"
end

#unfolders_by_length(length) ⇒ Object



26
27
28
# File 'lib/rewrite/by_example/bind_sequence.rb', line 26

def unfolders_by_length(length)
  [ unfolder_lambda ]
end