Class: LibUUID::UUID

Inherits:
Object
  • Object
show all
Defined in:
ext/lib_uuid.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



123
124
125
126
127
128
129
130
131
132
133
# File 'ext/lib_uuid.c', line 123

VALUE
lib_uuid_initialize(VALUE self)
{
  lib_uuid_t *s;

  Data_Get_Struct(self, lib_uuid_t, s);
  s->guid = s->short_guid = s->type = s->variant = 0;
  rb_iv_set(self, "@bytes", rb_str_new((char *) s->uu, 16));

  return self;
}

Class Method Details

.guidObject



99
100
101
102
103
104
105
106
107
108
109
# File 'ext/lib_uuid.c', line 99

VALUE
lib_uuid_generate_guid(VALUE class)
{
  char   uu_str[GUID_STRLEN];
  uuid_t uu;

  uuid_generate(uu);
  uuid_unparse(uu, uu_str);

  return rb_str_new(uu_str, GUID_STRLEN-1);
}

.new(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'ext/lib_uuid.c', line 50

VALUE
lib_uuid_new(unsigned int argc, VALUE *argv, VALUE class)
{
  lib_uuid_t *s;
  uuid_t     parsed_uu;
  VALUE      ret;

  if( argc > 1 )
    rb_raise(rb_eArgError, "too many arguments");

  if( argc == 0 )
    uuid_generate(parsed_uu);

  if( argc == 1 )
  {
    if( uuid_from_obj(argv[0], parsed_uu) == -1 )
      return Qnil;

    if( uuid_validity_check(parsed_uu) == -1 )
      return Qnil;
  }

  s = ALLOC_N(lib_uuid_t, 1);
  uuid_copy(s->uu, parsed_uu);
  ret = Data_Wrap_Struct(class, NULL, lib_uuid_free, s);
  rb_obj_call_init(ret, 0, 0);

  return ret;
}

.short_guidObject



111
112
113
114
115
116
117
118
119
120
121
# File 'ext/lib_uuid.c', line 111

VALUE
lib_uuid_generate_short_guid(VALUE class)
{
  char   uu_str[SHORT_GUID_STRLEN];
  uuid_t uu;

  uuid_generate(uu);
  uuid_encode64(uu, uu_str);

  return rb_str_new(uu_str, SHORT_GUID_STRLEN-1);
}

.valid?(*args) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'ext/lib_uuid.c', line 80

VALUE
lib_uuid_valid(unsigned int argc, VALUE *argv, VALUE class)
{
  int   i;
  VALUE test[1];

  if( argc == 0 )
    rb_raise(rb_eArgError, "too few arguments");

  for( i=0 ; i<argc ; i++ )
  {
    test[0] = argv[i];
    if( lib_uuid_new(1, test, class) == Qnil )
      return Qfalse;
  }

  return Qtrue;
}

Instance Method Details

#==(other) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/lib_uuid.c', line 201

VALUE
lib_uuid_equality(VALUE self, VALUE other)
{
  lib_uuid_t *s, *o;

  if( TYPE(self) != T_DATA || TYPE(other) != T_DATA )
    return Qfalse;

  Data_Get_Struct(self, lib_uuid_t, s);
  Data_Get_Struct(other, lib_uuid_t, o);

  if( uuid_compare(s->uu, o->uu) != 0 )
    return Qfalse;

  return Qtrue;
}

#bytesObject



135
136
137
138
139
# File 'ext/lib_uuid.c', line 135

VALUE
lib_uuid_bytes(VALUE self)
{
  return rb_iv_get(self, "@bytes");
}

#to_guidObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/lib_uuid.c', line 167

VALUE
lib_uuid_to_guid(VALUE self)
{
  lib_uuid_t *s;
  char       uu_str[GUID_STRLEN];

  Data_Get_Struct(self, lib_uuid_t, s);

  if( s->guid == 0 )
  {
    uuid_unparse(s->uu, uu_str);
    s->guid = rb_str_new(uu_str, GUID_STRLEN-1);
  }

  return s->guid;
}

#to_short_guidObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/lib_uuid.c', line 184

VALUE
lib_uuid_to_short_guid(VALUE self)
{
  lib_uuid_t *s;
  char       uu_str[SHORT_GUID_STRLEN];

  Data_Get_Struct(self, lib_uuid_t, s);

  if( s->short_guid == 0 )
  {
    uuid_encode64(s->uu, uu_str);
    s->short_guid = rb_str_new(uu_str, SHORT_GUID_STRLEN-1);
  }

  return s->short_guid;
}

#typeObject



141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/lib_uuid.c', line 141

VALUE
lib_uuid_type(VALUE self)
{
  lib_uuid_t *s;

  Data_Get_Struct(self, lib_uuid_t, s);

  if( s->type == 0 )
    s->type = INT2NUM(uuid_type(s->uu));

  return s->type;
}

#variantObject



154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/lib_uuid.c', line 154

VALUE
lib_uuid_variant(VALUE self)
{
  lib_uuid_t *s;

  Data_Get_Struct(self, lib_uuid_t, s);

  if( s->variant == 0 )
    s->variant = INT2NUM(uuid_variant(s->uu));

  return s->variant;
}