Class: UUID
- Inherits:
-
Object
- Object
- UUID
- Includes:
- Comparable
- Defined in:
- lib/extra/uuid.rb,
lib/inat/data/types/extras.rb
Constant Summary collapse
- BASE =
16
- BYTES =
16
- DIGITS =
32
- ZERO =
make 0
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .ddl ⇒ Object
- .from_db(src) ⇒ Object
- .generate(count = 1, prefix: nil, prefix_length: nil) ⇒ Object
- .make(*args) ⇒ Object
- .parse(src) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(value) ⇒ UUID
constructor
A new instance of UUID.
- #inspect ⇒ Object
- #to_db ⇒ Object
- #to_i ⇒ Object
- #to_query ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ UUID
Returns a new instance of UUID.
13 14 15 |
# File 'lib/extra/uuid.rb', line 13 def initialize value @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
11 12 13 |
# File 'lib/extra/uuid.rb', line 11 def value @value end |
Class Method Details
.ddl ⇒ Object
70 71 72 |
# File 'lib/inat/data/types/extras.rb', line 70 def ddl :TEXT end |
.from_db(src) ⇒ Object
74 75 76 |
# File 'lib/inat/data/types/extras.rb', line 74 def from_db src parse src end |
.generate(count = 1, prefix: nil, prefix_length: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/extra/uuid.rb', line 30 def generate count = 1, prefix: nil, prefix_length: nil if prefix == nil if prefix_length != nil raise ArgumentError, "Prefix length must be Integer < #{ BYTES }!", caller unless Integer === prefix_length && prefix_length < BYTES prefix = SecureRandom.hex prefix_length else prefix = '' prefix_length = 0 end else raise TypeError, "Prefix must be a String!", caller unless String === prefix prefix = prefix.gsub('-', '') prefix_length = prefix.length / 2 end result = count.times.map { parse(prefix + SecureRandom.hex(BYTES - prefix_length)) } if count == 1 result[0] else result end end |
.make(*args) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/extra/uuid.rb', line 52 def make *args raise ArgumentError, "Method receives 1..5 arguments!", caller unless (1..5) === args.size raise ArgumentError, "Arguments must be Integers!", caller unless args.all? { |a| Integer === a } last = args.pop value = 0 value += args[0] << 12 * 8 if args.size > 0 value += args[1] << 10 * 8 if args.size > 1 value += args[2] << 8 * 8 if args.size > 2 value += args[3] << 6 * 8 if args.size > 3 value += last new(value).freeze end |
.parse(src) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/extra/uuid.rb', line 21 def parse src return nil if src == nil raise TypeError, "Source must be a String!", caller unless String === src src = src.strip raise ArgumentError, "Invalid UUID source: #{ src.inspect }!", caller unless /^\h{8}\-\h{4}\-\h{4}\-\h{4}\-\h{12}$/ === src || /^\h{32}$/ === src value = src.gsub('-', '').to_i(BASE) new(value).freeze end |
Instance Method Details
#<=>(other) ⇒ Object
85 86 87 88 |
# File 'lib/extra/uuid.rb', line 85 def <=> other return nil unless UUID === other @value <=> other.value end |
#inspect ⇒ Object
79 80 81 |
# File 'lib/extra/uuid.rb', line 79 def inspect to_s end |
#to_db ⇒ Object
80 81 82 |
# File 'lib/inat/data/types/extras.rb', line 80 def to_db to_s end |
#to_i ⇒ Object
69 70 71 |
# File 'lib/extra/uuid.rb', line 69 def to_i @value end |
#to_query ⇒ Object
84 85 86 |
# File 'lib/inat/data/types/extras.rb', line 84 def to_query to_s end |