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

#child_url(_) ⇒ Hash

Given the ID of a child in this Parentless, returns that child’s URL

This is always the empty string. This is so that children of orphan objects have URLs starting with /their_id.

Examples:

Gets the URL of the child of a Parentless.

parentless.child_url(:child_id)
#=> ''

Returns:

  • (Hash)

    The empty string.



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

def child_url(_)
  ''
end

#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

#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)


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

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