Sha256: ce4447d6c31e0bcb124f0a0798855c97dea4a6b7c24f8f8fec7a11e6a4570557

Contents?: true

Size: 1.12 KB

Versions: 34

Compression:

Stored size: 1.12 KB

Contents

module ChargeBee
  module Util
    
    def self.serialize(value, prefix = nil, idx = nil)
      serialized = {}
      case value
        when Hash
          value.each do |k, v|
            if(v.instance_of? Hash or v.instance_of? Array)
              serialized.merge!(serialize(v, k))
            else
              key = "#{(prefix!=nil) ? prefix:''}#{(prefix!=nil) ? '['+k.to_s+']' : k}#{(idx != nil) ? '['+idx.to_s+']':''}"
              serialized.merge!({key => (v!=nil)?v:''})
            end
          end
        when Array
          value.each_with_index do |v, i|
            serialized.merge!(serialize(v, prefix, i))
          end
        else
          raise ArgumentError.new("only hash or arrays are allowed as value")
      end
      serialized
    end
    
    def self.symbolize_keys(object)
      case object
      when Hash
        new = {}
        object.each do |key, value|
          key = (key.to_sym rescue key) || key
          new[key] = symbolize_keys(value)
        end
        new
      when Array
        object.map { |value| symbolize_keys(value) }
      else
        object
      end
    end
    
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
chargebee-1.3.5 lib/chargebee/util.rb
chargebee-1.3.4 lib/chargebee/util.rb
chargebee-1.3.3 lib/chargebee/util.rb
chargebee-1.3.2 lib/chargebee/util.rb
chargebee-1.3.1 lib/chargebee/util.rb
chargebee-1.3.0 lib/chargebee/util.rb
chargebee-1.2.9 lib/chargebee/util.rb
chargebee-1.2.8 lib/chargebee/util.rb
chargebee-1.2.7 lib/chargebee/util.rb
chargebee-1.2.6 lib/chargebee/util.rb
chargebee-1.2.5 lib/chargebee/util.rb
chargebee-1.2.4 lib/chargebee/util.rb
chargebee-1.2.3 lib/chargebee/util.rb
chargebee-1.2.2 lib/chargebee/util.rb
chargebee-1.2.1 lib/chargebee/util.rb
chargebee-1.2.0 lib/chargebee/util.rb
chargebee-1.1.9 lib/chargebee/util.rb
chargebee-1.1.8 lib/chargebee/util.rb
chargebee-1.1.7 lib/chargebee/util.rb
chargebee-1.1.6 lib/chargebee/util.rb