Class: Yerba::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/yerba/document.rb,
ext/yerba/yerba.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Object

Document.new(path)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/yerba/yerba.c', line 113

static VALUE document_initialize(VALUE self, VALUE path) {
  const char *file_path = StringValueCStr(path);
  YerbaParseResult result = yerba_document_parse_file(file_path);

  if (!result.document) {
    VALUE message = make_utf8_string(result.error);
    yerba_string_free(result.error);

    rb_raise(rb_eParseError, "%s", StringValueCStr(message));
  }

  RTYPEDDATA_DATA(self) = result.document;
  rb_iv_set(self, "@path", path);

  return self;
}

Class Method Details

.parse(content) ⇒ Object

Document.parse(content)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'ext/yerba/yerba.c', line 131

static VALUE document_s_parse(VALUE klass, VALUE content) {
  const char *yaml_content = StringValueCStr(content);
  YerbaParseResult result = yerba_document_parse(yaml_content);

  if (!result.document) {
    VALUE message = make_utf8_string(result.error);
    yerba_string_free(result.error);

    rb_raise(rb_eParseError, "%s", StringValueCStr(message));
  }

  VALUE instance = document_alloc(klass);
  RTYPEDDATA_DATA(instance) = result.document;
  rb_iv_set(instance, "@path", Qnil);

  return instance;
}

Instance Method Details

#[](path) ⇒ Object

document[](path) → Yerba::Scalar, Yerba::Map, Yerba::Sequence, or nil



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'ext/yerba/yerba.c', line 210

static VALUE document_bracket(VALUE self, VALUE path) {
  struct Document *document = get_document(self);
  YerbaGetResult result = yerba_document_get(document, StringValueCStr(path));

  if (result.error) {
    VALUE message = make_utf8_string(result.error);
    yerba_get_result_free(result);

    rb_raise(rb_ePathValidationError, "%s", StringValueCStr(message));
  }

  VALUE instance;
  VALUE location = location_to_ruby(result.location);
  VALUE key = Qnil;

  if (result.key_name) {
    VALUE key_location = location_to_ruby(result.key_location);
    VALUE key_value = make_utf8_string(result.key_name);

    key = rb_funcall(rb_path2class("Yerba::Scalar"), rb_intern("new"), 4, Qnil, Qnil, key_value, key_location);
  }

  switch (result.node_type) {
    case YERBA_NODE_TYPE_SCALAR: {
      VALUE klass = rb_path2class("Yerba::Scalar");
      VALUE value = typed_value_to_ruby(result.single);
      yerba_get_result_free(result);

      instance = rb_funcall(klass, rb_intern("new"), 5, self, path, value, location, key);

      return instance;
    }

    case YERBA_NODE_TYPE_MAP: {
      yerba_get_result_free(result);
      VALUE klass = rb_path2class("Yerba::Map");

      instance = rb_funcall(klass, rb_intern("new"), 4, self, path, location, key);

      return instance;
    }

    case YERBA_NODE_TYPE_SEQUENCE: {
      yerba_get_result_free(result);
      VALUE klass = rb_path2class("Yerba::Sequence");

      instance = rb_funcall(klass, rb_intern("new"), 4, self, path, location, key);

      return instance;
    }

    default:
      yerba_get_result_free(result);

      return Qnil;
  }
}

#at_path(path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/yerba/document.rb', line 35

def at_path(path)
  if path.include?("[]")
    values = get(path)
    return [] unless values.is_a?(Array)

    path.sub("[]", "")

    values.each_with_index.map do |_value, index|
      resolved_path = path.sub("[]", "[#{index}]")
      self[resolved_path]
    end
  else
    self[path]
  end
end

#blank_lines(path, count) ⇒ Object

document.blank_lines(path, count)



606
607
608
609
610
611
612
613
# File 'ext/yerba/yerba.c', line 606

static VALUE document_blank_lines(VALUE self, VALUE path, VALUE count) {
  struct Document *document = get_document(self);
  YerbaResult result = yerba_document_blank_lines(document, StringValueCStr(path), NUM2SIZET(count));

  check_result(result);

  return self;
}

#changed?Boolean

document.changed?

Returns:

  • (Boolean)


642
643
644
645
646
647
648
649
650
# File 'ext/yerba/yerba.c', line 642

static VALUE document_changed_p(VALUE self) {
  VALUE path = rb_iv_get(self, "@path");
  if (NIL_P(path)) return Qtrue;

  VALUE current = document_to_s(self);
  VALUE original = rb_funcall(rb_cFile, rb_intern("read"), 1, path);

  return rb_str_equal(current, original) ? Qfalse : Qtrue;
}

