Module: RightScale::InfrastructureHelpers

Included in:
GlobalObjectReplicatorSink, GlobalObjectReplicatorSource, InfrastructureAuthClient
Defined in:
lib/right_infrastructure_agent/infrastructure_helpers.rb

Instance Method Summary collapse

Instance Method Details

#constantize(camel_cased_word) ⇒ Object

Tries to find a constant with the name specified in the argument string:

"Module".constantize     # => Module
"Test::Unit".constantize # => Test::Unit

The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account:

C = 'outside'
module M
  C = 'inside'
  C               # => 'inside'
  "C".constantize # => 'outside', same as ::C
end

NameError is raised when the name is not in CamelCase or the constant is unknown.



72
73
74
75
76
77
78
79
80
81
# File 'lib/right_infrastructure_agent/infrastructure_helpers.rb', line 72

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#format_error(description, exception, backtrace = :caller) ⇒ String

Format log message for an error

Parameters:

  • description (String)

    Error description that is placed first in output

  • exception (Exception, String)

    Exception or exception message

  • backtrace (Symbol) (defaults to: :caller)

    Exception backtrace extent: :no_trace, :caller, or :trace, defaults to :caller

Returns:

  • (String)

    Formatted error



33
34
35
# File 'lib/right_infrastructure_agent/infrastructure_helpers.rb', line 33

def format_error(description, exception, backtrace = :caller)
  RightScale::Log.format(description, exception, backtrace)
end

#render_nothingTrueClass

Render result as nothing

Returns:

  • (TrueClass)

    Always return true



20
21
22
23
# File 'lib/right_infrastructure_agent/infrastructure_helpers.rb', line 20

def render_nothing
  render(:nothing => true, :status => :no_content)
  true
end

#to_bool(param) ⇒ TrueClass, FalseClass

Convert string parameter to boolean

Parameters:

  • param (String, TrueClass, FalseClass, NilClass)

    Parameter value

Returns:

  • (TrueClass, FalseClass)

    Boolean form of parameter



51
52
53
# File 'lib/right_infrastructure_agent/infrastructure_helpers.rb', line 51

def to_bool(param)
  ![nil, false, "false"].include?(param)
end

#to_int_or_nil(param) ⇒ Integer, NilClass

Convert string parameter to integer if not nil or empty

Parameters:

  • param (String, NilClass)

    Parameter value

Returns:

  • (Integer, NilClass)

    Integer form of parameter, or nil if not a string



42
43
44
# File 'lib/right_infrastructure_agent/infrastructure_helpers.rb', line 42

def to_int_or_nil(param)
  (param.nil? || (param.respond_to?(:empty?) && param.empty?)) ? nil : param.to_i
end