Module: Artoo::Utility
- Included in:
- Connection, Device, Robot
- Defined in:
- lib/artoo/utility.rb
Overview
Utility methods used for convertions
Instance Method Summary collapse
-
#classify(word) ⇒ Class
Convert a underscore string to a class.
-
#constantize(camel_cased_word) ⇒ Constant
Converts camel_cased_word to constant.
-
#current_class ⇒ Class
Current actor class.
-
#current_instance ⇒ Celluloid::Actor
Current instance.
-
#random_string ⇒ String
Random string.
-
#underscore(camel_cased_word) ⇒ Object
Converts camel cased word to downcased with undercores.
Instance Method Details
#classify(word) ⇒ Class
Convert a underscore string to a class
37 38 39 |
# File 'lib/artoo/utility.rb', line 37 def classify(word) underscore(word).gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#constantize(camel_cased_word) ⇒ Constant
Converts camel_cased_word to constant
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/artoo/utility.rb', line 8 def constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? names.inject(Object) do |constant, name| if constant == Object constant.const_get(name) else candidate = constant.const_get(name) next candidate if constant.const_defined?(name, false) next candidate unless Object.const_defined?(name) # Go down the ancestors to check it it's owned # directly before we reach Object or the end of ancestors. constant = constant.ancestors.inject do |const, ancestor| break const if ancestor == Object break ancestor if ancestor.const_defined?(name, false) const end # owner is in Object, so raise constant.const_get(name, false) end end end |
#current_class ⇒ Class
Returns current actor class.
64 65 66 |
# File 'lib/artoo/utility.rb', line 64 def current_class Celluloid::Actor.current.class end |
#current_instance ⇒ Celluloid::Actor
Returns current instance.
59 60 61 |
# File 'lib/artoo/utility.rb', line 59 def current_instance Celluloid::Actor.current end |
#random_string ⇒ String
Returns random string.
42 43 44 |
# File 'lib/artoo/utility.rb', line 42 def random_string (0...8).map{65.+(rand(26)).chr}.join end |
#underscore(camel_cased_word) ⇒ Object
Converts camel cased word to downcased with undercores
48 49 50 51 52 53 54 55 56 |
# File 'lib/artoo/utility.rb', line 48 def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |