Class: Rewrite::ByExample::ObjectToMatcher

Inherits:
Object
  • Object
show all
Includes:
Returning
Defined in:
lib/rewrite/by_example/object_to_matcher.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Returning

included

Constructor Details

#initialize(*bind_arguments) ⇒ ObjectToMatcher

Returns a new instance of ObjectToMatcher.



73
74
75
76
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 73

def initialize (*bind_arguments)
  @bind_arguments = bind_arguments
  @binders = bind_arguments.map { |arg| bind_arg_to_binder(arg) }
end

Class Attribute Details

.debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

Instance Attribute Details

#bind_argumentsObject (readonly)

Returns the value of attribute bind_arguments.



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

def bind_arguments
  @bind_arguments
end

#bindersObject (readonly)

Returns the value of attribute binders.



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

def binders
  @binders
end

Class Method Details

.binding(*bind_arguments) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 23

def self.binding (*bind_arguments)
  if bind_arguments.empty?
    @base_object_to_sequence ||= self.new()
  else
    self.new(*bind_arguments)
  end
end

.from_example(&proc) ⇒ Object



90
91
92
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 90

def self.from_example(&proc)
  (@o2s_from_object ||= self.new).from_example(&proc)
end

.from_object(o) ⇒ Object



94
95
96
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 94

def self.from_object o
  (@o2s_from_object ||= self.new).from_object(o)
end

.from_sexp(*elements) ⇒ Object



98
99
100
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 98

def self.from_sexp(*elements)
  from_object(elements)
end

.noisilyObject



31
32
33
34
35
36
37
38
39
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 31

def self.noisily
  was = self.debug
  begin
    self.debug = true
    yield
  ensure
    self.debug = was
  end
end

.proc_capturerObject



67
68
69
70
71
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 67

def self.proc_capturer
  @@proc_capturer ||= self.from_object(
    s(:proc, nil,  Bind.new(:sexp, AnyEntity.new))
  )
end

.quietlyObject



41
42
43
44
45
46
47
48
49
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 41

def self.quietly
  was = self.debug
  begin
    self.debug = false
    yield
  ensure
    self.debug = was
  end
end

.symbol_like_expression_matcherObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 51

def self.symbol_like_expression_matcher
    @symbol_like_expression_matcher ||= 
      returning(
        quietly do
          self.from_object(
            s(
              UnionOfEntitiesSequence.new(:gvar, :dvar, :vcall, :lcall, :lit),
              Bind.new(:variable_symbol, AnyEntity.new)
            )
          ) 
        end
      ) do |slem|
        p slem.to_s if self.debug
      end
end

Instance Method Details

#from_example(&proc) ⇒ Object



78
79
80
81
82
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 78

def from_example(&proc)
  if unfolded = self.class.proc_capturer.unfold(proc.to_sexp)
    self.from_object(unfolded[:sexp])
  end
end

#from_object(o) ⇒ Object



84
85
86
87
88
# File 'lib/rewrite/by_example/object_to_matcher.rb', line 84

def from_object o
  matcher = convert(o)
  raise "#{o.to_s} => #{matcher.to_s} does not describe a matcher" unless matcher.kind_of? EntityMatcher
  matcher
end