Module: Bumbleworks::Support
- Defined in:
- lib/bumbleworks/support.rb
Overview
Support methods for utility functionality such as string modification - could also be accomplished by monkey-patching String class.
Class Method Summary collapse
- .all_files(directory, options = {}) ⇒ Object
- .camelize(string) ⇒ Object
- .constantize(name) ⇒ Object
- .humanize(string) ⇒ Object
- .titleize(string) ⇒ Object
- .tokenize(string) ⇒ Object
Class Method Details
.all_files(directory, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/bumbleworks/support.rb', line 12 def all_files(directory, = {}) Dir["#{directory}/**/*.rb"].inject({}) do |memo, path| name = File.basename(path, '.rb') name = camelize(name) if [:camelize] == true memo[path] = name memo end end |
.camelize(string) ⇒ Object
7 8 9 10 |
# File 'lib/bumbleworks/support.rb', line 7 def camelize(string) string = string.sub(/^[a-z\d]*/) { $&.capitalize } string = string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::') end |
.constantize(name) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bumbleworks/support.rb', line 21 def constantize(name) name_parts = name.split('::') name_parts.shift if name_parts.first.empty? constant = Object name_parts.each do |name_part| constant_defined = if Module.method(:const_defined?).arity == 1 constant.const_defined?(name_part) else constant.const_defined?(name_part, false) end constant = constant_defined ? constant.const_get(name_part) : constant.const_missing(name_part) end constant end |
.humanize(string) ⇒ Object
45 46 47 48 49 |
# File 'lib/bumbleworks/support.rb', line 45 def humanize(string) return nil if string.nil? tokenize(string).gsub(/_/, ' '). gsub(/^\w/) { $&.upcase } end |
.titleize(string) ⇒ Object
51 52 53 54 |
# File 'lib/bumbleworks/support.rb', line 51 def titleize(string) return nil if string.nil? humanize(string).gsub(/\b('?[a-z])/) { $1.capitalize } end |
.tokenize(string) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/bumbleworks/support.rb', line 37 def tokenize(string) return nil if string.nil? string = string.gsub(/&/, ' and '). gsub(/[ \/]+/, '_'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). downcase end |