#condition?(*args) ⇒ Boolean

document.condition?(condition, path: “”)

Returns:

  • (Boolean)


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'ext/yerba/yerba.c', line 337

static VALUE document_condition_p(int argc, VALUE *argv, VALUE self) {
  VALUE condition, opts;
  rb_scan_args(argc, argv, "1:", &condition, &opts);

  const char *parent_path = "";
  if (!NIL_P(opts)) {
    VALUE v_path = rb_hash_aref(opts, ID2SYM(rb_intern("path")));

    if (!NIL_P(v_path)) {
      parent_path = StringValueCStr(v_path);
    }
  }

  struct Document *document = get_document(self);

  return yerba_document_evaluate_condition(document, parent_path, StringValueCStr(condition)) ? Qtrue : Qfalse;
}

#delete(*args) ⇒ Object

document.delete(path, condition: nil)



492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'ext/yerba/yerba.c', line 492

static VALUE document_delete(int argc, VALUE *argv, VALUE self) {
  VALUE path, opts;
  rb_scan_args(argc, argv, "1:", &path, &opts);

  struct Document *document = get_document(self);
  if (!should_proceed(document, opts)) return self;

  YerbaResult result = yerba_document_delete(document, StringValueCStr(path));

  check_result(result);

  return self;
}

#dig(*keys) ⇒ Object



29
30
31
32
33
# File 'lib/yerba/document.rb', line 29

def dig(*keys)
  result = keys.reduce(self) { |node, key| node.nil? ? nil : node[key] }

  result&.value
end

#exists?(path) ⇒ Boolean

document.exists?(path)

Returns:

  • (Boolean)


269
270
271
272
273
# File 'ext/yerba/yerba.c', line 269

static VALUE document_exists_p(VALUE self, VALUE path) {
  struct Document *document = get_document(self);

  return yerba_document_exists(document, StringValueCStr(path)) ? Qtrue : Qfalse;
}

#find(*args) ⇒ Object

document.find(path, condition: nil, select: nil)



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'ext/yerba/yerba.c', line 356

static VALUE document_find(int argc, VALUE *argv, VALUE self) {
  VALUE path, opts;
  rb_scan_args(argc, argv, "1:", &path, &opts);

  const char *condition = NULL;
  const char *select = NULL;

  if (!NIL_P(opts)) {
    VALUE v_condition = rb_hash_aref(opts, ID2SYM(rb_intern("condition")));
    VALUE v_select = rb_hash_aref(opts, ID2SYM(rb_intern("select")));

    if (!NIL_P(v_condition)) condition = StringValueCStr(v_condition);
    if (!NIL_P(v_select)) select = StringValueCStr(v_select);
  }

  struct Document *document = get_document(self);
  char *json = yerba_document_find(document, StringValueCStr(path), condition, select);

  if (!json) return rb_ary_new();

  VALUE json_string = make_utf8_string(json);
  yerba_string_free(json);

  return rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
}

#get(path) ⇒ Object

document.get(path)



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/yerba/yerba.c', line 150

static VALUE document_get(VALUE self, VALUE path) {
  struct Document *document = get_document(self);
  YerbaGetResult result = yerba_document_get(document, StringValueCStr(path));

  if (result.error) {
    VALUE message = make_utf8_string(result.error);
    yerba_get_result_free(result);

    rb_raise(rb_ePathValidationError, "%s", StringValueCStr(message));
  }

  if (!result.is_list) {
    VALUE ruby_value = typed_value_to_ruby(result.single);
    yerba_get_result_free(result);

    return ruby_value;
  } else {
    VALUE json_string = make_utf8_string(result.list.json);
    yerba_get_result_free(result);

    VALUE items = rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
    long length = RARRAY_LEN(items);
    VALUE array = rb_ary_new_capa(length);

    for (long i = 0; i < length; i++) {
      VALUE item = rb_ary_entry(items, i);
      VALUE text = rb_hash_aref(item, rb_str_new_cstr("text"));
      int type_value = NUM2INT(rb_hash_aref(item, rb_str_new_cstr("type")));

      if (NIL_P(text)) {
        rb_ary_push(array, Qnil);

        continue;
      }

      YerbaTypedValue typed_value;
      typed_value.text = (char *)StringValueCStr(text);
      typed_value.value_type = (YerbaValueType)type_value;

      rb_ary_push(array, typed_value_to_ruby(typed_value));
    }

    return array;
  }
}

