Sha256: 19105d08f18ab960645e9dfb4009d1221c4078345f423108f74722eae07b3267

Contents?: true

Size: 826 Bytes

Versions: 5

Compression:

Stored size: 826 Bytes

Contents

class Object
  def any_kind_of? *kinds
    kinds.all_kinds.each do |kind| 
      return true if self.kind_of? kind        
    end
    false
  end

  def kind_of_label?
    self.any_kind_of? String, Symbol          
  end

  def kind_of_symbol?
    self.any_kind_of? Symbols, Symbol          
  end
end

module Enumerable
  def only_kinds_of? *kinds    
    all?{|a| a.any_kind_of? *kinds }    
  end

  def only_labels?
    all?{|a| a.kind_of_label? }    
  end
  
  def select_kinds_of *kinds
    select{|a| a.any_kind_of? *kinds }
  end
  
  def all_kinds
    map do |a| 
      case a
      when Kinds
        a.kinds
      else
        a if a.kind_of?(Module)
      end
    end.compact.uniq.flatten
  end
end     

class Kinds            
  attr_accessor :kinds
  
  def initialize *kinds
    self.kinds = *kinds
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sugar-high-0.2.0 lib/sugar-high/kind_of.rb
sugar-high-0.1.8 lib/sugar-high/kind_of.rb
sugar-high-0.1.7 lib/sugar-high/kind_of.rb
sugar-high-0.1.6 lib/sugar-high/kind_of.rb
sugar-high-0.1.5 lib/sugar-high/kind_of.rb