`
xxj
  • 浏览: 420084 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

在rails中使用UUIDTools

阅读更多
无意间发现一UUID生成器UUIDTools:http://rubyforge.org/projects/uuidtools/,可以从这里下载http://rubyforge.org/frs/?group_id=914。它本身就是个plugin,所以并不需要我们做什么额外的事情,只需要放到vendorpluginsuuidtools下,然后在需要用的地方require进来就可以了
ex:
  def getFileName(filename)
     if !filename.nil?
       require 'uuidtools'
       filename.sub(/.*./,UUID.random_create.to_s+'.')
     end
  end


readme.txt中的使用说明
  UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
  => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
  UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
  => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
  UUID.timestamp_create
  => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
  UUID.random_create
  => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>


看一下它的测试用类:


require 'test/unit'
require 'uuidtools'

class CreateTest < Test::Unit::TestCase
  def setup
  end
  
  def test_sha1_create
    assert_equal(
      "f2d04685-b787-55da-8644-9bd28a6f5a53",
      UUID.sha1_create(UUID_URL_NAMESPACE, 'http://sporkmonger.com').to_s)
  end

  def test_md5_create
    assert_equal(
      "15074785-9071-3fe3-89bd-876e4b9e919b",
      UUID.md5_create(UUID_URL_NAMESPACE, 'http://sporkmonger.com').to_s)
  end
  
  def test_timestamp_create
    assert_not_equal(
      UUID.timestamp_create.to_s,
      UUID.timestamp_create.to_s)
    current_time = Time.now
    assert_not_equal(
      UUID.timestamp_create(current_time).to_s,
      UUID.timestamp_create(current_time).to_s)
    uuids = []
    1000.times do
      uuids << UUID.timestamp_create
    end
    assert_equal(uuids.size, (uuids.map {|x| x.to_s}).uniq.size,
      "Duplicate timestamp-based UUID generated.")
  end

  def test_random_create
    assert_not_equal(
      UUID.random_create.to_s,
      UUID.random_create.to_s)
  end
end
  • 描述: UUIDTools 生成的UUID
  • 大小: 31.7 KB
分享到:
评论
3 楼 luopeng_tr 2011-08-17  
楼主你好, 看了帖子我做了一下测试。 出来一个问题,希望指教

no such file to load -- uuidtools

Rails.root: E:/work/rest_pg

Application Trace | Framework Trace | Full Trace


app/controllers/users_controller.rb:2:in `(root)'
app/controllers/users_controller.rb:454:in `load_file'

This error occurred while loading the following files:
   uuidtools


我用的是rails3 ,UUIDTools用的是uuidtools2.1.2
2 楼 xxj 2006-10-12  
UUIDTools is a rails plugin not java
1 楼 xxj 2006-09-13  
恩, 感觉挺好的,我看typo的源代码中也是使用的这个tool,我在处理文件上传的时候,将他用来给文件重命名。

相关推荐

Global site tag (gtag.js) - Google Analytics