Sha256: 031e78d029a95c04ce4604d9db4cd71c600bfe079baec48c58bf7589184ffbf1

Contents?: true

Size: 939 Bytes

Versions: 6

Compression:

Stored size: 939 Bytes

Contents

module Trax
  module Model
    class UUID < String
      class_attribute :prefix_map

      self.prefix_map = ::Hashie::Mash.new

      def self.klass_prefix_map
        prefix_map.invert
      end

      def self.generate(prefix = nil)
        uuid = ::SecureRandom.uuid
        uuid[0..1] = prefix if prefix
        uuid
      end

      def self.prefix(prefix_value, klass)
        if prefix_map.has_key(:"#{prefix_value}") && prefix_map[:"#{prefix_value}"] == klass
          raise ::Trax::Model::Errors::DuplicatePrefix.new(prefix_value)
        end

        prefix_map[:"#{prefix_value}"] = klass
      end

      def self.register(&block)
        instance_exec(&block)
      end

      def record
        @record ||= record_type ? record_type.find_by(:"#{record_type.uuid_column}" => self) : nil
      end

      def record_type
        @record_type ||= ::Trax::Model::Registry.model_type_for_uuid(self)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
trax_model-0.0.92 lib/trax/model/uuid.rb
trax_model-0.0.91 lib/trax/model/uuid.rb
trax_model-0.0.9 lib/trax/model/uuid.rb
trax_model-0.0.8 lib/trax/model/uuid.rb
trax_model-0.0.7 lib/trax/model/uuid.rb
trax_model-0.0.6 lib/trax/model/uuid.rb