Permalink
Browse files

IndexPath class, to help match

  • Loading branch information...
1 parent be98495 commit f37b04a048467e691a7d201c57110917c00a0be2 @colinta colinta committed Aug 30, 2012
Showing with 54 additions and 1 deletion.
  1. +16 −0 README.md
  2. +37 −0 lib/sugarcube/nsindexpath.rb
  3. +1 −1 lib/sugarcube/version.rb
View
@@ -124,6 +124,22 @@ end
"https://github.com".nsurl # => NSURL.URLWithString("https://github.com")
```
+ NSIndexPath
+-------------
+
+Use the `IndexPath` class to match `NSIndexPath` objects, for instance in a
+`UITableViewDelegate`.
+
+```ruby
+index_path = [0, 2].nsindexpath
+case index_path
+when IndexPath[0]
+when IndexPath[1, 0..5]
+when IndexPath[1, 5..objects.length]
+end
+[0, 2].nsindexpath.to_a == [0, 2] # => true
+```
+
Symbol
--------
@@ -9,3 +9,40 @@ def to_a
a
end
end
+
+
+class IndexPath
+
+ def self.[] *values
+ IndexPath.new values
+ end
+
+ def initialize values
+ @values = values
+ end
+
+ def === other
+ return true if super
+
+ if other.is_a? NSIndexPath
+ other = other.to_a
+ end
+
+ if other.is_a? Enumerable
+ ret = true
+ other_i = 0
+ @values.each do |val|
+ next_val = other[other_i]
+ other_i += 1
+ unless val === next_val
+ ret = false
+ break
+ end
+ end
+
+ return ret
+ end
+ return false
+ end
+
+end
View
@@ -1,3 +1,3 @@
module SugarCube
- Version = '0.8.1'
+ Version = '0.8.2'
end

0 comments on commit f37b04a

Please sign in to comment.