Sha256: c5222505cd1b9ed9c343b2c75f05c39f48c85a6ed6369a70636521538baba0e7

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

require 'delegate'
require 'securerandom'

unless defined? SecureRandom.uuid
  # I think this only applies to 1.8.6 (and JRuby/Rubinius in 1.8 mode) now:
  def SecureRandom.uuid
    hex(16).
      sub(
	@@format_pattern ||= /(........)(....)(....)(....)(............)/,
	@@format_string ||= '\1-\2-\3-\4-\5'
      )
  end
end

# The Guid class is what it says on the packet, but you can assert a :new one.
class Guid
  def initialize(i = :new)
    if i == :new
      @value = SecureRandom.uuid.freeze
    elsif (v = i.to_s).length == 36 and !(v !~ /[^0-9a-f]/i)
      @value = v.clone.freeze
    else
      raise ArgumentError.new("Illegal non-Guid value #{i.inspect} given for Guid")
    end
  end

  def to_s
    @value
  end

  # if the value is unassigned, it equal?(:new).
  def equal? value
    @value == value
  end

  def == value
    @value == value.to_s
  end

  def inspect
    "\#<Guid #{@value}>"
  end

  def hash                              #:nodoc:
    @value.hash
  end

  def eql?(o)                           #:nodoc:
    to_s.eql?(o.to_s)
  end

  def <=>(o)				#:nodoc:
    to_s.<=>(o.to_s)
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activefacts-api-1.1.0 lib/activefacts/api/guid.rb
activefacts-api-1.0.0 lib/activefacts/api/guid.rb
activefacts-api-0.9.9 lib/activefacts/api/guid.rb
activefacts-api-0.9.8 lib/activefacts/api/guid.rb