Class Selectable::Tags

  1. lib/selectable/tags.rb
Parent: ::Hash

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

Methods

public instance

  1. <
  2. <=
  3. <=>
  4. ==
  5. >
  6. >=
  7. inspect
  8. to_s

Public instance methods

< (other)
[show source]
    # File lib/selectable/tags.rb, line 61
61:     def <(other)  (self <=> other)  < 0 end
<= (other)
[show source]
    # File lib/selectable/tags.rb, line 63
63:     def <=(other) (self <=> other) <= 0 end
<=> (b)

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
[show source]
    # File lib/selectable/tags.rb, line 55
55:     def <=>(b)
56:       return 0 if self == b
57:       self.send :"compare_#{b.class}", b
58:     end
== (other)
[show source]
    # File lib/selectable/tags.rb, line 32
32:     def ==(other)
33:       if other.is_a?(Array)
34:         # NOTE: This resolves the issue of sorting an Array
35:         # with a mix of Object types (Integers, Strings, Symbols).
36:         # As in: self.values.sort == other.sort)
37:         (self.values.size == other.size) &&
38:         (self.values - other).empty?
39:       else
40:         super(other)
41:       end
42:     end
> (other)
[show source]
    # File lib/selectable/tags.rb, line 60
60:     def >(other)  (self <=> other)  > 0 end
>= (other)
[show source]
    # File lib/selectable/tags.rb, line 64
64:     def >=(other) (self <=> other) >= 0 end
inspect ()
[show source]
    # File lib/selectable/tags.rb, line 28
28:     def inspect
29:       to_s
30:     end
to_s ()
[show source]
    # File lib/selectable/tags.rb, line 20
20:     def to_s
21:       tagstr = []
22:       self.each_pair do |n,v|
23:         tagstr << "%s=%s" % [n,v]
24:       end
25:       tagstr.join ' '
26:     end