<strong>Note: Classes that include Selectable must also subclass Array</strong>
class Something < Array include Selectable end
Creates an alias for filter called +[]+, but only if [] doesn’t already exist in obj.
# File lib/selectable.rb, line 76 def self.included(obj) alias_method :[], :filter unless obj.method_defined? :[] end
Returns a Hash or Array
# File lib/selectable.rb, line 20 def Selectable.normalize(*tags) tags.flatten! tags = tags.first if tags.first.kind_of?(Hash) || tags.first.kind_of?(Array) # NOTE: The string enforcement is disabled # FOR NOW. #if tags.is_a?(Hash) # #tmp = {} # #tags.each_pair { |n,v| tmp[n] = v.to_s } # #tags = tmp #else # tags.collect! { |v| v.to_s } #end tags end
Return the objects that match the given tags. This process is optimized for speed so there as few conditions as possible. One result of that decision is that it does not gracefully handle error conditions. For example, if the tags in an object have not been initialized, you will see this error:
undefined method `>=' for nil:NilClass
It also means you need be aware of the types of objects you are storing as values. If you store a Symbol, you must send a Symbol here.
# File lib/selectable.rb, line 48 def filter(*tags) tags = Selectable.normalize tags # select returns an Array. We want a Selectable. items = self.select { |obj| obj.tags >= tags } self.class.new items end
Generated with the Darkfish Rdoc Generator 2.