module Selectable

Selectable

<strong>Note: Classes that include Selectable must also subclass Array</strong>

class Something < Array
  include Selectable
end

Public Class Methods

included(obj) click to toggle source

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
normalize(*tags) click to toggle source

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

Public Instance Methods

filter(*tags) click to toggle source

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
filter!(*tags) click to toggle source
# File lib/selectable.rb, line 63
def filter!(*tags)
  tags = Selectable.normalize tags
  self.delete_if { |obj|   obj.tags < tags }
end
rfilter(*tags) click to toggle source

Reverse filter.

# File lib/selectable.rb, line 56
def rfilter(*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
tags() click to toggle source
# File lib/selectable.rb, line 68
def tags
  t = Selectable::Tags.new
  self.each { |o| t.merge o.tags }
  t
end