Module: Kernel

Defined in:
lib/buzzcore/require_paths.rb,
lib/buzzcore/enum.rb,
lib/buzzcore/misc_utils.rb

Overview

This sorts out the issues of require’ing files in Ruby 1) on one line, you specify all the paths you need 2) Relative paths will be relative to the file you are in, absolute paths also supported 3) Paths will be expanded 4) Paths will only be added if they don’t already exist

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](idx) ⇒ Object

this returns the enum name given the value



34
35
36
# File 'lib/buzzcore/enum.rb', line 34

def self.[]( idx )
  (idx.is_a? Integer) ? const_get(:NAMES)[idx] : nil
end

.parse(name, default = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/buzzcore/enum.rb', line 42

def self.parse(name,default=nil)
  return default if name.nil? || name.empty?
			return default if not name = name.to_sym
  result = const_get(:NAMES).index(name)
  return result==nil ? default : result
end

.valid?(idx) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/buzzcore/enum.rb', line 38

def self.valid?(idx)
  (idx.is_a? Integer) && (idx >= 0) && (idx <= const_get(:MAXVALUE))
end

Instance Method Details

#enum(*syms) ⇒ Object

simple (sequential) enumerated values usage :

module Constants

module Gradient
  enum :B, :A, :C
end

end

then :

puts Constants::Gradient::B -> 0 puts Constants::Gradient::C -> 2 puts Constants::Gradient::MINVALUE -> 0 puts Constants::Gradient::MAXVALUE -> 2 puts Constants::Gradient::NAMES -> [:B, :A, :C] puts Constants::Gradient -> :B puts Constants::Gradient -> :A puts Constants::Gradient -> :C



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/buzzcore/enum.rb', line 22

def enum(*syms)
  syms.each_index { |i| 
    const_set(syms[i], i) 
  }
  const_set(:NAMES, syms || []) 
  const_set(:MINVALUE, syms==nil ? nil : 0) 
  const_set(:MAXVALUE, syms==nil ? nil : syms.length-1) 
  const_set(:VALUECOUNT, syms==nil ? nil : syms.length) 
  const_set(:ALL, syms==nil ? [] : (0..syms.length-1).to_a) 
  const_set(:HUMAN_NAMES, syms.map{|n| n.to_s.humanize} || []) 

  # this returns the enum name given the value
  def self.[]( idx )
    (idx.is_a? Integer) ? const_get(:NAMES)[idx] : nil
  end

  def self.valid?(idx)
    (idx.is_a? Integer) && (idx >= 0) && (idx <= const_get(:MAXVALUE))
  end

  def self.parse(name,default=nil)
    return default if name.nil? || name.empty?
	return default if not name = name.to_sym
    result = const_get(:NAMES).index(name)
    return result==nil ? default : result
  end
end

#require_paths(*aArgs) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/buzzcore/require_paths.rb', line 19

def require_paths(*aArgs)
  caller_dir = File.dirname(File.expand_path(caller.first.sub(/:[0-9]+.*/,'')))
	aArgs.each do |aPath|
		aPath = File.expand_path(aPath,caller_dir)
		$LOAD_PATH << aPath unless $LOAD_PATH.include?(aPath)
	end
end

#require_paths_first(*aArgs) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/buzzcore/require_paths.rb', line 27

def require_paths_first(*aArgs)
  caller_dir = File.dirname(File.expand_path(caller.first.sub(/:[0-9]+.*/,'')))
	paths = []
	aArgs.each do |aPath|
		aPath = File.expand_path(aPath,caller_dir)
		paths << aPath
	end
	paths.each do |p|
		$LOAD_PATH.insert(0,p)
	end
end

#require_which(aFilepath) ⇒ Object

returns full path given relative to $LOAD_PATH



10
11
12
13
14
15
16
17
# File 'lib/buzzcore/require_paths.rb', line 10

def require_which(aFilepath)
	aFilepath += '.rb'
	$LOAD_PATH.each do |dir|
		full_path = File.expand_path(File.join(dir,aFilepath))
		return full_path if File.exist? full_path
	end
	return nil
end

#secure_class(aOptions = {}) ⇒ Object



440
441
442
# File 'lib/buzzcore/misc_utils.rb', line 440

def secure_class(aOptions={})
	SecureThisClass::hack(self,aOptions)
end