Socket
Book a DemoInstallSign in
Socket

attr_uuid

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

attr_uuid

1.1.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

attr_uuid

attr_uuid makes binary uuid attribute easy to use.

Installation

Add this line to your application's Gemfile:

gem 'attr_uuid'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attr_uuid

Usage

  • In a migration file, prevent generate id column and add uuid column (tinyblob).

     class CreateUsers < ActiveRecord::Migration
         def up
             create_table :users, :id => false do |t|
                 t.column :uuid, :tinyblob
                 t.string :user_name
             end
             execute "ALTER TABLE users ADD PRIMARY KEY (`uuid`(16))"
         end
         def down
             drop_table :users
         end
     end
    
  • Change primary_key to uuid, and call attr_uuid method in a model class. When you pass autofill: true to attr_uuid, it hooks before_create callback to set automatically generated uuid as 16 byte binary data to id attirbute.

     class User < ActiveRecord::Base
         self.primary_key = "uuid"
         attr_uuid :id, column_name: "uuid", autofill: true
     end
    
  • attr_uuid adds #hex_id, #hex_id=, #formatted_id and #formatted_id= to the model to refer formatted uuid.

     user = User.create(:user_name => "foo")
     user.id  #=> "3~\x05v\xCBfAQ\xA2\xE1\xE0\xFC\x04\xFB3\xD1"
     user.hex_id  #=> "337e0576cb664151a2e1e0fc04fb33d1"
     user.formatted_id  #=> "337e0576-cb66-4151-a2e1-e0fc04fb33d1"
    
     user.hex_id = "9f648b40fa6e11e3a3ac0800200c9a66"
     user.id  #=> "\x9Fd\x8B@\xFAn\x11\xE3\xA3\xAC\b\x00 \f\x9Af"
     user.formatted_id  #=> "9f648b40-fa6e-11e3-a3ac-0800200c9a66"
    
  • attr_uuid also adds .find_by_hex_id, .find_by_formatted_id, .find_all_by_hex_id and .find_all_by_formatted_id methods to retrive model from data store with an uuid string.

     User.find_by_hex_id("337e0576cb664151a2e1e0fc04fb33d1")
     User.find_by_formatted_id("337e0576-cb66-4151-a2e1-e0fc04fb33d1")
     User.find_all_by_hex_id(["337e0576cb664151a2e1e0fc04fb33d1", "5a038fcdfcd644b5bffa0245c7cfd7fa", "165a33cff2a947549d960d95adfd119a"])
     User.find_all_by_formatted_id(["337e0576-cb66-4151-a2e1-e0fc04fb33d1", "5a038fcd-fcd6-44b5-bffa-0245c7cfd7fa", "165a33cf-f2a9-4754-9d96-0d95adfd119a"])
    

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

FAQs

Package last updated on 20 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.