lib/eco/data/locations/node_diff.rb in eco-helpers-2.6.4 vs lib/eco/data/locations/node_diff.rb in eco-helpers-2.7.0
- old
+ new
@@ -5,51 +5,79 @@
# - `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/selectors'
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
- attr_expose :nodeId, :name, :parentId, :archived
-
- alias_method :insert?, :new?
-
- alias_method :diff_name_src?, :diff_name?
+ # move method, to redefine it
+ alias_method :name_src?, :name?
# Has the property `name` changed?
- def diff_name?
- diff_name_src? && update?
+ # @note should not be part of an insert
+ def name?
+ name_src? && update?
end
- alias_method :name?, :diff_name?
- alias_method :id? , :diff_name? # currently a change of name is a change of id (tag)
- #alias_method :id? , :key?
- #alias_method :nodeId? , :id?
+ 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?
- id? || diff_name?
+ return true if id?
+ return true if name?
+ classifications?
end
# Has the parent id changed?
def move?
- update? && diff_parentId?
+ return false unless update?
+ parent_id?
end
# Has the `archived` property changed and it was `true`?
def unarchive?
- !archived && update? && diff_archived?
+ return false if archived
+ return false unless update?
+ archived?
end
# Has the `archived` property changed and it was `false`?
- def archive?
- !prev_archived && (del? || archived)
+ 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