Permalink
Browse files

expose Chain methods as class methods for reuse

  • Loading branch information...
1 parent f681774 commit b2fe4167f84df9e177ee2698d2025be29c3e835d @sunaku committed Dec 30, 2011
Showing with 26 additions and 10 deletions.
  1. +26 −10 lib/rumai/wm.rb
View
@@ -32,27 +32,43 @@ def chain
[self]
end
+ ##
+ # Returns the object before this one in the chain.
+ #
+ def prev
+ Chain.prev chain, self
+ end
+
##
# Returns the object after this one in the chain.
#
def next
- sibling(+1)
+ Chain.next chain, self
end
##
- # Returns the object before this one in the chain.
+ # Fetches the object that comes before the
+ # given target object in the given array.
#
- def prev
- sibling(-1)
+ def self.prev array, target
+ near array, target, -1
end
- private
-
- def sibling offset
- arr = chain
+ ##
+ # Fetches the object that comes after the
+ # given target object in the given array.
+ #
+ def self.next array, target
+ near array, target, +1
+ end
- if pos = arr.index(self)
- arr[(pos + offset) % arr.length]
+ ##
+ # Fetches the object at the given offset from
+ # the given target object in the given array.
+ #
+ def self.near array, target, offset
+ if index = array.index(target)
+ array[(index + offset) % array.length]
end
end
end

0 comments on commit b2fe416

Please sign in to comment.