module Ribbon::Intercom::Utils

Constants

BASIC_TYPES

Public Class Methods

basic_type?(object) click to toggle source
# File lib/ribbon/intercom/utils.rb, line 16
def basic_type?(object)
  case object
  when *BASIC_TYPES then true
  else false
  end
end
classify(str) click to toggle source
# File lib/ribbon/intercom/utils.rb, line 60
def classify(str)
  str.split("_").map(&:capitalize).join
end
method_identifier(subject, method) click to toggle source

Returns an identifier for the method (e.g., A::B::C#method_name)

# File lib/ribbon/intercom/utils.rb, line 66
def method_identifier(subject, method)
  scope = subject.is_a?(Class) ? subject.name : subject.class.name
  scope + (subject.is_a?(Class) ? '.' : '#') + method.to_s
end
sanitize(object) click to toggle source

Raises an error if the object is or contains any non-basic types.

# File lib/ribbon/intercom/utils.rb, line 46
def sanitize(object)
  walk(object) { |object|
    if basic_type?(object)
      object
    else
      raise Errors::UnsafeValueError, object.inspect
    end
  }
end
symbolize_keys(hash) click to toggle source
# File lib/ribbon/intercom/utils.rb, line 56
def symbolize_keys(hash)
  hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end
walk(object, context=nil) { |object, context| ... } click to toggle source
# File lib/ribbon/intercom/utils.rb, line 23
def walk(object, context=nil, &block)
  case object
  when Hash
    Hash[
      object.map { |key, val|
        [walk(key, :hash_key, &block), walk(val, :hash_value, &block)]
      }
    ]
  when Array
    object.map { |obj| walk(obj, :array_elem, &block) }
  when Range
    Range.new(
      walk(object.begin, :range_begin, &block),
      walk(object.end, :range_end, &block),
      object.exclude_end?
    )
  else
    yield object, context
  end
end