class Linked::Item

This is the default implementation of a listable object

This class implements a listable value object that wraps an arbitrary value an can be with other listable items.

Attributes

value[RW]

The Item can hold an arbitrary object as its value and it will stay with the item.

@return [Object] any object that is stored in the item.

Public Class Methods

new(value = nil) click to toggle source

Creates a new item. If a list is given the item will be considered a part of that list and appended to the end of it.

@param value [Object] an arbitrary object to store with the item.

Calls superclass method Linked::Listable::new
# File lib/linked/item.rb, line 21
def initialize(value = nil)
  @value = value
  super()
end

Public Instance Methods

==(other) click to toggle source

Item equality is solely determined by tha value. If the other object responds to value, and its value is equal (#==) to this value, the objects are considered equal.

@param other [#value, Object] any object. @return [true, false] true if the value of the given object is equal to

the item value.
# File lib/linked/item.rb, line 47
def ==(other)
  return false unless other.respond_to? :value
  value == other.value
end
Also aliased as: eql?
eql?(other)
Alias for: ==
freeze() click to toggle source

Freezes the value, as well as making the item itself immutable.

@return [self]

Calls superclass method
# File lib/linked/item.rb, line 64
def freeze
  value.freeze
  super
end
hash() click to toggle source

Uses the hash value of the item value.

@return [Integer] a fixnum that can be used by Hash to identify the item.

# File lib/linked/item.rb, line 57
def hash
  value.hash
end
initialize_dup(source) click to toggle source

Calling dup on an item returns a copy that is no longer connected to the original item chain. The value will also be copied.

@param source [Item] the item to copy. @return [item] a new Item.

Calls superclass method
# File lib/linked/item.rb, line 31
def initialize_dup(source)
  @value = begin
             source.value.dup
           rescue TypeError
             source.value
           end
  super
end
inspect() { |self| ... } click to toggle source

The default inspect method becomes very cluttered the moment you start linking objects together. This implementation fixes that and only shows the class name, object id and value (if set).

@return [String] a string representation of the list item.

# File lib/linked/item.rb, line 74
def inspect
  return yield(self).to_s if block_given?
  value ? object_identifier + " value=#{value.inspect}" : object_identifier
end