Sha256: c52c022aa74f836d4a2aa2b993a160b2ba767084881a3b5438e6cd447e3d3dfe

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module Trax
  module Model
    module UniqueId
      extend ::ActiveSupport::Concern

      included do
        #grab prefix from uuid registry if using that
        if ::Trax::Model::UUID.klass_prefix_map.has_key?(self.name)
          self.trax_defaults.uuid_prefix = ::Trax::Model::UUIDPrefix.new(klass_prefix_map[self.name])
        end
      end

      def uuid
        uuid_column = self.class.trax_defaults.uuid_column
        uuid_value = (uuid_column == "uuid") ? super : __send__(uuid_column)

        ::Trax::Model::UUID.new(uuid_value)
      end

      #i.e. Blog::Post
      def uuid_type
        uuid.record_type
      end

      #i.e. 'Blog::Post'
      def uuid_type_name
        uuid.record_type.name
      end

      #i.e, Blog::Post will = post
      def uuid_type_slug
        uuid.record_type.name.demodulize.underscore
      end

      module ClassMethods
        delegate :uuid_prefix, :to => :trax_defaults
        delegate :uuid_column, :to => :trax_defaults

        def defaults(*args)
          super(*args)

          self.default_value_for(:"#{self.trax_defaults.uuid_column}") {
            ::Trax::Model::UUID.generate(self.trax_defaults.uuid_prefix)
          }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trax_model-0.0.8 lib/trax/model/unique_id.rb
trax_model-0.0.7 lib/trax/model/unique_id.rb
trax_model-0.0.6 lib/trax/model/unique_id.rb