Sha256: 83d2615fb7125a657cee1727bc0308a18ffb7117fb09c2655b6746f329011952

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

require 'compo/composites/composite'

module Compo
  module Composites
    # A Composite that cannot have children
    #
    # Add/remove operations on a leaf composite always fail, and #children
    # always returns the empty hash.
    #
    # This is useful for leaf classes that still need to expose the composite
    # API.
    class Leaf
      include Composite

      # Returns the empty hash
      #
      # @api  public
      # @example  Gets the children
      #   comp.children
      #   #=> {}
      #
      # @return [Hash]  The empty hash.
      def children
        {}
      end

      private

      # Fails to add a child into the leaf
      #
      # @api  private
      #
      # @param id [Object]  Ignored.
      # @param child [Object]  Ignored.
      #
      # @return [nil]
      def add!(_, _)
        nil
      end

      # Fails to remove the given child
      #
      # @api  private
      #
      # @param child [Object]  Ignored.
      #
      # @return [nil]
      def remove!(_)
        nil
      end

      # Fails to remove the child with the given ID
      #
      # @api  private
      #
      # @param id [Object]  Ignored.
      #
      # @return [nil]
      def remove_id!(_)
        nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
compo-0.5.1 lib/compo/composites/leaf.rb
compo-0.5.0 lib/compo/composites/leaf.rb
compo-0.4.0 lib/compo/composites/leaf.rb