Class: Rewrite::ByExample::Bind

Inherits:
EntityMatcher show all
Defined in:
lib/rewrite/by_example/bind.rb

Overview

Abstract class that matches any entity and optionally binds that entity’s value to a name in the unfolded result. Useful for creating classes that bind and match simultaneously

Instance Attribute Summary collapse

Attributes inherited from EntityMatcher

#predicate

Instance Method Summary collapse

Methods inherited from EntityMatcher

#after_unfold, #such_that!

Constructor Details

#initialize(name, entity_matcher) ⇒ Bind

Returns a new instance of Bind.



16
17
18
19
# File 'lib/rewrite/by_example/bind.rb', line 16

def initialize(name, entity_matcher)
  self.name = name.to_sym
  self.entity_matcher = entity_matcher
end

Instance Attribute Details

#entity_matcherObject

Returns the value of attribute entity_matcher.



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

def entity_matcher
  @entity_matcher
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#fold(enum_of_bindings) ⇒ Object



25
26
27
# File 'lib/rewrite/by_example/bind.rb', line 25

def fold (enum_of_bindings)
  enum_of_bindings[self.name]
end

#to_sObject



29
30
31
32
33
34
35
# File 'lib/rewrite/by_example/bind.rb', line 29

def to_s
  if name
    "#{entity_matcher.to_s} => #{name.inspect}"
  else
    '*'
  end
end

#unfold(sexp) ⇒ Object



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

def unfold (sexp)
  { self.name => sexp } if entity_matcher.unfold(sexp)
end