Anonymous avatar Anonymous committed 779f612

encapsulate_field: added resources parameter

Comments (0)

Files changed (3)

 ===========
 
 
+- Choosing which files to apply a encapsulate_field on : January 22, 2008
+
+
 - Choosing which files to apply a change_signature on : January 22, 2008
 
 

rope/refactor/encapsulate_field.py

                 'Encapsulate field should be performed on class attributes.')
         self.resource = self.pyname.get_definition_location()[0].get_resource()
 
-    def get_changes(self, getter=None, setter=None,
+    def get_changes(self, getter=None, setter=None, resources=None,
                     task_handle=taskhandle.NullTaskHandle()):
         """Get the changes this refactoring makes
 
-        If getter is not None, that will be the name of the getter,
-        otherwise get_${field_name} will be used.  The same is true
-        for setter and if it is None set_${field_name} is used.
+        If `getter` is not `None`, that will be the name of the
+        getter, otherwise ``get_${field_name}`` will be used.  The
+        same is true for `setter` and if it is None set_${field_name} is
+        used.
+
+        `resources` can be a list of `rope.base.resource.File`\s that
+        the refactoring should be applied on; if `None` all python
+        files in the project are searched.
 
         """
+        if resources is None:
+            resources = self.pycore.get_python_files()
         changes = ChangeSet('Encapsulate field <%s>' % self.name)
-        job_set = task_handle.create_jobset(
-            'Collecting Changes', len(self.pycore.get_python_files()))
+        job_set = task_handle.create_jobset('Collecting Changes',
+                                            len(resources))
         if getter is None:
             getter = 'get_' + self.name
         if setter is None:
             setter = 'set_' + self.name
         renamer = GetterSetterRenameInModule(
             self.pycore, self.name, [self.pyname], getter, setter)
-        for file in self.pycore.get_python_files():
+        for file in resources:
             job_set.started_job('Working on <%s>' % file.path)
             if file == self.resource:
                 result = self._change_holding_module(changes, renamer,

ropetest/refactor/__init__.py

         expected = self.a_class + new_methods
         self.assertEquals(expected, self.mod.read())
 
+    def test_using_resources_parameter(self):
+        self.mod1.write('import mod\na = mod.A()\nvar = a.attr\n')
+        self.mod.write(self.a_class)
+        self._encapsulate(self.mod, self.mod.read().index('attr') + 1,
+                          resources=[self.mod])
+        self.assertEquals('import mod\na = mod.A()\nvar = a.attr\n',
+                          self.mod1.read())
+        expected = self.a_class + self.added_methods
+        self.assertEquals(expected, self.mod.read())
+
 
 class LocalToFieldTest(unittest.TestCase):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.