Class: MaglevRecord::SuperclassMismatchChange

Inherits:
SuperclassMismatchChangeBase show all
Defined in:
lib/maglev_record/snapshot/superclass_mismatch_change.rb

Instance Method Summary collapse

Methods inherited from SuperclassMismatchChangeBase

#changes_since, #nothing_changed?, #superclass_mismatch_class_names, #superclass_mismatch_classes

Constructor Details

#initialize(cls, file_path) ⇒ SuperclassMismatchChange

Returns a new instance of SuperclassMismatchChange.



35
36
37
38
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 35

def initialize(cls, file_path)
  @cls = cls
  @file_path = file_path
end

Instance Method Details

#class_nameObject

methods for the single change



89
90
91
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 89

def class_name
  mismatching_class.name
end

#determine_new_superclassObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 52

def determine_new_superclass
  # 1. replace the actual class by a new one
  # 2. inherit
  # 3. get the superclass
  # 4. restore the class
  constant_name = mismatching_class.name
  class_to_replace = nil # change the scope
  Maglev.persistent do
    begin
      class_to_replace = Object.remove_const constant_name
    rescue NameError
      # negative path 1 TODO: test
      return ClassWithMismatchNotFound.new
    else
      if class_to_replace != mismatching_class
        # negative path 2 TODO: test
        Object.const_set constant_name, class_to_replace
        return ClassWithMismatchNotFound.new
      end
    end
  end
  begin
    Kernel.load file_path
    cls = Object.const_get constant_name
    return cls.superclass
  ensure
    Object.const_set constant_name, class_to_replace
  end
end

#file_pathObject



40
41
42
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 40

def file_path
  @file_path
end

#migration_string(identation = 0) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 44

def migration_string(identation = 0)
  " " * identation + [
    "# TypeError: superclass mismatch for #{class_name}",
    "# in #{file_path}",
    "#{class_name}.change_superclass_to #{new_superclass.name}"
  ].join("\n" + " " * identation)
end

#mismatching_classObject



93
94
95
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 93

def mismatching_class
  @cls
end

#new_superclassObject



82
83
84
85
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 82

def new_superclass
  @new_super_class = determine_new_superclass if @new_super_class.nil?
  @new_super_class
end

#reversedObject

reverse



99
100
101
# File 'lib/maglev_record/snapshot/superclass_mismatch_change.rb', line 99

def reversed
  ReversedSuperclassMismatchChange.new(self)
end