Sha256: ec5fc82833e7d6e32ed529a8bab1487547a996914a8145009982ca0ae59fe62a

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Paid
  module Util

    def self.symbolize_keys(obj)
      if obj.is_a?(Hash)
        ret = {}
        obj.each do |key, value|
          ret[(key.to_sym rescue key) || key] = symbolize_keys(value)
        end
        return ret
      elsif obj.is_a?(Array)
        return obj.map{ |value| symbolize_keys(value) }
      else
        return obj
      end
    end

    def self.sorta_deep_clone(json)
      if json.is_a?(Hash)
        ret = {}
        json.each do |k, v|
          ret[k] = sorta_deep_clone(v)
        end
        ret
      elsif json.is_a?(Array)
        json.map{ |j| sorta_deep_clone(j) }
      else
        begin
          json.dup
        rescue
          json
        end
      end
    end

    def self.constantize(str, prefix=false)
      str = str.to_s
      begin
        str.split('::').reduce(Module, :const_get)
      rescue NameError => e
        if prefix
          raise e
        else
          p = "#{self.name}".split("::").first
          constantize("#{p}::#{str}", true)
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
paid-1.0.5 lib/paid/util.rb
paid-1.0.3 lib/paid/util.rb
paid-1.0.2 lib/paid/util.rb