module Eco::Data::Locations # Differences between node before and after # @note this works with `Hash` object where basic keys are: # - `nodeId` # - `name` # - `parentId` # - `archived` # @note other properties can be part of the `Hash` although # they may not influence the results. # @note that for special `exposed` methods question mark `?` on a # property of the model checks if that property changed # (it has nothing to do with checking the boolean value of that property) class NodeDiff < Eco::Data::Hashes::DiffResult require_relative 'node_diff/accessors' require_relative 'node_diff/nodes_diff' include Eco::Data::Locations::NodeDiff::Accessors key :nodeId # it also adds the snake_methods # i.e. `parent_id`, `parent_id_prev`, `parent_id?` (to check if updated) attr_expose :nodeId, :name, :parentId, :archived, :archivedToken, :classifications compare :parentId, :name, :archived compare :classifications #, when_present: true case_sensitive false # move method, to redefine it alias_method :name_src?, :name? # Has the property `name` changed? # @note should not be part of an insert def name? name_src? && update? end alias_method :classifications_src?, :classifications? # @note should not be part of an insert def classifications? classifications_src? && update? end alias_method :insert?, :new? # node id diff? check should be performed as a `key` alias_method :id?, :key? alias_method :nodeId?, :id? alias_method :node_id?, :nodeId? # Has any of `id` or `name` properties changed? def id_name? return true if id? return true if name? classifications? end # Has the parent id changed? def move? return false unless update? parent_id? end def unarchive! @marked_for_unarchived = true end def marked_for_unarchive? @marked_for_unarchived || false end # Has the `archived` property changed and it was `true`? def unarchive? return true if marked_for_unarchive? return false if archived return false unless update? archived? end # Has the `archived` property changed and it was `false`? def archive?(validate: true) return false if archived_prev msg = "Value of archived shouldn't be true, " msg << "because node '#{node_id}' doesn't exist " msg << "(it's being inserted). " msg << "It should have been discarded as a diff" raise msg if validate && archived && insert? return true if del? archived end end end