Module: Artoo::Utility

Included in:
Commands::Commands, Connection, Device, Generator::Adaptor, Robot
Defined in:
lib/artoo/utility.rb

Overview

Utility methods used for various important things

Instance Method Summary collapse

Instance Method Details

#classify(word) ⇒ Class

Convert a underscore string to a class

Examples:

"underscore_word" > UnderscoreWord

Returns:

  • (Class)

    converted class



40
41
42
# File 'lib/artoo/utility.rb', line 40

def classify(word)
  underscore(word).gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#constantize(camel_cased_word) ⇒ Constant

Converts camel_cased_word to constant

Examples:

"CamelCasedWord" > CamelCasedWord

Returns:

  • (Constant)

    Converted constant



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/artoo/utility.rb', line 11

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_classClass

Returns current actor class.

Returns:

  • (Class)

    current actor class



67
68
69
# File 'lib/artoo/utility.rb', line 67

def current_class
  Celluloid::Actor.current.class
end

#current_instanceCelluloid::Actor

Returns current instance.

Returns:

  • (Celluloid::Actor)

    current instance



62
63
64
# File 'lib/artoo/utility.rb', line 62

def current_instance
  Celluloid::Actor.current
end

#osObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/artoo/utility.rb', line 71

def os
  @os ||= (
    host_os = RbConfig::CONFIG['host_os']
    case host_os
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      :windows
    when /darwin|mac os/
      :macosx
    when /linux/
      :linux
    when /solaris|bsd/
      :unix
    else
      raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
    end
  )
end

#random_stringString

Returns random string.

Returns:

  • (String)

    random string



45
46
47
# File 'lib/artoo/utility.rb', line 45

def random_string
  (0...8).map{65.+(rand(26)).chr}.join
end

#remove_keys(h, *keys) ⇒ Hash

Removes selected keys from hash

Examples:

'one', two: 'two' => 'two'

Returns:

  • (Hash)

    new object without selected keys



92
93
94
95
96
# File 'lib/artoo/utility.rb', line 92

def remove_keys(h, *keys)
  hash = h.dup
  keys.each { |key| hash.delete(key)  }
  hash
end

#underscore(camel_cased_word) ⇒ Object

Converts camel cased word to downcased with undercores

Examples:

CamelCase > camel_case



51
52
53
54
55
56
57
58
59
# File 'lib/artoo/utility.rb', line 51

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