Class: Mxrb::Model::DesignMigrationPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/model/design_migration.rb

Overview

Immutable preview of literal-to-token stylesheet replacements.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, changes) ⇒ DesignMigrationPlan

Returns a new instance of DesignMigrationPlan.



38
39
40
41
42
43
44
# File 'lib/mxrb/model/design_migration.rb', line 38

def initialize(root, changes)
  @root = File.expand_path(root)
  @changes = changes.freeze
  @digests = changes.to_h { [_1.path, Digest::SHA256.hexdigest(_1.before)] }.freeze
  @after_digests = changes.to_h { [_1.path, Digest::SHA256.hexdigest(_1.after)] }.freeze
  @applied = false
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



12
13
14
# File 'lib/mxrb/model/design_migration.rb', line 12

def changes
  @changes
end

Class Method Details

.build(root, replacements) ⇒ Object



14
15
16
17
18
# File 'lib/mxrb/model/design_migration.rb', line 14

def self.build(root, replacements)
  pairs = replacements.to_h.transform_keys(&:to_s).transform_values(&:to_s)
  changes = stylesheet_paths(root).filter_map { migration_change(root, _1, pairs) }
  new(root, changes)
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


47
# File 'lib/mxrb/model/design_migration.rb', line 47

def applied? = @applied

#apply!Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
# File 'lib/mxrb/model/design_migration.rb', line 49

def apply!
  raise ArgumentError, 'design migration plan was already applied' if @applied

  verify_changes!(@digests, 'changed after preview')
  replace_all!(fallback: :before, &:after)
  @applied = true
  self
end

#empty?Boolean

Returns:

  • (Boolean)


46
# File 'lib/mxrb/model/design_migration.rb', line 46

def empty? = @changes.empty?

#rollback!Object

Restores the previewed contents when a surrounding batch fails.



59
60
61
62
63
64
65
66
# File 'lib/mxrb/model/design_migration.rb', line 59

def rollback!
  return self unless @applied

  verify_changes!(@after_digests, 'changed after migration')
  replace_all!(fallback: :after, &:before)
  @applied = false
  self
end