Class: ActiveRecord::ModelSpaces::Persistor

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/active_record/model_spaces/persistor.rb

Overview

manages ModelSpace persistence…

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

all_model_superclasses, class_for_classname, class_from_classname, is_active_record_model?, model_from_name, name_from_model, require_for_classname

Constructor Details

#initialize(connection, table_name) ⇒ Persistor

Returns a new instance of Persistor.



13
14
15
16
17
# File 'lib/active_record/model_spaces/persistor.rb', line 13

def initialize(connection, table_name)
  @connection = connection
  @table_name = table_name || "model_spaces_tables"
  create_model_spaces_table(@connection, @table_name)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/active_record/model_spaces/persistor.rb', line 10

def connection
  @connection
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



11
12
13
# File 'lib/active_record/model_spaces/persistor.rb', line 11

def table_name
  @table_name
end

Instance Method Details

#create_model_spaces_table(connection, tn) ⇒ Object

create the model_spaces table if it doesn’t exist



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/model_spaces/persistor.rb', line 56

def create_model_spaces_table(connection, tn)
  if !connection.table_exists?(tn)
    connection.instance_eval do
      create_table(tn) do |t|
        t.string :model_space_name, :null=>false
        t.string :model_space_key, :null=>false
        t.string :model_name, :null=>false
        t.integer :version, :null=>false, :default=>0
      end
      add_index tn, [:model_space_name, :prefix, :model_name], :unique=>true
    end
  end
end

#list_keys(model_space_name) ⇒ Object

list all persisted prefixes for a given model space



20
21
22
# File 'lib/active_record/model_spaces/persistor.rb', line 20

def list_keys(model_space_name)
  connection.select_rows("select model_space_key from #{table_name} where model_space_name='#{model_space_name}'").map{|r| r.first}
end

#read_model_space_model_versions(model_space_name, model_space_key) ⇒ Object

returns a map of => version entries for a given model-space and model_space_key



25
26
27
# File 'lib/active_record/model_spaces/persistor.rb', line 25

def read_model_space_model_versions(model_space_name, model_space_key)
  connection.select_all("select model_name, version from #{table_name} where model_space_name='#{model_space_name}' and model_space_key='#{model_space_key}'").reduce({}){|h,r| h[r["model_name"]] = r["version"].to_i ; h}
end

#update_model_space_model_versions(model_space_name, model_space_key, new_model_versions) ⇒ Object

update



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_record/model_spaces/persistor.rb', line 30

def update_model_space_model_versions(model_space_name, model_space_key, new_model_versions)
  ActiveRecord::Base.transaction do
    old_model_versions = read_model_space_model_versions(model_space_name, model_space_key)

    new_model_versions.map do |model_or_name, new_version|
      model_name = name_from_model(model_or_name)
      old_version = old_model_versions[model_name]

      if old_version && new_version && old_version != new_version && new_version != 0

        connection.execute("update #{table_name} set version=#{new_version} where model_space_name='#{model_space_name}' and model_space_key='#{model_space_key}' and model_name='#{model_name}'")

      elsif !old_version && new_version && new_version != 0

        connection.execute("insert into #{table_name} (model_space_name, model_space_key, model_name, version) values ('#{model_space_name}', '#{model_space_key}', '#{model_name}', #{new_version})")

      elsif old_version && ( !new_version || new_version == 0 )

        connection.execute("delete from #{table_name} where model_space_name='#{model_space_name}' and model_space_key='#{model_space_key}' and model_name='#{model_name}'")
      end
    end
    true
  end
end