require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/array/access'
require 'active_model/global_id/railtie'

module ActiveModel
  autoload :GlobalLocator,  'active_model/global_locator'
  autoload :SignedGlobalID, 'active_model/signed_global_id'

  class GlobalID
    def self.create(model)
      new "GlobalID-#{model.class.name}-#{model.id}"
    end

    def initialize(gid)
      @gid = gid
    end

    def model_class
      @model_klass ||= @gid.split("-").second.constantize
    end

    def model_id
      @model_id ||= @gid.split('-')[2..-1].join('-')
    end

    def ==(other_global_id)
      other_global_id.is_a?(GlobalID) && to_s == other_global_id.to_s
    end

    def to_s
      @gid
    end
  end
end
