lib/sugar-high/kind_of.rb in sugar-high-0.1.4 vs lib/sugar-high/kind_of.rb in sugar-high-0.1.5
- old
+ new
@@ -1,12 +1,49 @@
class Object
def any_kind_of? *kinds
- kinds.each do |kind|
- return true if self.kind_of? kind
+ kinds.all_kinds.each do |kind|
+ return true if self.kind_of? kind
end
false
end
def kind_of_label?
self.any_kind_of? String, Symbol
+ end
+
+ def kind_of_symbol?
+ self.any_kind_of? Symbols, Symbol
+ end
+end
+
+module Enumerable
+ def only_kinds_of? *kinds
+ all?{|a| a.any_kind_of? *kinds }
+ end
+
+ def only_labels?
+ all?{|a| a.kind_of_label? }
+ end
+
+ def select_kinds_of *kinds
+ select{|a| a.any_kind_of? *kinds }
+ end
+
+ def all_kinds
+ map do |a|
+ case a
+ when Kinds
+ a.kinds
+ else
+ a if a.kind_of?(Module)
+ end
+ end.compact.uniq.flatten
+ end
+end
+
+class Kinds
+ attr_accessor :kinds
+
+ def initialize *kinds
+ self.kinds = *kinds
end
end
\ No newline at end of file