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
-
.[](idx) ⇒ Object
this returns the enum name given the value.
- .parse(name, default = nil) ⇒ Object
- .valid?(idx) ⇒ Boolean
Instance Method Summary collapse
-
#enum(*syms) ⇒ Object
simple (sequential) enumerated values usage :.
- #require_paths(*aArgs) ⇒ Object
- #require_paths_first(*aArgs) ⇒ Object
- #secure_class(aOptions = {}) ⇒ Object
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
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
8 9 10 11 12 13 14 |
# File 'lib/buzzcore/require_paths.rb', line 8 def require_paths(*aArgs) caller_dir = File.dirname(File.(caller.first.sub(/:[0-9]+.*/,''))) aArgs.each do |aPath| aPath = File.(aPath,caller_dir) $LOAD_PATH << aPath unless $LOAD_PATH.include?(aPath) end end |
#require_paths_first(*aArgs) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/buzzcore/require_paths.rb', line 16 def require_paths_first(*aArgs) caller_dir = File.dirname(File.(caller.first.sub(/:[0-9]+.*/,''))) paths = [] aArgs.each do |aPath| aPath = File.(aPath,caller_dir) paths << aPath end paths.each do |p| $LOAD_PATH.insert(0,p) end end |
#secure_class(aOptions = {}) ⇒ Object
378 379 380 |
# File 'lib/buzzcore/misc_utils.rb', line 378 def secure_class(aOptions={}) SecureThisClass::hack(self,aOptions) end |