Class: Iron::Schema::Diff

Inherits:
Object
  • Object
show all
Defined in:
app/models/iron/schema/diff.rb

Constant Summary collapse

PRUNE_HINT =
"[in database, not in file — PRUNE=1 deletes]"
SET_ATTRIBUTES =
%i[supported_block_definitions supported_content_types].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Diff

Returns a new instance of Diff.



8
9
10
11
12
# File 'app/models/iron/schema/diff.rb', line 8

def initialize(schema)
  @schema = schema
  @notes = []
  snapshot
end

Instance Attribute Details

#notesObject (readonly)

Returns the value of attribute notes.



6
7
8
# File 'app/models/iron/schema/diff.rb', line 6

def notes
  @notes
end

Instance Method Details

#add_note(note) ⇒ Object



44
45
46
# File 'app/models/iron/schema/diff.rb', line 44

def add_note(note)
  notes << note
end

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/iron/schema/diff.rb', line 23

def empty?
  sections.all? { |section| section.values.all?(&:empty?) }
end

#removalsObject



34
35
36
37
38
39
40
41
42
# File 'app/models/iron/schema/diff.rb', line 34

def removals
  {
    locales: @locales[:remove],
    block_definitions: @block_definitions[:remove],
    content_types: @content_types[:remove],
    block_definition_fields: field_removals(@block_definitions),
    content_type_fields: field_removals(@content_types)
  }
end

#size(include_removals: true) ⇒ Object

Removals are only applied in prune mode, so the apply task excludes them from its applied-changes count while still listing them as candidates.



29
30
31
32
# File 'app/models/iron/schema/diff.rb', line 29

def size(include_removals: true)
  buckets = include_removals ? %i[add update remove] : %i[add update]
  sections.sum { |section| section.values_at(*buckets).sum(&:size) }
end

#to_hObject



14
15
16
17
18
19
20
21
# File 'app/models/iron/schema/diff.rb', line 14

def to_h
  {
    locales: @locales,
    block_definitions: @block_definitions,
    content_types: @content_types,
    notes: notes
  }
end

#to_sObject



48
49
50
51
52
53
54
55
56
57
# File 'app/models/iron/schema/diff.rb', line 48

def to_s
  return "Schema is in sync." if empty?

  lines = []
  lines.concat(section_lines("locales", @locales))
  lines.concat(section_lines("block_definitions", @block_definitions))
  lines.concat(section_lines("content_types", @content_types))
  lines.concat(notes.map { |note| "note: #{note}" })
  lines.join("\n")
end