class 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
Calls superclass method
# 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
Private Instance Methods
compare_Array(b)
click to toggle source
# File lib/selectable/tags.rb, line 75 def compare_Array(b) return -1 unless (self.values & b).size >= b.size 1 end
compare_Hash(b)
click to toggle source
# File lib/selectable/tags.rb, line 68 def compare_Hash(b) a = self return -1 unless (a.values_at(*b.keys) & b.values).size >= b.size 1 end
Also aliased as: "compare_Selectable::Tags"
method_missing(meth, *args)
click to toggle source
# File lib/selectable/tags.rb, line 80 def method_missing(meth, *args) raise SelectableError, "#{meth}: #{args.first} is not a Hash or Array #{self}" end