Sha256: 3ec22f3d629f4a60530fe6e2bb2cc0928d72805f6eb97b7609abfea8729d1bcf

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 KB

Contents

module NCore
  module Util
    extend ActiveSupport::Concern

    def self.deep_clone(value)
      case value
      when Hash
        cl = value.clone
        value.each{|k,v| cl[k] = deep_clone(v)}
        cl
      when Array
        cl = value.clone
        cl.clear
        value.each{|v| cl << deep_clone(v)}
        cl
      when NilClass, Numeric, TrueClass, FalseClass
        value
      else
        value.clone rescue value
      end
    end


    module ClassMethods

      def interpret_type(val_or_enum, api_creds)
        case val_or_enum
        when Hash
          if key = val_or_enum[:object]
            discover_class(key).new({data: val_or_enum}, api_creds)
          else
            val_or_enum
          end
        when Array
          val_or_enum.map{|v| interpret_type v, api_creds }
        else
          val_or_enum
        end
      end


      private

      def discover_class(key, default_klass=parent::GenericObject)
        klass_name = key.to_s.camelize.singularize
        begin
          "#{module_name}::#{klass_name}".constantize
        rescue NameError => e
          default_klass
        end
      end

    end


    def inspect
      "#<#{self.class}:0x#{self.object_id.to_s(16)}> id: #{id.inspect}, attribs: #{@attribs.except(:id).inspect}, metadata: #{metadata.inspect}"
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ncore-2.3.3 lib/ncore/util.rb
ncore-2.3.2 lib/ncore/util.rb
ncore-2.3.1 lib/ncore/util.rb
ncore-2.3.0 lib/ncore/util.rb
ncore-2.2.2 lib/ncore/util.rb
ncore-2.2.1 lib/ncore/util.rb
ncore-2.2.0 lib/ncore/util.rb
ncore-2.1.2 lib/ncore/util.rb
ncore-2.1.1 lib/ncore/util.rb
ncore-2.1.0 lib/ncore/util.rb
ncore-2.0.8 lib/ncore/util.rb