Class: Compo::Composites::Parentless

Inherits:
Object
  • Object
show all
Includes:
Composite
Defined in:
lib/compo/composites/parentless.rb

Overview

A Composite that represents the non-existent parent of an orphan

Parentless is the parent assigned when an object is removed from a Composite, and should be the default parent of an object that can be added to one. It exists to make some operations easier, such as URL creation.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composite

#add, #get_child, #get_child_such_that, #remove_id

Class Method Details

.for(item) ⇒ void

This method returns an undefined value.

Creates a new instance of Parentless and adds an item to it

This effectively removes the item’s parent.

If this method is passed nil, then nothing happens.

Examples:

Makes a new Parentless for an item.

Parentless.for(item)

Does nothing.

Parentless.for(nil)

Parameters:

  • item (Object)

    The item to be reparented to a Parentless.



29
30
31
# File 'lib/compo/composites/parentless.rb', line 29

def self.for(item)
  new.add(nil, item) unless item.nil?
end

Instance Method Details

#childrenHash

Returns the empty hash

Examples:

Gets the children

parentless.children
#=> {}

Returns:

  • (Hash)

    The empty hash.



56
57
58
# File 'lib/compo/composites/parentless.rb', line 56

def children
  {}
end

#on_nodenil

Performs an action on this node, if it is an actual Composite node

A Parentless is not, and thus this method returns nil and ignores any block present.

Examples:

(Doesn’t) perform an action on this Parentless.

parentless.on_node { |n| 3 }
#=> nil

Returns:

  • (nil)


98
99
100
# File 'lib/compo/composites/parentless.rb', line 98

def on_node
  nil
end

#parentself

Returns the parent of this Parentless

This is always the same Parentless, for convenience’s sake. Technically, as a null object, Parentless has no parent.

Examples:

Gets the ‘parent’ of a Parentless.

parentless.parent

Returns:

  • (self)


84
85
86
# File 'lib/compo/composites/parentless.rb', line 84

def parent
  self
end

#remove(child) ⇒ Object

‘Removes’ a child from this Parentless

This always succeeds, and never triggers any other action.

Examples:

‘Removes’ a child.

parentless.remove(child)

Parameters:

  • child (Object)

    The child to ‘remove’ from this Parentless.

Returns:

  • (Object)

    The child.



44
45
46
# File 'lib/compo/composites/parentless.rb', line 44

def remove(child)
  child
end

#urlHash

Returns the URL of this Parentless

This is always the empty string.

Examples:

Gets the URL of a Parentless

parentless.url
#=> ''

Returns:

  • (Hash)

    The empty string.



70
71
72
# File 'lib/compo/composites/parentless.rb', line 70

def url
  ''
end