module CkuruTools

Public Class Methods

class_space() click to toggle source
# File lib/ckuru-tools.rb, line 173
def self.class_space
  ret = TypedArray.new(Class)
  ObjectSpace.each_object {|x| ret.push(x) if x.is_a? Class}
  ret.uniq
end
libpath( *args ) click to toggle source

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.

# File lib/ckuru-tools.rb, line 194
def self.libpath( *args )
  args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
end
only_these_parameters(h,*args) click to toggle source
# File lib/ckuru-tools.rb, line 107
def self.only_these_parameters(h,*args)
  ret = parameters(h,*args)
  keys = h.keys
  args.each do |a|
    name,options = a
    keys.delete name
  end
  if keys.length > 0
    raise ArgumentError.new("unknown parameters #{keys.inspect} passed to #{self.class}##{calling_method}")
  end
  ret
end
parameters(h,*args) click to toggle source
# File lib/ckuru-tools.rb, line 54
def self.parameters(h,*args)
  raise ArgumentError.new("argument to #{self.class}##{current_method} must be of class Hash; you may get this error if you don't call a function with a hash; check the initial call") unless h.is_a? Hash
  ret = []
  args.each do |a|
    name,options = a
    options = options || {}
    unless h[:no_recurse]
      vals = only_these_parameters(
                                   options.merge!(:no_recurse => true),
                                   [:instance_that_inherits_from, {:instance_of => Class}],
                                   [:instance_of, {:instance_of => Class}],
                                   [:klass_that_inherits_from, {:instance_of => Class}],
                                   [:klass_of, {:instance_of => Class}],
                                   [:no_recurse, {:instance_of => TrueClass}],
                                   [:required, {:instance_of => TrueClass}],
                                   [:default, {:instance_of => TrueClass}]
                                   )
      instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
    end

    if val = h[name]
      if instance_that_inherits_from 
        unless val.class.inherits_from? instance_that_inherits_from
          raise ArgumentError.new(
                                  "argument :#{name} to #{self.class}##{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not") 
        end
      elsif instance_of
        unless val.class == instance_of
          raise ArgumentError.new(
                                  "argument :#{name} to #{self.class}##{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
        end
      elsif klass_that_inherits_from
        unless val.inherits_from? klass_that_inherits_from
          raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not") 
        end
      elsif klass_of
        unless val == klass_to
          raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must be of class #{klass_of}, not #{val}") 
        end
      end
    else
      if options[:default]
        val = options[:default]
      elsif options[:required]
        raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} is required")
      end
    end
    ret.push val
  end
  ret
end
path( *args ) click to toggle source

Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.

# File lib/ckuru-tools.rb, line 202
def self.path( *args )
  args.empty? ? PATH : ::File.join(PATH, *args)
end
require_all_libs_relative_to( fname, dir = nil ) click to toggle source

Utility method used to rquire all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.

# File lib/ckuru-tools.rb, line 211
def self.require_all_libs_relative_to( fname, dir = nil )
  dir ||= ::File.basename(fname, '.*')
  search_me = ::File.expand_path(
                                 ::File.join(::File.dirname(fname), dir, '**', '*.rb'))

  Dir.glob(search_me).sort.each {|rb| require rb}
end
validate_hash_arguments(h,*args) click to toggle source
# File lib/ckuru-tools.rb, line 120
def self.validate_hash_arguments(h,*args)
  raise ArgumentError.new("argument to #{current_method} must be of class Hash") unless h.is_a? Hash
  ret = []
  args.each do |a|
    name,options = a
    options = options || {}
    unless h[:no_recurse]
      vals = only_these_parameters(
        options.merge!(:no_recurse => true),
        [:instance_that_inherits_from, {:instance_of => Class}],
        [:instance_of, {:instance_of => Class}],
        [:klass_that_inherits_from, {:instance_of => Class}],
        [:klass_of, {:instance_of => Class}],
        [:no_recurse, {:instance_of => TrueClass}],
        [:required, {:instance_of => TrueClass}],
        [:default, {:instance_of => TrueClass}]
        )
      instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
    end

    if val = h[name]
      if instance_that_inherits_from 
        unless val.class.inherits_from? instance_that_inherits_from
          raise ArgumentError.new(
            "argument :#{name} to #{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not") 
        end
      elsif instance_of
        unless val.class == instance_of
          raise ArgumentError.new(
            "argument :#{name} to #{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
        end
      elsif klass_that_inherits_from
        unless val.inherits_from? klass_that_inherits_from
          raise ArgumentError.new("argument :#{name} to #{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not") 
        end
      elsif klass_of
        unless val == klass_of
          raise ArgumentError.new("argument :#{name} to #{calling_method} must be of class #{klass_of}, not #{val}") 
        end
      end
    else
      if options[:default]
        val = options[:default]
      elsif options[:required]
        raise ArgumentError.new("argument :#{name} to #{calling_method} is required")
      end
    end
    ret.push val
  end
  ret
end
version() click to toggle source

Returns the version string for the library.

# File lib/ckuru-tools.rb, line 186
def self.version
  VERSION
end