Class: RecordStore::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/record_store/changeset.rb

Defined Under Namespace

Classes: Change

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_records: [], desired_records: [], provider:, zone:) ⇒ Changeset

Returns a new instance of Changeset.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/record_store/changeset.rb', line 57

def initialize(current_records: [], desired_records: [], provider:, zone:)
  @current_records = Set.new(normalize_records(current_records))
  @desired_records = Set.new(normalize_records(desired_records))
  @provider = provider
  @zone = zone

  @additions = []
  @removals = []
  @updates = []

  build_changeset
end

Instance Attribute Details

#additionsObject (readonly)

Returns the value of attribute additions.



39
40
41
# File 'lib/record_store/changeset.rb', line 39

def additions
  @additions
end

#current_recordsObject (readonly)

Returns the value of attribute current_records.



39
40
41
# File 'lib/record_store/changeset.rb', line 39

def current_records
  @current_records
end

#desired_recordsObject (readonly)

Returns the value of attribute desired_records.



39
40
41
# File 'lib/record_store/changeset.rb', line 39

def desired_records
  @desired_records
end

#providerObject (readonly)

Returns the value of attribute provider.



39
40
41
# File 'lib/record_store/changeset.rb', line 39

def provider
  @provider
end

#removalsObject (readonly)

Returns the value of attribute removals.



39
40
41
# File 'lib/record_store/changeset.rb', line 39

def removals
  @removals
end

#updatesObject (readonly)

Returns the value of attribute updates.



39
40
41
# File 'lib/record_store/changeset.rb', line 39

def updates
  @updates
end

#zoneObject (readonly)

Returns the value of attribute zone.



39
40
41
# File 'lib/record_store/changeset.rb', line 39

def zone
  @zone
end

Class Method Details

.build_from(provider:, zone:, all: false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/record_store/changeset.rb', line 42

def build_from(provider:, zone:, all: false)
  current_zone = provider.build_zone(zone_name: zone.unrooted_name, config: zone.config)

  current_records = all ? current_zone.all : current_zone.records
  desired_records = all ? zone.all : zone.records

  new(
    current_records: current_records,
    desired_records: desired_records,
    provider: provider,
    zone: zone.unrooted_name,
  )
end

Instance Method Details

#applyObject



70
71
72
73
74
75
76
# File 'lib/record_store/changeset.rb', line 70

def apply
  puts "Applying #{additions.size} additions, #{removals.size} removals, and #{updates.size} updates..."

  provider.apply_changeset(self)

  puts "Published #{zone} changes to #{provider}\n\n"
end

#changesObject



82
83
84
# File 'lib/record_store/changeset.rb', line 82

def changes
  updates.to_a + removals.to_a + additions.to_a
end

#empty?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/record_store/changeset.rb', line 86

def empty?
  changes.empty?
end

#unchangedObject



78
79
80
# File 'lib/record_store/changeset.rb', line 78

def unchanged
  current_records & desired_records
end