Commits

Anonymous committed ae7100e

introduce_factory: added resources parameter

  • Participants
  • Parent commits 32ff88b

Comments (0)

Files changed (3)

File docs/done.txt

 ===========
 
 
-- Choosing which files to apply a encapsulate_field on : January 22, 2008
-
-
-- Choosing which files to apply a change_signature on : January 22, 2008
-
-
-- Choosing which files to search in find_occurrences : January 22, 2008
-
-
-- Choosing which files to apply a rename on : January 22, 2008
+- Adding resources parameter to some refactorings : January 22, 2008
 
 
 - Deprecated in_file argument of `Rename.get_changes()` : January 22, 2008

File rope/refactor/introduce_factory.py

         self.pymodule = self.old_pyname.get_object().get_module()
         self.resource = self.pymodule.get_resource()
 
-    def get_changes(self, factory_name, global_factory=False,
+    def get_changes(self, factory_name, global_factory=False, resources=None,
                     task_handle=taskhandle.NullTaskHandle()):
+        if resources is None:
+            resources = self.pycore.get_python_files()
         changes = ChangeSet('Introduce factory method <%s>' % factory_name)
-        job_set = task_handle.create_jobset(
-            'Collecting Changes', len(self.pycore.get_python_files()))
-        self._change_module(changes, factory_name, global_factory, job_set)
+        job_set = task_handle.create_jobset('Collecting Changes',
+                                            len(resources))
+        self._change_module(resources, changes, factory_name,
+                            global_factory, job_set)
         return changes
 
-    def _change_module(self, changes, factory_name, global_, job_set):
+    def _change_module(self, resources, changes,
+                       factory_name, global_, job_set):
         import_tools = rope.refactor.importutils.ImportTools(self.pycore)
         new_import = import_tools.get_import(self.resource)
         if global_:
         else:
             replacement = self._new_function_name(factory_name, global_)
 
-        for file_ in self.pycore.get_python_files():
+        for file_ in resources:
             if file_ == self.resource:
                 job_set.started_job('Changing definition')
                 self._change_resource(changes, factory_name, global_)

File ropetest/refactor/__init__.py

         testutils.remove_project(self.project)
         super(IntroduceFactoryTest, self).tearDown()
 
-    def _introduce_factory(self, resource, offset, factory_name,
-                                   global_factory=False):
+    def _introduce_factory(self, resource, offset, *args, **kwds):
         factory_introducer = IntroduceFactory(self.project,
-                                                         resource, offset)
-        changes = factory_introducer.get_changes(factory_name, global_factory)
+                                              resource, offset)
+        changes = factory_introducer.get_changes(*args, **kwds)
         self.project.do(changes)
 
     def test_adding_the_method(self):
         self.assertEquals('import pkg.mod2\nfrom pkg.mod2 import AClass\n',
                           new_init.read())
 
+    def test_resources_parameter(self):
+        code = 'class A(object):\n    an_attr = 10\n'
+        code1 = 'import mod\na = mod.A()\n'
+        mod = testutils.create_module(self.project, 'mod')
+        mod1 = testutils.create_module(self.project, 'mod1')
+        mod.write(code)
+        mod1.write(code1)
+        expected = 'class A(object):\n    an_attr = 10\n\n' \
+                   '    @staticmethod\n    def create(*args, **kwds):\n' \
+                   '        return A(*args, **kwds)\n'
+        self._introduce_factory(mod, mod.read().index('A') + 1,
+                                'create', resources=[mod])
+        self.assertEquals(expected, mod.read())
+        self.assertEquals(code1, mod1.read())
+
 
 class EncapsulateFieldTest(unittest.TestCase):