Class: ViewModel::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/migrator.rb

Direct Known Subclasses

DownMigrator, UpMigrator

Constant Summary collapse

EXCLUDE_FROM_MIGRATION =
'_exclude_from_migration'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(required_versions) ⇒ Migrator

Returns a new instance of Migrator.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/view_model/migrator.rb', line 26

def initialize(required_versions)
  @paths = required_versions.each_with_object({}) do |(viewmodel_class, required_version), h|
    if required_version != viewmodel_class.schema_version
      path = viewmodel_class.migration_path(from: required_version, to: viewmodel_class.schema_version)
      h[viewmodel_class.view_name] = path
    end
  end

  @versions = required_versions.each_with_object({}) do |(viewmodel_class, required_version), h|
    h[viewmodel_class.view_name] = [required_version, viewmodel_class.schema_version]
  end
end

Class Method Details

.migrated_deep_schema_version(viewmodel_class, required_versions, include_referenced: true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/view_model/migrator.rb', line 8

def migrated_deep_schema_version(viewmodel_class, required_versions, include_referenced: true)
  deep_schema_version = viewmodel_class.deep_schema_version(include_referenced: include_referenced)

  if required_versions.present?
    deep_schema_version = deep_schema_version.dup

    required_versions.each do |required_vm_class, required_version|
      name = required_vm_class.view_name
      if deep_schema_version.has_key?(name)
        deep_schema_version[name] = required_version
      end
    end
  end

  deep_schema_version
end

Instance Method Details

#migrate!(serialization) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/view_model/migrator.rb', line 39

def migrate!(serialization)
  references = (serialization['references'] ||= {})

  # First visit everything except references; there's no issue with adding
  # new references during this.
  migrate_tree!(serialization.except('references'), references: references)

  # While visiting references itself, we need to take care that we can
  # concurrently modify them (e.g. by adding new referenced views).
  # Moreover, such added references must themselves be visited, as they'll
  # be synthesized at the current version and so may need to be migrated
  # down to the client's requested version.
  visited_refs = []
  loop do
    unvisited_refs = references.keys - visited_refs
    break if unvisited_refs.empty?

    unvisited_refs.each do |ref|
      migrate_tree!(references[ref], references: references)
    end

    visited_refs.concat(unvisited_refs)
  end

  GarbageCollection.garbage_collect_references!(serialization)

  if references.empty?
    serialization.delete('references')
  end
end