Module: MaglevRecord::MigrationOperations::ClassMethods

Included in:
NullClass
Defined in:
lib/maglev_record/migration/operations.rb

Instance Method Summary collapse

Instance Method Details

#change_superclass_to(new_superclass) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/maglev_record/migration/operations.rb', line 91

def change_superclass_to(new_superclass)
  # remove the superclass to enable to change the superclass
  return unless new_superclass.is_able_to_become_superclass_of(self)
  _name = name
  Maglev.persistent do
    maglev_redefine {
      new_class = Object.module_eval "
        class #{_name} < #{new_superclass.name}
          self
        end"
      if respond_to? :object_pool_key
        # where should I put it else?
        raise "self should be object_pool_key" unless self == object_pool_key
        pool_pool = Maglev::PERSISTENT_ROOT[MaglevRecord::PERSISTENT_ROOT_KEY]
        unless pool_pool.nil?
          #TODO: test no instances while superclass change
          old_pool = pool_pool.delete(self) # TODO: test removed
          pool_pool[new_class] = old_pool unless old_pool.nil?
        end
      end
    }
  end
end

#delete_attribute(name) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/maglev_record/migration/operations.rb', line 33

def delete_attribute(name)
  # TODO: test attribute names for string and for symbol
  each { |model|
    value = model.attributes.delete(name)
    yield value if block_given?
  }
  attributes.delete name.to_s if respond_to? :attributes
end

#delete_instance_variable(name) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/maglev_record/migration/operations.rb', line 25

def delete_instance_variable(name)
  each { |model|
    value = model.instance_variable_get(name)
    yield value if block_given?
    model.remove_instance_variable name.to_s
  }
end

#migration_deleteObject



57
58
59
60
61
62
# File 'lib/maglev_record/migration/operations.rb', line 57

def migration_delete
  Maglev.persistent do
    nesting_list[-2].remove_const(nesting_name_list[-1])
  end
  delete_object_pool
end

#migration_rename_to(new_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/maglev_record/migration/operations.rb', line 42

def migration_rename_to(new_name)
  old_name = name
  old_class = self
  nested_class = old_class.nesting_list[-2]
  Maglev.persistent do
    cls = nested_class.remove_const nesting_name_list[-1]
  end
  old_class.instance_eval "
    def name
      '#{(nesting_name_list[0...-1] + [new_name.to_s]).join('::')}'
    end
  "
  nested_class.const_set new_name, old_class
end

#nesting_listObject



68
69
70
71
72
73
74
75
# File 'lib/maglev_record/migration/operations.rb', line 68

def nesting_list
  names = nesting_name_list
  list = [Object]
  names.each { |name|
    list << list[-1].const_get(name)
  }
  list
end

#nesting_name_listObject



64
65
66
# File 'lib/maglev_record/migration/operations.rb', line 64

def nesting_name_list
   self.name.split('::')
end

#remove_class_method(name) ⇒ Object



84
85
86
87
88
89
# File 'lib/maglev_record/migration/operations.rb', line 84

def remove_class_method(name)
  begin
    singleton_class.remove_method name
  rescue NameError
  end
end

#remove_instance_method(name) ⇒ Object



77
78
79
80
81
82
# File 'lib/maglev_record/migration/operations.rb', line 77

def remove_instance_method(name)
  begin
    remove_method name
  rescue NameError
  end
end

#rename_attribute(old_name, new_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/maglev_record/migration/operations.rb', line 14

def rename_attribute(old_name, new_name)
  # TODO: test wether attribute is removed from attributes of class
  attr_accessor new_name
  each { |model|
    value = model.attributes[old_name]
    value = yield value if block_given?
    model.attributes.delete old_name
    model.attributes[new_name] = value
  }
end

#rename_instance_variable(old_name, new_name) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/maglev_record/migration/operations.rb', line 5

def rename_instance_variable(old_name, new_name)
  each { |model|
    value = model.instance_variable_get(old_name)
    value = yield value if block_given?
    model.remove_instance_variable old_name.to_s
    model.instance_variable_set(new_name, value)
  }
end