Sha256: 45d1695b735faef089a0e394cea58150065cd863a78f697e3bb283c85ef01085

Contents?: true

Size: 1.84 KB

Versions: 9

Compression:

Stored size: 1.84 KB

Contents

require 'forwardable'
require 'compo/composite'

module Compo
  # Implementation of Composite that stores its children in a Hash.
  #
  # IDs for items entering a ListComposite may be any permissible hash.
  #
  # Adding an item at an occupied ID removes the occupant.
  class HashComposite
    include Composite
    extend Forwardable

    # Initialises a HashComposite
    #
    # @api  public
    # @example  Initializes a HashComposite.
    #   comp = HashComposite.new
    def initialize
      @children = {}
    end

    # Returns the HashComposite's children, as a Hash
    #
    # @api  public
    # @example  Gets the children of an empty HashComposite.
    #   comp.children
    #   #=> {}
    # @example  Gets the children of a populated HashComposite.
    #   comp.children
    #   #=> {foo: 3, bar: 5}
    #
    # @return [Hash]  The Hash mapping the IDs of children to their values.
    attr_reader :children

    private

    # Inserts a child into the HashComposite with the given ID
    #
    # You probably want to use #add instead.
    #
    # @api  private
    #
    # @param id [Object]  The ID under which the child is to be added.
    # @param child [Object]  The child to add to the HashComposite.
    #
    # @return [Object]  The newly added child.
    def add!(id, child)
      remove_id(id)
      @children[id] = child
    end

    def_delegator :@children, :delete, :remove_id!

    # Creates an ID function for the given child
    #
    # The returned proc is O(1), as it stores the ID assigned to the child at
    # calling time under the assumption that it will not change until removal.
    #
    # @api  private
    #
    # @param child [Object]  The child whose ID is to be returned by the proc.
    #
    # @return [Proc]  A proc returning the child's ID.
    def id_function(child)
      id = @children.key(child)
      proc { id }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
compo-0.3.1 lib/compo/hash_composite.rb
compo-0.3.0 lib/compo/hash_composite.rb
compo-0.2.0 lib/compo/hash_composite.rb
compo-0.1.5 lib/compo/hash_composite.rb
compo-0.1.4 lib/compo/hash_composite.rb
compo-0.1.3 lib/compo/hash_composite.rb
compo-0.1.2 lib/compo/hash_composite.rb
compo-0.1.1 lib/compo/hash_composite.rb
compo-0.1.0 lib/compo/hash_composite.rb