Sha256: 2463be97ae66b8808ff5411687bcb2734766a74dcfa4f1245a5495aec17ab218

Contents?: true

Size: 1.15 KB

Versions: 19

Compression:

Stored size: 1.15 KB

Contents

require "active_support/inflector"

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 select_labels
    select{|a| a.kind_of_label? }
  end

  def select_symbols
    select{|a| a.kind_of_symbol? }
  end

  def select_strings
    select_only :string
  end

  def select_only type    
    const = type.kind_of_label? ? "#{type.to_s.camelize}".constantize : type
    select{|a| a.kind_of? const}
  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

19 entries across 19 versions & 1 rubygems

Version Path
sugar-high-0.3.7 lib/sugar-high/kind_of.rb
sugar-high-0.3.6 lib/sugar-high/kind_of.rb
sugar-high-0.3.5 lib/sugar-high/kind_of.rb
sugar-high-0.3.4 lib/sugar-high/kind_of.rb
sugar-high-0.3.3 lib/sugar-high/kind_of.rb
sugar-high-0.3.2 lib/sugar-high/kind_of.rb
sugar-high-0.3.1 lib/sugar-high/kind_of.rb
sugar-high-0.3.0 lib/sugar-high/kind_of.rb
sugar-high-0.2.12 lib/sugar-high/kind_of.rb
sugar-high-0.2.11 lib/sugar-high/kind_of.rb
sugar-high-0.2.10 lib/sugar-high/kind_of.rb
sugar-high-0.2.9 lib/sugar-high/kind_of.rb
sugar-high-0.2.8 lib/sugar-high/kind_of.rb
sugar-high-0.2.7 lib/sugar-high/kind_of.rb
sugar-high-0.2.6 lib/sugar-high/kind_of.rb
sugar-high-0.2.5 lib/sugar-high/kind_of.rb
sugar-high-0.2.4 lib/sugar-high/kind_of.rb
sugar-high-0.2.2 lib/sugar-high/kind_of.rb
sugar-high-0.2.1 lib/sugar-high/kind_of.rb