Class: MaglevRecord::ClassChange

Inherits:
Object
  • Object
show all
Defined in:
lib/maglev_record/snapshot/class_change.rb

Overview

Change of a class between two class snapshots

Instance Method Summary collapse

Constructor Details

#initialize(old, new) ⇒ ClassChange

Returns a new instance of ClassChange.



7
8
9
10
# File 'lib/maglev_record/snapshot/class_change.rb', line 7

def initialize(old, new)
  @old = old
  @new = new
end

Instance Method Details

#changed_classObject



24
25
26
# File 'lib/maglev_record/snapshot/class_change.rb', line 24

def changed_class
  @old.snapshot_class
end

#class_nameObject



20
21
22
# File 'lib/maglev_record/snapshot/class_change.rb', line 20

def class_name
  @old.class_name
end

#migration_string_listObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/maglev_record/snapshot/class_change.rb', line 28

def migration_string_list
  if removed_attributes.size == 1 and new_attributes.size == 1
    from_attr = removed_attributes.first
    to_attr = new_attributes.first
    ["#{class_name}.rename_attribute(:#{from_attr}, :#{to_attr})"]
  else
    removed_attributes.map{ |attr|
      "#{class_name}.delete_attribute(:#{attr})"
    } + new_attributes.map{ |attr|
      "#new accessor :#{attr} of #{class_name}"
    }
  end + new_class_methods.map{ |cm|
      "#new class method: #{class_name}.#{cm.to_s}"
    } + new_instance_methods.map{ |im|
      "#new instance method: #{class_name}.new.#{im.to_s}"
    } + removed_class_methods.map{ |cm|
      "#{class_name}.remove_class_method :#{cm}"
    } + removed_instance_methods.map{ |im|
      "#{class_name}.remove_instance_method :#{im}"
    }
end

#new_attributesObject



12
13
14
# File 'lib/maglev_record/snapshot/class_change.rb', line 12

def new_attributes
  (@new.attributes - @old.attributes).sort
end

#new_class_methodsObject



58
59
60
# File 'lib/maglev_record/snapshot/class_change.rb', line 58

def new_class_methods
  (@new.class_methods - @old.class_methods).sort
end

#new_instance_methodsObject



50
51
52
# File 'lib/maglev_record/snapshot/class_change.rb', line 50

def new_instance_methods
  (@new.instance_methods - @old.instance_methods).sort
end

#removed_attributesObject



16
17
18
# File 'lib/maglev_record/snapshot/class_change.rb', line 16

def removed_attributes
  (@old.attributes - @new.attributes).sort
end

#removed_class_methodsObject



62
63
64
# File 'lib/maglev_record/snapshot/class_change.rb', line 62

def removed_class_methods
  (@old.class_methods - @new.class_methods).sort
end

#removed_instance_methodsObject



54
55
56
# File 'lib/maglev_record/snapshot/class_change.rb', line 54

def removed_instance_methods
  (@old.instance_methods - @new.instance_methods).sort
end