Class: Opencdd::EntityDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/opencdd/entity_diff.rb

Overview

Compute a structured diff between two entities of the same IRDI.

Pure value object: doesn't mutate its inputs, doesn't reach beyond entity.properties. Iterates the union of keys and categorizes each into added, removed, or changed.

Multilingual fields (MDC_P001.en, MDC_P001.fr) are diffed as a group keyed by their base property ID. A change in any language counts as one change to the base field.

Example:

diff = Opencdd::EntityDiff.between(v001, v002)
diff.added      # => ["MDC_P999"]
diff.removed    # => []
diff.changed    # => [{ field: "preferred_name", from: "X", to: "Y" }]
diff.empty?     # => false

Defined Under Namespace

Classes: Change

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_entity, to_entity) ⇒ EntityDiff

Returns a new instance of EntityDiff.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
# File 'lib/opencdd/entity_diff.rb', line 29

def initialize(from_entity, to_entity)
  raise ArgumentError, "EntityDiff requires same IRDI (#{from_entity.irdi} vs #{to_entity.irdi})" unless from_entity.irdi == to_entity.irdi
  @from_entity = from_entity
  @to_entity   = to_entity
  compute_diff
end

Instance Attribute Details

#addedObject (readonly)

Returns the value of attribute added.



23
24
25
# File 'lib/opencdd/entity_diff.rb', line 23

def added
  @added
end

#changedObject (readonly)

Returns the value of attribute changed.



23
24
25
# File 'lib/opencdd/entity_diff.rb', line 23

def changed
  @changed
end

#from_entityObject (readonly)

Returns the value of attribute from_entity.



23
24
25
# File 'lib/opencdd/entity_diff.rb', line 23

def from_entity
  @from_entity
end

#removedObject (readonly)

Returns the value of attribute removed.



23
24
25
# File 'lib/opencdd/entity_diff.rb', line 23

def removed
  @removed
end

#to_entityObject (readonly)

Returns the value of attribute to_entity.



23
24
25
# File 'lib/opencdd/entity_diff.rb', line 23

def to_entity
  @to_entity
end

Class Method Details

.between(from_entity, to_entity) ⇒ Object



25
26
27
# File 'lib/opencdd/entity_diff.rb', line 25

def self.between(from_entity, to_entity)
  new(from_entity, to_entity)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/opencdd/entity_diff.rb', line 36

def empty?
  added.empty? && removed.empty? && changed.empty?
end

#sizeObject

Total change count (added + removed + changed).



41
42
43
# File 'lib/opencdd/entity_diff.rb', line 41

def size
  added.size + removed.size + changed.size
end

#to_hObject

Wire-format summary suitable for JSON emit or browser diff view.



46
47
48
49
50
51
52
53
# File 'lib/opencdd/entity_diff.rb', line 46

def to_h
  {
    irdi: from_entity.irdi.to_s,
    added: added,
    removed: removed,
    changed: changed.map(&:to_h),
  }
end