Sha256: ef7b8a35751bf745959df581613c50fe49752cd18133ead25d121be15ef5009c

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

module Compo
  # Helper mixin for objects that can be moved into other objects
  #
  # This mixin defines a method, #move_to, which handles removing a child
  # from its current parent and adding it to a new one.
  #
  # It expects the current parent to be reachable from #parent.
  module Movable
    # Moves this model object to a new parent with a new ID
    #
    # @api  public
    # @example  Moves the object to a new parent.
    #   movable.move_to(parent, :id)
    # @example  Moves the object out of its parent (deleting it, if there are
    #   no other live references).
    #   movable.move_to(nil, nil)
    #
    # @param new_parent [ModelObject] The new parent for this object (can be
    #   nil).
    # @param new_id [Object]  The new ID under which the object will exist in
    #   the parent.
    #
    # @return [self]
    def move_to(new_parent, new_id)
      move_from_old_parent
      move_to_new_parent(new_parent, new_id)
      self
    end

    private

    # Performs the move from an old parent, if necessary
    #
    # @api private
    #
    # @return [void]
    def move_from_old_parent
      parent.remove(self) unless parent.nil?
    end

    # Performs the move to a new parent, if necessary
    #
    # @api private
    #
    # @param new_parent [Composite]  The target parent of the move.
    # @param new_id [Object]  The intended new ID of this child.
    #
    # @return [void]
    def move_to_new_parent(new_parent, new_id)
      new_parent.add(new_id, self) unless new_parent.nil?
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
compo-0.2.0 lib/compo/movable.rb
compo-0.1.5 lib/compo/movable.rb
compo-0.1.4 lib/compo/movable.rb
compo-0.1.3 lib/compo/movable.rb
compo-0.1.2 lib/compo/movable.rb
compo-0.1.1 lib/compo/movable.rb
compo-0.1.0 lib/compo/movable.rb