Sha256: bb6711a798c90ad189088186cb146cb876063022b34395c1b615886f5f6a2472

Contents?: true

Size: 870 Bytes

Versions: 3

Compression:

Stored size: 870 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module Traits
  class UnidentifiedModelReference < StandardError
  end

  module Utilities
    def retrieve_model_class(obj)
      if active_record_descendant?(obj)
        obj

      elsif active_record_instance?(obj)
        obj.class

      else
        case obj
          when Symbol then obj.to_s.camelize.constantize
          when String then obj.camelize.constantize
          when Model  then obj.model_class
          else raise UnidentifiedModelReference, obj
        end
      end
    end

    def active_record_descendant?(obj)
      obj.kind_of?(Class) && obj < ActiveRecord::Base
    end

    def active_record_instance?(obj)
      obj.kind_of?(ActiveRecord::Base)
    end

    def valid_active_record_identifier?(id)
      id.kind_of?(String) || id.kind_of?(Numeric)
    end
  end

  extend Utilities
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activerecord-traits-1.0.3 lib/traits/utilities.rb
activerecord-traits-1.0.2 lib/traits/utilities.rb
activerecord-traits-1.0.1 lib/traits/utilities.rb