Parent

Selectable::Tags

An example of filtering an Array of tagged objects based on a provided Hash of tags or Array of tag values. obj in this case would be an object that includes Taggable.

class Something
  def [](tags={})
    tags = [tags].flatten unless tags.is_a?(Hash)
    self.select do |obj|
      obj.tags >= tags
    end
  end
end

Public Instance Methods

<(other) click to toggle source
# File lib/selectable/tags.rb, line 61
def <(other)  (self <=> other)  < 0 end
<=(other) click to toggle source
# File lib/selectable/tags.rb, line 63
def <=(other) (self <=> other) <= 0 end
<=>(b) click to toggle source

Comparison between other Hash and Array objects.

e.g.

a = {:a => 1, :b => 2}
a > {:a => 1, :b => 2, :c => 3}    # => false
a > {:a => 1}                      # => true
a < {:a => 1, :b => 2, :c => 3}    # => true
a >= [2, 1]                        # => true
a > [2, 1]                         # => false
# File lib/selectable/tags.rb, line 55
def <=>(b)
  return 0 if self == b
  self.send :"compare_#{b.class}", b
end
==(other) click to toggle source
# File lib/selectable/tags.rb, line 32
def ==(other)
  if other.is_a?(Array)
    # NOTE: This resolves the issue of sorting an Array
    # with a mix of Object types (Integers, Strings, Symbols).
    # As in: self.values.sort == other.sort)
    (self.values.size == other.size) &&
    (self.values - other).empty?
  else
    super(other)
  end
end
>(other) click to toggle source
# File lib/selectable/tags.rb, line 60
def >(other)  (self <=> other)  > 0 end
>=(other) click to toggle source
# File lib/selectable/tags.rb, line 64
def >=(other) (self <=> other) >= 0 end
inspect() click to toggle source
# File lib/selectable/tags.rb, line 28
def inspect
  to_s
end
to_s() click to toggle source
# File lib/selectable/tags.rb, line 20
def to_s
  tagstr = []
  self.each_pair do |n,v|
    tagstr << "%s=%s" % [n,v]
  end
  tagstr.join ' '
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.