#get_quote_style(path) ⇒ Object

document.get_quote_style(path) → :plain, :single, :double, or nil



302
303
304
305
306
307
308
309
310
311
312
# File 'ext/yerba/yerba.c', line 302

static VALUE document_get_quote_style(VALUE self, VALUE path) {
  struct Document *document = get_document(self);
  int style = yerba_document_get_quote_style(document, StringValueCStr(path));

  switch (style) {
    case 0: return ID2SYM(rb_intern("plain"));
    case 1: return ID2SYM(rb_intern("single"));
    case 2: return ID2SYM(rb_intern("double"));
    default: return Qnil;
  }
}

#get_value(path) ⇒ Object

document.get_value(path) → parsed Ruby object (Hash/Array/String/Integer/etc)



276
277
278
279
280
281
282
283
284
285
286
# File 'ext/yerba/yerba.c', line 276

static VALUE document_get_value(VALUE self, VALUE path) {
  struct Document *document = get_document(self);
  char *json = yerba_document_get_value(document, StringValueCStr(path));

  if (!json) return Qnil;

  VALUE json_string = make_utf8_string(json);
  yerba_string_free(json);

  return rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
}

#get_values(path) ⇒ Object

document.get_values(path) → Array of parsed Ruby objects



289
290
291
292
293
294
295
296
297
298
299
# File 'ext/yerba/yerba.c', line 289

static VALUE document_get_values(VALUE self, VALUE path) {
  struct Document *document = get_document(self);
  char *json = yerba_document_get_values(document, StringValueCStr(path));

  if (!json) return rb_ary_new();

  VALUE json_string = make_utf8_string(json);
  yerba_string_free(json);

  return rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
}

#insert(*args) ⇒ Object

document.insert(path, value, before: nil, after: nil, at: nil)



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'ext/yerba/yerba.c', line 439

static VALUE document_insert(int argc, VALUE *argv, VALUE self) {
  VALUE path, value, opts;
  rb_scan_args(argc, argv, "2:", &path, &value, &opts);

  const char *before = NULL;
  const char *after = NULL;
  long long at = -1;

  if (!NIL_P(opts)) {
    VALUE v_before = rb_hash_aref(opts, ID2SYM(rb_intern("before")));
    VALUE v_after = rb_hash_aref(opts, ID2SYM(rb_intern("after")));
    VALUE v_at = rb_hash_aref(opts, ID2SYM(rb_intern("at")));

    if (!NIL_P(v_before)) before = StringValueCStr(v_before);
    if (!NIL_P(v_after)) after = StringValueCStr(v_after);
    if (!NIL_P(v_at)) at = NUM2LL(v_at);
  }

  struct Document *document = get_document(self);
  YerbaResult result = yerba_document_insert(document, StringValueCStr(path), StringValueCStr(value), before, after, at);
  check_result(result);

  return self;
}

#insert_object(*args) ⇒ Object

document.insert_object(path, object, before: nil, after: nil, at: nil)



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'ext/yerba/yerba.c', line 465

static VALUE document_insert_object(int argc, VALUE *argv, VALUE self) {
  VALUE path, object, opts;
  rb_scan_args(argc, argv, "2:", &path, &object, &opts);

  const char *before = NULL;
  const char *after = NULL;
  long long at = -1;

  if (!NIL_P(opts)) {
    VALUE v_before = rb_hash_aref(opts, ID2SYM(rb_intern("before")));
    VALUE v_after = rb_hash_aref(opts, ID2SYM(rb_intern("after")));
    VALUE v_at = rb_hash_aref(opts, ID2SYM(rb_intern("at")));

    if (!NIL_P(v_before)) before = StringValueCStr(v_before);
    if (!NIL_P(v_after)) after = StringValueCStr(v_after);
    if (!NIL_P(v_at)) at = NUM2LL(v_at);
  }

  struct Document *document = get_document(self);
  VALUE json_string = rb_funcall(rb_path2class("JSON"), rb_intern("generate"), 1, object);

  YerbaResult result = yerba_document_insert_object(document, StringValueCStr(path), StringValueCStr(json_string), before, after, at);
  check_result(result);
  return self;
}

#inspectObject



51
52
53
54
55
56
57
# File 'lib/yerba/document.rb', line 51

def inspect
  if path
    "#<Yerba::Document path=#{path.inspect}>"
  else
    "#<Yerba::Document (parsed)>"
  end
