Class: String
Instance Method Summary collapse
- #class_to_path ⇒ Object
- #class_to_require ⇒ Object
-
#constantize ⇒ Object
Add ability to get constants from a string name.
- #path_to_class ⇒ Object
-
#path_to_require ⇒ Object
Useful for converting between filepaths, require paths, and class names.
- #require_to_class ⇒ Object
- #require_to_path ⇒ Object
Instance Method Details
#class_to_path ⇒ Object
100 101 102 |
# File 'lib/milk.rb', line 100 def class_to_path self.class_to_require.require_to_path end |
#class_to_require ⇒ Object
94 95 96 |
# File 'lib/milk.rb', line 94 def class_to_require self.gsub(/([a-z])([A-Z])/) { "#{$1}_#{$2}" }.downcase.gsub('::', '/') end |
#constantize ⇒ Object
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_class ⇒ Object
97 98 99 |
# File 'lib/milk.rb', line 97 def path_to_class self.path_to_require.require_to_class end |
#path_to_require ⇒ Object
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_class ⇒ Object
91 92 93 |
# File 'lib/milk.rb', line 91 def require_to_class self.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } end |