Sha256: 657c40621568f47a82e80cae9f6a415104a9cf32ed4099c6f6cb51f12b8c4a4f

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Compo
  module Mixins
    # 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 [Object] 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)
      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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
compo-0.5.1 lib/compo/mixins/movable.rb