lib/linked/item.rb in linked-0.1.3 vs lib/linked/item.rb in linked-0.2.0
- old
+ new
@@ -22,11 +22,11 @@
# Access the list (if any) that the item belongs to. Writing to this
# attribute is protected and should be avoided.
#
# Returns the item's list, or nil
- attr_accessor :list
+ attr_writer :list
protected :list=
# The Item can hold an arbitrary object as its value and it will stay with
# the item.
@@ -68,9 +68,39 @@
source.value.dup
rescue TypeError
source.value
end
super
+ end
+
+ # Identity method that simply return the item. This method mirrors List#item
+ # and allows other methods that work on Item objects to easily and
+ # interchangebly accept both lists and items as arguments.
+ #
+ # Returns the item itself.
+
+ def item
+ self
+ end
+
+ # Access the list that the item is part of. If the item is not in a list a
+ # NoMethodError will be raised. This mirrors the behaviour of List#list and
+ # allows other methods that work on List objects to easily and
+ # interchangeably accept both lists and items as arguments.
+ #
+ # Returns the list that the item is part of.
+
+ def list
+ raise NoMethodError unless @list
+ @list
+ end
+
+ # Check it the item is part of a list.
+ #
+ # Returns true if the item is in a list.
+
+ def in_list?
+ @list ? true : false
end
# Check if this is the first item in the list. It is crucial that tail#nil?
# returns true for the first item to be identified correctly.
#