end

#map?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/yerba/document.rb', line 9

def map?
  root.is_a?(Map)
end

#pathObject

document.path



653
654
655
# File 'ext/yerba/yerba.c', line 653

static VALUE document_path(VALUE self) {
  return rb_iv_get(self, "@path");
}

#quote_style(*args) ⇒ Object

document.quote_style(path: nil, key_style: nil, value_style: nil)



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'ext/yerba/yerba.c', line 579

static VALUE document_quote_style(int argc, VALUE *argv, VALUE self) {
  VALUE opts;
  rb_scan_args(argc, argv, ":", &opts);

  const char *path = NULL;
  const char *key_style = NULL;
  const char *value_style = NULL;

  if (!NIL_P(opts)) {
    VALUE v_path = rb_hash_aref(opts, ID2SYM(rb_intern("path")));
    VALUE v_key_style = rb_hash_aref(opts, ID2SYM(rb_intern("key_style")));
    VALUE v_value_style = rb_hash_aref(opts, ID2SYM(rb_intern("value_style")));

    if (!NIL_P(v_path)) path = StringValueCStr(v_path);
    if (!NIL_P(v_key_style)) key_style = StringValueCStr(v_key_style);
    if (!NIL_P(v_value_style)) value_style = StringValueCStr(v_value_style);
  }

  struct Document *document = get_document(self);
  YerbaResult result = yerba_document_quote_style(document, path, key_style, value_style);

  check_result(result);

  return self;
}

#remove(path, value) ⇒ Object

document.remove(path, value)



507
508
509
510
511
512
513
514
# File 'ext/yerba/yerba.c', line 507

static VALUE document_remove(VALUE self, VALUE path, VALUE value) {
  struct Document *document = get_document(self);
  YerbaResult result = yerba_document_remove(document, StringValueCStr(path), StringValueCStr(value));

  check_result(result);

  return self;
}

#remove_at(path, index) ⇒ Object

document.remove_at(path, index)



517
518
519
520
521
522
523
524
# File 'ext/yerba/yerba.c', line 517

static VALUE document_remove_at(VALUE self, VALUE path, VALUE index) {
  struct Document *document = get_document(self);
  YerbaResult result = yerba_document_remove_at(document, StringValueCStr(path), NUM2SIZET(index));

  check_result(result);

  return self;
}

#rename(source, destination) ⇒ Object

document.rename(source, destination)



527
528
529
530
531
532
533
534
# File 'ext/yerba/yerba.c', line 527

static VALUE document_rename(VALUE self, VALUE source, VALUE destination) {
  struct Document *document = get_document(self);
  YerbaResult result = yerba_document_rename(document, StringValueCStr(source), StringValueCStr(destination));

  check_result(result);

  return self;
}

#rootObject



5
6
7
# File 'lib/yerba/document.rb', line 5

def root
  self[""]
end

#save!Object

document.save!



627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'ext/yerba/yerba.c', line 627

static VALUE document_save(VALUE self) {
  VALUE path = rb_iv_get(self, "@path");

  if (NIL_P(path)) {
    rb_raise(rb_eError, "Cannot save: document has no file path");
  }

  VALUE content = document_to_s(self);

  rb_funcall(rb_cFile, rb_intern("write"), 2, path, content);

  return self;
}

#sequence?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/yerba/document.rb', line 13

def sequence?
  root.is_a?(Sequence)
end

#set(*args) ⇒ Object

document.set(path, value, condition: nil, if_exists: false, if_missing: false)



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'ext/yerba/yerba.c', line 383

