Class: StructureProcessor

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/ruby_diff/structure_processor.rb

Overview

StructureProcessor is a SexpProcessor which will generate a logical model of the ruby code. It can be fooled by metaprogramming and method redefinition, but in most cases should be fairly accurate.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = '') ⇒ StructureProcessor

Returns a new instance of StructureProcessor.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_diff/structure_processor.rb', line 108

def initialize(name='')
  super()
  @name = name
  self.strict = false
  self.auto_shift_type = true
  
  @instance_scope = true
  @code_objects = {}
  @root_objects = {}
  @scope_stack = []
  @meta_methods = {
    :attr_accessor => AccessorHandler.new("accessor"),
    :attr_writer   => AccessorHandler.new("writer"),
    :attr_reader   => AccessorHandler.new("reader")
  }
end

Instance Attribute Details

#code_objectsObject

Returns the value of attribute code_objects.



102
103
104
# File 'lib/ruby_diff/structure_processor.rb', line 102

def code_objects
  @code_objects
end

#meta_methodsObject (readonly)

Returns the value of attribute meta_methods.



106
107
108
# File 'lib/ruby_diff/structure_processor.rb', line 106

def meta_methods
  @meta_methods
end

#nameObject (readonly)

Returns the value of attribute name.



101
102
103
# File 'lib/ruby_diff/structure_processor.rb', line 101

def name
  @name
end

#root_objectsObject

Returns the value of attribute root_objects.



103
104
105
# File 'lib/ruby_diff/structure_processor.rb', line 103

def root_objects
  @root_objects
end

#scope_stackObject

Returns the value of attribute scope_stack.



105
106
107
# File 'lib/ruby_diff/structure_processor.rb', line 105

def scope_stack
  @scope_stack
end

Instance Method Details

#diff(other_processor) ⇒ Object



184
185
186
# File 'lib/ruby_diff/structure_processor.rb', line 184

def diff(other_processor)
  method_diff = CodeComparison.new(self.root_objects, other_processor.root_objects).changes
end

#process_class(exp) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/ruby_diff/structure_processor.rb', line 125

def process_class(exp)
  name = exp.shift
  super_class = exp.shift #?
  body = exp.shift
  
  record ClassCode.new(name, self.scope, body) do
    s(:class, name, process(super_class), process(body))
  end
end

#process_defn(exp) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/ruby_diff/structure_processor.rb', line 144

def process_defn(exp)
  name = exp.shift
  body = process exp.shift
  
  record MethodCode.new(name, self.scope, @instance_scope, body) do
    s(:defn, name, body)
  end
end

#process_defs(exp) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/ruby_diff/structure_processor.rb', line 153

def process_defs(exp)
  exp_scope = process exp.shift
  name = exp.shift
  body = process exp.shift
  
  record MethodCode.new(name, self.scope, false, body) do
    s(:defs, exp_scope, name, body)
  end
end

#process_fcall(exp) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ruby_diff/structure_processor.rb', line 172

def process_fcall(exp)
  name = exp.shift
  args = process exp.shift

  if meta_handler = @meta_methods[name]
    meta_codes = meta_handler.meta_codes(args, self.scope)
    meta_codes.each{|m| record(m)}
  end
  
  return s(:fcall, name, args)
end

#process_module(exp) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/ruby_diff/structure_processor.rb', line 135

def process_module(exp)
  name = exp.shift
  body = exp.shift
  
  record ModuleCode.new(name, self.scope, body) do
    s(:class, name, process(body))
  end
end

#process_sclass(exp) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/ruby_diff/structure_processor.rb', line 163

def process_sclass(exp)
  @instance_scope = false
  exp_scope = process exp.shift
  body = process exp.shift
  @instance_scope = true
  
  s(:sclass, exp_scope, body)
end