Class: String

Inherits:
Object show all
Defined in:
lib/milk.rb

Instance Method Summary collapse

Instance Method Details

#class_to_pathObject



100
101
102
# File 'lib/milk.rb', line 100

def class_to_path
  self.class_to_require.require_to_path
end

#class_to_requireObject



94
95
96
# File 'lib/milk.rb', line 94

def class_to_require
  self.gsub(/([a-z])([A-Z])/) { "#{$1}_#{$2}" }.downcase.gsub('::', '/')
end

#constantizeObject

Add ability to get constants from a string name. WARNING: this is akin to eval for security concerns.



72
73
74
75
76
77
78
# File 'lib/milk.rb', line 72

def constantize
  const = Object
  self.split("::").each do |part|
    const = const.const_get(part)
  end
  const
end

#path_to_classObject



97
98
99
# File 'lib/milk.rb', line 97

def path_to_class
  self.path_to_require.require_to_class
end

#path_to_requireObject

Useful for converting between filepaths, require paths, and class names



81
82
83
84
85
86
87
# File 'lib/milk.rb', line 81

def path_to_require
  name = self.rpartition('.').first
  $LOAD_PATH.each do |lib|
    name.sub!(lib+"/", '')
  end
  name
end

#require_to_classObject



91
92
93
# File 'lib/milk.rb', line 91

def require_to_class
  self.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
end

#require_to_pathObject



88
89
90
# File 'lib/milk.rb', line 88

def require_to_path
  Milk::LIB_DIR + "/" + self + ".rb"
end