static VALUE document_set(int argc, VALUE *argv, VALUE self) {
  VALUE path, value, opts;
  rb_scan_args(argc, argv, "2:", &path, &value, &opts);

  struct Document *document = get_document(self);

  if (!should_proceed(document, opts)) return self;

  if (!NIL_P(opts)) {
    VALUE v_if_exists = rb_hash_aref(opts, ID2SYM(rb_intern("if_exists")));
    VALUE v_if_missing = rb_hash_aref(opts, ID2SYM(rb_intern("if_missing")));

    if (RTEST(v_if_exists) && !yerba_document_exists(document, StringValueCStr(path))) return self;
    if (RTEST(v_if_missing) && yerba_document_exists(document, StringValueCStr(path))) return self;
  }

  const char *c_value;
  YerbaValueType value_type;
  char number_buffer[64];
  bool all = false;

  if (value == Qnil) {
    c_value = "null";
    value_type = YERBA_VALUE_TYPE_NULL;
  } else if (value == Qtrue) {
    c_value = "true";
    value_type = YERBA_VALUE_TYPE_BOOLEAN;
  } else if (value == Qfalse) {
    c_value = "false";
    value_type = YERBA_VALUE_TYPE_BOOLEAN;
  } else if (RB_INTEGER_TYPE_P(value)) {
    snprintf(number_buffer, sizeof(number_buffer), "%ld", NUM2LONG(value));
    c_value = number_buffer;
    value_type = YERBA_VALUE_TYPE_INTEGER;
  } else if (RB_FLOAT_TYPE_P(value)) {
    snprintf(number_buffer, sizeof(number_buffer), "%g", NUM2DBL(value));
    c_value = number_buffer;
    value_type = YERBA_VALUE_TYPE_FLOAT;
  } else {
    c_value = StringValueCStr(value);
    value_type = YERBA_VALUE_TYPE_STRING;
  }

  if (!NIL_P(opts)) {
    VALUE v_all = rb_hash_aref(opts, ID2SYM(rb_intern("all")));

    if (RTEST(v_all)) all = true;
  }

  YerbaResult result = yerba_document_set(document, StringValueCStr(path), c_value, value_type, all);
  check_result(result);

  return self;
}

#set_quote_style(path, style) ⇒ Object

document.set_quote_style(path, style)



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'ext/yerba/yerba.c', line 315

static VALUE document_set_quote_style(VALUE self, VALUE path, VALUE style) {
  struct Document *document = get_document(self);

  int style_int;
  if (RB_TYPE_P(style, T_SYMBOL)) {
    ID style_id = SYM2ID(style);
    if (style_id == rb_intern("plain")) style_int = 0;
    else if (style_id == rb_intern("single")) style_int = 1;
    else if (style_id == rb_intern("double")) style_int = 2;
    else rb_raise(rb_eError, "Invalid quote style (use :plain, :single, or :double)");
  } else {
    style_int = NUM2INT(style);
  }

  YerbaResult result = yerba_document_set_quote_style(document, StringValueCStr(path), style_int);

  check_result(result);

  return self;
}

#sort(*args) ⇒ Object

document.sort(path, by: nil, case_sensitive: false)



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'ext/yerba/yerba.c', line 537

static VALUE document_sort(int argc, VALUE *argv, VALUE self) {
  VALUE path, opts;
  rb_scan_args(argc, argv, "1:", &path, &opts);

  const char *by = NULL;
  bool case_sensitive = false;

  if (!NIL_P(opts)) {
    VALUE v_by = rb_hash_aref(opts, ID2SYM(rb_intern("by")));
    VALUE v_case_sensitive = rb_hash_aref(opts, ID2SYM(rb_intern("case_sensitive")));

    if (!NIL_P(v_by)) by = StringValueCStr(v_by);
    if (RTEST(v_case_sensitive)) case_sensitive = true;
  }

  struct Document *document = get_document(self);
  YerbaResult result = yerba_document_sort(document, StringValueCStr(path), by, case_sensitive);

  check_result(result);

  return self;
}

#sort_keys(path, order) ⇒ Object

document.sort_keys(path, order)



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'ext/yerba/yerba.c', line 561

static VALUE document_sort_keys(VALUE self, VALUE path, VALUE order) {
  struct Document *document = get_document(self);

  VALUE order_string;
  if (RB_TYPE_P(order, T_ARRAY)) {
    order_string = rb_ary_join(order, rb_str_new_cstr(","));
  } else {
    order_string = order;
  }

  YerbaResult result = yerba_document_sort_keys(document, StringValueCStr(path), StringValueCStr(order_string));

  check_result(result);

  return self;
}

#to_aObject



21
22
23
# File 'lib/yerba/document.rb', line 21

def to_a
  get_value("")
end

#to_hObject



17
18
19
# File 'lib/yerba/document.rb', line 17

def to_h
  get_value("")
end

#to_sObject

document.to_s



616
617
618
619
620
621
622
623
624
# File 'ext/yerba/yerba.c', line 616

static VALUE document_to_s(VALUE self) {
  struct Document *document = get_document(self);
  char *content = yerba_document_to_string(document);
  VALUE string = make_utf8_string(content);

  yerba_string_free(content);

  return string;
}

#to_yamlObject



25
26
27
# File 'lib/yerba/document.rb', line 25

def to_yaml
  to_s
end