| 1 | package Zing::ID; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | use Sys::Hostname (); | ||
| 15 | |||
| 16 | use overload ( | ||
| 17 | '""' => 'string', | ||
| 18 | fallback => 1 | ||
| 19 | ); | ||
| 20 | |||
| 21 | our $VERSION = '0.26'; # VERSION | ||
| 22 | |||
| 23 | # ATTRIBUTES | ||
| 24 | |||
| 25 | has host => ( | ||
| 26 | is => 'ro', | ||
| 27 | isa => 'Str', | ||
| 28 | init_arg => undef, | ||
| 29 | new => 1, | ||
| 30 | ); | ||
| 31 | |||
| 32 | fun new_host($self) { | ||
| 33 | Sys::Hostname::hostname | ||
| 34 | } | ||
| 35 | |||
| 36 | has iota => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Int', | ||
| 39 | init_arg => undef, | ||
| 40 | new => 1, | ||
| 41 | ); | ||
| 42 | |||
| 43 | my ($i, $t, $x) = (0, time); | ||
| 44 | |||
| 45 | fun new_iota($self) { | ||
| 46 | $x = sprintf('%04d', ($i = ($t == time ? $i + 1 : 1))); | ||
| 47 | $t = time; # reset | ||
| 48 | $x | ||
| 49 | } | ||
| 50 | |||
| 51 | has pid => ( | ||
| 52 | is => 'ro', | ||
| 53 | isa => 'Int', | ||
| 54 | init_arg => undef, | ||
| 55 | new => 1, | ||
| 56 | ); | ||
| 57 | |||
| 58 | fun new_pid($self) { | ||
| 59 | $$ | ||
| 60 | } | ||
| 61 | |||
| 62 | has salt => ( | ||
| 63 | is => 'ro', | ||
| 64 | isa => 'Str', | ||
| 65 | new => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | fun new_salt($self) { | ||
| 69 | rand | ||
| 70 | } | ||
| 71 | |||
| 72 | has time => ( | ||
| 73 | is => 'ro', | ||
| 74 | isa => 'Int', | ||
| 75 | init_arg => undef, | ||
| 76 | new => 1, | ||
| 77 | ); | ||
| 78 | |||
| 79 | fun new_time($self) { | ||
| 80 | time | ||
| 81 | } | ||
| 82 | |||
| 83 | # METHODS | ||
| 84 | |||
| 85 | method string() { | ||
| 86 | require Digest::SHA; return Digest::SHA::sha1_hex( | ||
| 87 | join('-', map $self->$_, qw(host pid time iota salt)) | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | =encoding utf8 | ||
| 94 | |||
| 95 | =head1 NAME | ||
| 96 | |||
| 97 | Zing::ID - Conditionally Unique Identifier | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | =head1 ABSTRACT | ||
| 102 | |||
| 103 | Conditionally Unique Identifier | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | =head1 SYNOPSIS | ||
| 108 | |||
| 109 | use Zing::ID; | ||
| 110 | |||
| 111 | my $id = Zing::ID->new; | ||
| 112 | |||
| 113 | # "$id" | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | =head1 DESCRIPTION | ||
| 118 | |||
| 119 | This package provides a globally unique identifier. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | =head1 LIBRARIES | ||
| 124 | |||
| 125 | This package uses type constraints from: | ||
| 126 | |||
| 127 | L<Zing::Types> | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | =head1 ATTRIBUTES | ||
| 132 | |||
| 133 | This package has the following attributes: | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | =head2 host | ||
| 138 | |||
| 139 | host(Str) | ||
| 140 | |||
| 141 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | =head2 iota | ||
| 146 | |||
| 147 | iota(Int) | ||
| 148 | |||
| 149 | This attribute is read-only, accepts C<(Int)> values, and is optional. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | =head2 pid | ||
| 154 | |||
| 155 | pid(Int) | ||
| 156 | |||
| 157 | This attribute is read-only, accepts C<(Int)> values, and is optional. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | =head2 salt | ||
| 162 | |||
| 163 | salt(Str) | ||
| 164 | |||
| 165 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | =head2 time | ||
| 170 | |||
| 171 | time(Int) | ||
| 172 | |||
| 173 | This attribute is read-only, accepts C<(Int)> values, and is optional. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | =head1 METHODS | ||
| 178 | |||
| 179 | This package implements the following methods: | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | =head2 string | ||
| 184 | |||
| 185 | string() : Str | ||
| 186 | |||
| 187 | The string method serializes the object properties and generates a globally | ||
| 188 | unique identifier. | ||
| 189 | |||
| 190 | =over 4 | ||
| 191 | |||
| 192 | =item string example #1 | ||
| 193 | |||
| 194 | # given: synopsis | ||
| 195 | |||
| 196 | $id->string; | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | =head1 AUTHOR | ||
| 203 | |||
| 204 | Al Newkirk, C<awncorp@cpan.org> | ||
| 205 | |||
| 206 | =head1 LICENSE | ||
| 207 | |||
| 208 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 209 | |||
| 210 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 211 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 212 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 213 | |||
| 214 | =head1 PROJECT | ||
| 215 | |||
| 216 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 217 | |||
| 218 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 219 | |||
| 220 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 221 | |||
| 222 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 223 | |||
| 224 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 225 | |||
| 226 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 227 | |||
| 228 | =cut |