Module: RightScale::InfrastructureHelpers
- Defined in:
- lib/right_infrastructure_agent/infrastructure_helpers.rb
Instance Method Summary collapse
-
#constantize(camel_cased_word) ⇒ Object
Tries to find a constant with the name specified in the argument string:.
-
#format_error(description, exception, backtrace = :caller) ⇒ String
Format log message for an error.
-
#render_nothing ⇒ TrueClass
Render result as nothing.
-
#to_bool(param) ⇒ TrueClass, FalseClass
Convert string parameter to boolean.
-
#to_int_or_nil(param) ⇒ Integer, NilClass
Convert string parameter to integer if not nil or empty.
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
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_nothing ⇒ TrueClass
Render result as nothing
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
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
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 |