Module: ActiveMethod::Util
- Defined in:
- lib/active_method/util.rb
Class Method Summary collapse
- .camel_case(str) ⇒ Object
- .constantize(klass, name) ⇒ Object
- .namespaces_of(klass) ⇒ Object
- .snake_case(str) ⇒ Object
Class Method Details
.camel_case(str) ⇒ Object
30 31 32 33 34 |
# File 'lib/active_method/util.rb', line 30 def camel_case(str) str = str.to_s return str if str !~ /_/ && str =~ /[A-Z]+.*/ str.split("_").map(&:capitalize).join end |
.constantize(klass, name) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/active_method/util.rb', line 5 def constantize(klass, name) constant = klass.const_get(name) rescue nil return constant unless constant.nil? namespaces_of(klass).each do |namespace| constant = namespace.const_get(name) rescue nil return constant unless constant.nil? end raise NameError.new("wrong constant name #{name}") if constant.nil? end |
.namespaces_of(klass) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/active_method/util.rb', line 17 def namespaces_of(klass) namespaces = [] names = klass.name.split("::") names.pop while !names.empty? namespaces << Object.const_get(names.join("::")) names.pop end namespaces end |
.snake_case(str) ⇒ Object
36 37 38 39 40 |
# File 'lib/active_method/util.rb', line 36 def snake_case(str) return str.downcase if str =~ /^[A-Z_]+$/ str.gsub(/\B[A-Z]/, '_\&').squeeze("_") =~ /_*(.*)/ $+.downcase end |