Class: Yerba::Document
- Inherits:
-
Object
- Object
- Yerba::Document
- Defined in:
- lib/yerba/document.rb,
ext/yerba/yerba.c
Constant Summary collapse
- ROOT_SELECTOR =
""
Class Method Summary collapse
- .cache ⇒ Object
- .clear_cache! ⇒ Object
- .from(object, path: nil) ⇒ Object
-
.parse(content) ⇒ Object
Document.parse(content).
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #[](path) ⇒ Object
- #[]=(key, value) ⇒ Object
- #apply(yerbafile = nil) ⇒ Object
- #apply!(yerbafile = nil) ⇒ Object
-
#apply_yerbafile(*args) ⇒ Object
document.apply_yerbafile(yerbafile_path = nil).
-
#blank_lines(path, count) ⇒ Object
document.blank_lines(path, count).
-
#changed? ⇒ Boolean
document.changed?.
- #concat(items) ⇒ Object
-
#condition?(*args) ⇒ Boolean
document.condition?(condition, path: "").
-
#delete(*args) ⇒ Object
document.delete(path, condition: nil).
- #dig(*keys) ⇒ Object
-
#exists?(path) ⇒ Boolean
document.exists?(path).
- #fetch(selector) ⇒ Object
-
#find(*args) ⇒ Object
document.find(path, condition: nil, select: nil).
- #find_by ⇒ Object
-
#get_all(path) ⇒ Object
document.get_all(selector) → [Yerba::Scalar|Map|Sequence, ...].
-
#get_collection_style(path) ⇒ Object
document.get_collection_style(path) → :flow, :block, or nil.
-
#get_quote_style(path) ⇒ Object
document.get_quote_style(path) → :plain, :single, :double, :literal, etc.
-
#get_sequence_indent(path) ⇒ Object
document.get_sequence_indent(path) → :compact, :indented, or nil.
-
#initialize(*args) ⇒ Object
constructor
Document.new(path).
-
#insert(*args) ⇒ Object
document.insert(path, value, before: nil, after: nil, at: nil).
-
#insert_object(*args) ⇒ Object
document.insert_object(path, object, before: nil, after: nil, at: nil).
-
#insert_objects(path, array) ⇒ Object
document.insert_objects(path, array).
- #inspect ⇒ Object
-
#keys_at(path) ⇒ Object
document.keys_at(selector) → ["host", "port", ...].
-
#location(*args) ⇒ Object
document.location(selector = nil) → Yerba::Location.
-
#locations(path) ⇒ Object
document.locations(selector) → [Yerba::Location, ...].
- #map? ⇒ Boolean
- #node_at(path) ⇒ Object
-
#path ⇒ Object
document.path.
- #pluck ⇒ Object
-
#quote_style(*args) ⇒ Object
document.quote_style(path: nil, key_style: nil, value_style: nil).
-
#remove(path, value) ⇒ Object
document.remove(path, value).
-
#remove_at(path, index) ⇒ Object
document.remove_at(path, index).
-
#rename(source, destination) ⇒ Object
document.rename(source, destination).
-
#replace_content!(content) ⇒ Object
document.replace_content!(content) — re-parse from YAML string, keeping the same Ruby object.
-
#resolve_selectors(path) ⇒ Object
document.resolve_selectors(path) → ["[0].speakers[0]", "[0].speakers[1]", ...].
- #root ⇒ Object
- #root=(value) ⇒ Object
- #save!(apply: false) ⇒ Object
- #save_to!(path) ⇒ Object
- #selector ⇒ Object
-
#selectors ⇒ Object
document.selectors → ["database", "database.host", "tags", "tags[]", ...].
- #sequence? ⇒ Boolean
-
#set(*args) ⇒ Object
document.set(path, value, condition: nil, if_exists: false, if_missing: false).
-
#set_collection_style(path, style) ⇒ Object
document.set_collection_style(path, style).
-
#set_quote_style(path, style) ⇒ Object
document.set_quote_style(path, style).
-
#set_sequence_indent(path, style) ⇒ Object
document.set_sequence_indent(path, style).
-
#sort(*args) ⇒ Object
document.sort(path = "", by: nil, order: nil, case_sensitive: false).
-
#sort_keys(path, order) ⇒ Object
document.sort_keys(path, order).
-
#source(path) ⇒ Object
document.source(path) → raw YAML text of the node at path.
- #stale? ⇒ Boolean
- #to_a ⇒ Object
- #to_h ⇒ Object
-
#to_s ⇒ Object
document.to_s.
- #to_yaml ⇒ Object
- #valid?(schema, selector: nil) ⇒ Boolean
-
#valid_selector?(path) ⇒ Boolean
document.valid_selector?(selector) → true/false.
- #validate(schema, selector: nil) ⇒ Object
-
#validate_schema(*args) ⇒ Object
document.validate_schema(schema_json, selector = nil).
- #validate_selector!(selector) ⇒ Object
-
#value_at(path) ⇒ Object
document.get_value(path) → parsed Ruby object (Hash/Array/String/Integer/etc).
- #where ⇒ Object
-
#write! ⇒ Object
document.save!.
Constructor Details
#initialize(*args) ⇒ Object
Document.new(path)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'ext/yerba/yerba.c', line 128
static VALUE document_initialize(int argc, VALUE *argv, VALUE self) {
VALUE path;
rb_scan_args(argc, argv, "01", &path);
YerbaParseResult result;
if (NIL_P(path)) {
result = yerba_document_parse("---\n");
rb_iv_set(self, "@path", Qnil);
rb_iv_set(self, "@loaded_mtime", Qnil);
} else {
const char *file_path = StringValueCStr(path);
result = yerba_document_parse_file(file_path);
rb_iv_set(self, "@path", path);
rb_iv_set(self, "@loaded_mtime", rb_funcall(rb_cFile, rb_intern("mtime"), 1, 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;
return self;
}
|
Class Method Details
.cache ⇒ Object
7 8 9 |
# File 'lib/yerba/document.rb', line 7 def self.cache @cache ||= Hash.new { |hash, path| hash[path] = new(path) } end |
.clear_cache! ⇒ Object
11 12 13 |
# File 'lib/yerba/document.rb', line 11 def self.clear_cache! @cache = nil end |
.from(object, path: nil) ⇒ Object
15 16 17 18 19 |
# File 'lib/yerba/document.rb', line 15 def self.from(object, path: nil) document = parse(Formatting.to_yaml_document(object)) document.instance_variable_set(:@path, path) if path document end |
.parse(content) ⇒ Object
Document.parse(content)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'ext/yerba/yerba.c', line 181
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
#<<(item) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/yerba/document.rb', line 107 def <<(item) if root.is_a?(Scalar) raise Error, "document root is not set. Use `document.root = []` first" end if root.is_a?(Map) raise Error, "document root is a Map, not a Sequence. Use `document[\"key\"] = value` to set keys, or `document.root = []` to switch to a Sequence" end root << item end |
#[](path) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'ext/yerba/yerba.c', line 263
static VALUE document_bracket(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char index_buffer[32];
if (RB_TYPE_P(path, T_FIXNUM)) {
snprintf(index_buffer, sizeof(index_buffer), "[%ld]", FIX2LONG(path));
path = rb_str_new_cstr(index_buffer);
}
const char *selector = StringValueCStr(path);
if (yerba_selector_has_wildcard(selector)) {
char *json = yerba_document_resolve_selectors(document, selector);
if (!json) return rb_ary_new();
VALUE json_string = make_utf8_string(json);
yerba_string_free(json);
VALUE resolved = rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
long length = RARRAY_LEN(resolved);
VALUE array = rb_ary_new_capa(length);
for (long i = 0; i < length; i++) {
VALUE concrete_path = rb_ary_entry(resolved, i);
YerbaGetResult result = yerba_document_get(document, StringValueCStr(concrete_path));
VALUE node = node_from_get_result(result, self, concrete_path);
if (!NIL_P(node)) rb_ary_push(array, node);
}
return array;
}
YerbaGetResult result = yerba_document_get(document, selector);
return node_from_get_result(result, self, path);
}
|
#[]=(key, value) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/yerba/document.rb', line 33 def []=(key, value) if root.is_a?(Scalar) raise Error, "document root is not set. Use `document.root = {}` or `document.root = []` first" end if root.is_a?(Sequence) raise Error, "document root is a Sequence, not a Map. Use `document << item` to append, or `document.root = {}` to switch to a Map" end root[key] = value end |
#apply(yerbafile = nil) ⇒ Object
140 141 142 143 144 |
# File 'lib/yerba/document.rb', line 140 def apply(yerbafile = nil) Yerbafile.apply!(self, yerbafile) self end |
#apply!(yerbafile = nil) ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/yerba/document.rb', line 131 def apply!(yerbafile = nil) check_stale! apply(yerbafile) write! if changed? @loaded_mtime = File.mtime(@path) if @path self end |
#apply_yerbafile(*args) ⇒ Object
document.apply_yerbafile(yerbafile_path = nil)
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 |
# File 'ext/yerba/yerba.c', line 892
static VALUE document_apply_yerbafile(int argc, VALUE *argv, VALUE self) {
VALUE yerbafile_path;
rb_scan_args(argc, argv, "01", &yerbafile_path);
struct Document *document = get_document(self);
VALUE file_path = rb_iv_get(self, "@path");
const char *file_path_str = NIL_P(file_path) ? "" : StringValueCStr(file_path);
const char *yerbafile_path_str = NIL_P(yerbafile_path) ? NULL : StringValueCStr(yerbafile_path);
YerbaResult result = yerba_document_apply_yerbafile(document, file_path_str, yerbafile_path_str);
check_result(result);
return self;
}
|
#blank_lines(path, count) ⇒ Object
document.blank_lines(path, count)
882 883 884 885 886 887 888 889 |
# File 'ext/yerba/yerba.c', line 882
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?
953 954 955 956 957 958 959 960 961 |
# File 'ext/yerba/yerba.c', line 953
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;
}
|
#concat(items) ⇒ Object
119 120 121 |
# File 'lib/yerba/document.rb', line 119 def concat(items) root.concat(items) end |
#condition?(*args) ⇒ Boolean
document.condition?(condition, path: "")
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'ext/yerba/yerba.c', line 523
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);
check_condition(StringValueCStr(condition), parent_path[0] != '\0');
return yerba_document_evaluate_condition(document, parent_path, StringValueCStr(condition)) ? Qtrue : Qfalse;
}
|
#delete(*args) ⇒ Object
document.delete(path, condition: nil)
709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'ext/yerba/yerba.c', line 709
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
79 80 81 |
# File 'lib/yerba/document.rb', line 79 def dig(*keys) keys.reduce(self) { |node, key| node.nil? ? nil : node[key] } end |
#exists?(path) ⇒ Boolean
document.exists?(path)
303 304 305 306 307 |
# File 'ext/yerba/yerba.c', line 303
static VALUE document_exists_p(VALUE self, VALUE path) {
struct Document *document = get_document(self);
return yerba_document_exists(document, StringValueCStr(path)) ? Qtrue : Qfalse;
}
|
#fetch(selector) ⇒ Object
83 84 85 86 87 |
# File 'lib/yerba/document.rb', line 83 def fetch(selector) validate_selector!(selector) self[selector] end |
#find(*args) ⇒ Object
document.find(path, condition: nil, select: nil)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'ext/yerba/yerba.c', line 544
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);
check_condition(condition, true);
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);
}
|
#find_by ⇒ Object
89 90 91 92 93 |
# File 'lib/yerba/document.rb', line 89 def find_by(...) return nil if empty_root? root.find_by(...) end |
#get_all(path) ⇒ Object
document.get_all(selector) → [Yerba::Scalar|Map|Sequence, ...]
1070 1071 1072 1073 1074 |
# File 'ext/yerba/yerba.c', line 1070
static VALUE document_get_all(VALUE self, VALUE path) {
struct Document *document = get_document(self);
return located_list_to_ruby(yerba_document_get_all(document, StringValueCStr(path)), self);
}
|
#get_collection_style(path) ⇒ Object
document.get_collection_style(path) → :flow, :block, or nil
453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'ext/yerba/yerba.c', line 453
static VALUE document_get_collection_style(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char *style = yerba_document_get_collection_style(document, StringValueCStr(path));
if (style == NULL) return Qnil;
VALUE symbol = ID2SYM(rb_intern(style));
yerba_string_free(style);
return symbol;
}
|
#get_quote_style(path) ⇒ Object
document.get_quote_style(path) → :plain, :single, :double, :literal, etc. or nil
418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'ext/yerba/yerba.c', line 418
static VALUE document_get_quote_style(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char *style = yerba_document_get_quote_style(document, StringValueCStr(path));
if (style == NULL) return Qnil;
VALUE symbol = ID2SYM(rb_intern(style));
yerba_string_free(style);
return symbol;
}
|
#get_sequence_indent(path) ⇒ Object
document.get_sequence_indent(path) → :compact, :indented, or nil
488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'ext/yerba/yerba.c', line 488
static VALUE document_get_sequence_indent(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char *style = yerba_document_get_sequence_indent(document, StringValueCStr(path));
if (style == NULL) return Qnil;
VALUE symbol = ID2SYM(rb_intern(style));
yerba_string_free(style);
return symbol;
}
|
#insert(*args) ⇒ Object
document.insert(path, value, before: nil, after: nil, at: nil)
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
# File 'ext/yerba/yerba.c', line 642
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);
}
char number_buffer[64];
struct TypedValue typed_value = ruby_to_typed_value(value, number_buffer);
struct Document *document = get_document(self);
YerbaResult result = yerba_document_insert(document, StringValueCStr(path), typed_value.text, typed_value.type, before, after, at);
check_result(result);
return self;
}
|
#insert_object(*args) ⇒ Object
document.insert_object(path, object, before: nil, after: nil, at: nil)
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
# File 'ext/yerba/yerba.c', line 671
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;
}
|
#insert_objects(path, array) ⇒ Object
document.insert_objects(path, array)
698 699 700 701 702 703 704 705 706 |
# File 'ext/yerba/yerba.c', line 698
static VALUE document_insert_objects(VALUE self, VALUE path, VALUE array) {
struct Document *document = get_document(self);
VALUE json_string = rb_funcall(rb_path2class("JSON"), rb_intern("generate"), 1, array);
YerbaResult result = yerba_document_insert_objects(document, StringValueCStr(path), StringValueCStr(json_string));
check_result(result);
return self;
}
|
#inspect ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/yerba/document.rb', line 192 def inspect if path "#<Yerba::Document path=#{path.inspect}>" else "#<Yerba::Document (parsed)>" end end |
#keys_at(path) ⇒ Object
document.keys_at(selector) → ["host", "port", ...]
356 357 358 359 360 361 362 363 364 365 366 |
# File 'ext/yerba/yerba.c', line 356
static VALUE document_keys_at(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char *json = yerba_document_keys(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);
}
|
#location(*args) ⇒ Object
document.location(selector = nil) → Yerba::Location
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 |
# File 'ext/yerba/yerba.c', line 964
static VALUE document_location(int argc, VALUE *argv, VALUE self) {
VALUE path;
rb_scan_args(argc, argv, "01", &path);
struct Document *document = get_document(self);
const char *selector = NIL_P(path) ? "" : StringValueCStr(path);
YerbaGetResult result = yerba_document_get(document, selector);
if (result.error) {
yerba_get_result_free(result);
return Qnil;
}
if (result.location.start_line == 0 && result.location.end_line == 0 && strlen(selector) > 0) {
yerba_get_result_free(result);
return Qnil;
}
VALUE location = location_to_ruby(result.location);
yerba_get_result_free(result);
return location;
}
|
#locations(path) ⇒ Object
document.locations(selector) → [Yerba::Location, ...]
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'ext/yerba/yerba.c', line 369
static VALUE document_locations(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char *json = yerba_document_resolve_selectors(document, StringValueCStr(path));
if (!json) return rb_ary_new();
VALUE json_string = make_utf8_string(json);
yerba_string_free(json);
VALUE resolved = rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
long length = RARRAY_LEN(resolved);
VALUE array = rb_ary_new_capa(length);
for (long i = 0; i < length; i++) {
VALUE concrete_path = rb_ary_entry(resolved, i);
YerbaGetResult result = yerba_document_get(document, StringValueCStr(concrete_path));
if (result.error) {
yerba_get_result_free(result);
continue;
}
if (result.location.start_line == 0 && result.location.end_line == 0) {
yerba_get_result_free(result);
continue;
}
VALUE location = location_to_ruby(result.location);
yerba_get_result_free(result);
rb_ary_push(array, location);
}
return array;
}
|
#node_at(path) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'ext/yerba/yerba.c', line 263
static VALUE document_bracket(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char index_buffer[32];
if (RB_TYPE_P(path, T_FIXNUM)) {
snprintf(index_buffer, sizeof(index_buffer), "[%ld]", FIX2LONG(path));
path = rb_str_new_cstr(index_buffer);
}
const char *selector = StringValueCStr(path);
if (yerba_selector_has_wildcard(selector)) {
char *json = yerba_document_resolve_selectors(document, selector);
if (!json) return rb_ary_new();
VALUE json_string = make_utf8_string(json);
yerba_string_free(json);
VALUE resolved = rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
long length = RARRAY_LEN(resolved);
VALUE array = rb_ary_new_capa(length);
for (long i = 0; i < length; i++) {
VALUE concrete_path = rb_ary_entry(resolved, i);
YerbaGetResult result = yerba_document_get(document, StringValueCStr(concrete_path));
VALUE node = node_from_get_result(result, self, concrete_path);
if (!NIL_P(node)) rb_ary_push(array, node);
}
return array;
}
YerbaGetResult result = yerba_document_get(document, selector);
return node_from_get_result(result, self, path);
}
|
#path ⇒ Object
document.path
990 991 992 |
# File 'ext/yerba/yerba.c', line 990 static VALUE document_path(VALUE self) { return rb_iv_get(self, "@path"); } |
#pluck ⇒ Object
101 102 103 104 105 |
# File 'lib/yerba/document.rb', line 101 def pluck(...) return [] if empty_root? root.pluck(...) end |
#quote_style(*args) ⇒ Object
document.quote_style(path: nil, key_style: nil, value_style: nil)
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 |
# File 'ext/yerba/yerba.c', line 855
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)
724 725 726 727 728 729 730 731 |
# File 'ext/yerba/yerba.c', line 724
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)
734 735 736 737 738 739 740 741 |
# File 'ext/yerba/yerba.c', line 734
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)
744 745 746 747 748 749 750 751 |
# File 'ext/yerba/yerba.c', line 744
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;
}
|
#replace_content!(content) ⇒ Object
document.replace_content!(content) — re-parse from YAML string, keeping the same Ruby object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'ext/yerba/yerba.c', line 158
static VALUE document_replace_content(VALUE self, 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));
}
struct Document *old_document = get_document(self);
if (old_document) {
yerba_document_free(old_document);
}
RTYPEDDATA_DATA(self) = result.document;
return self;
}
|
#resolve_selectors(path) ⇒ Object
document.resolve_selectors(path) → ["[0].speakers[0]", "[0].speakers[1]", ...]
405 406 407 408 409 410 411 412 413 414 415 |
# File 'ext/yerba/yerba.c', line 405
static VALUE document_resolve_selectors(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char *json = yerba_document_resolve_selectors(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);
}
|
#root ⇒ Object
25 26 27 |
# File 'lib/yerba/document.rb', line 25 def root self[ROOT_SELECTOR] end |
#root=(value) ⇒ Object
29 30 31 |
# File 'lib/yerba/document.rb', line 29 def root=(value) replace_content!(Formatting.to_yaml_document(value)) end |
#save!(apply: false) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/yerba/document.rb', line 123 def save!(apply: false) check_stale! Yerbafile.apply!(self, apply) if apply write! @loaded_mtime = File.mtime(@path) if @path self end |
#save_to!(path) ⇒ Object
67 68 69 70 71 |
# File 'lib/yerba/document.rb', line 67 def save_to!(path) @path = path @loaded_mtime = nil save! end |
#selector ⇒ Object
21 22 23 |
# File 'lib/yerba/document.rb', line 21 def selector ROOT_SELECTOR end |
#selectors ⇒ Object
document.selectors → ["database", "database.host", "tags", "tags[]", ...]
343 344 345 346 347 348 349 350 351 352 353 |
# File 'ext/yerba/yerba.c', line 343
static VALUE document_selectors(VALUE self) {
struct Document *document = get_document(self);
char *json = yerba_document_selectors(document);
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);
}
|
#sequence? ⇒ Boolean
49 50 51 |
# File 'lib/yerba/document.rb', line 49 def sequence? root.is_a?(Sequence) end |
#set(*args) ⇒ Object
document.set(path, value, condition: nil, if_exists: false, if_missing: false)
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
# File 'ext/yerba/yerba.c', line 609
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;
}
char number_buffer[64];
struct TypedValue typed_value = ruby_to_typed_value(value, number_buffer);
bool all = false;
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), typed_value.text, typed_value.type, all);
check_result(result);
return self;
}
|
#set_collection_style(path, style) ⇒ Object
document.set_collection_style(path, style)
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'ext/yerba/yerba.c', line 467
static VALUE document_set_collection_style(VALUE self, VALUE path, VALUE style) {
struct Document *document = get_document(self);
const char *style_string;
if (RB_TYPE_P(style, T_SYMBOL)) {
style_string = rb_id2name(SYM2ID(style));
} else if (RB_TYPE_P(style, T_STRING)) {
style_string = StringValueCStr(style);
} else {
rb_raise(rb_eError, "Invalid collection style (expected Symbol or String)");
}
YerbaResult result = yerba_document_set_collection_style(document, StringValueCStr(path), style_string);
check_result(result);
return self;
}
|
#set_quote_style(path, style) ⇒ Object
document.set_quote_style(path, style)
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'ext/yerba/yerba.c', line 432
static VALUE document_set_quote_style(VALUE self, VALUE path, VALUE style) {
struct Document *document = get_document(self);
const char *style_string;
if (RB_TYPE_P(style, T_SYMBOL)) {
style_string = rb_id2name(SYM2ID(style));
} else if (RB_TYPE_P(style, T_STRING)) {
style_string = StringValueCStr(style);
} else {
rb_raise(rb_eError, "Invalid quote style (expected Symbol or String)");
}
YerbaResult result = yerba_document_set_quote_style(document, StringValueCStr(path), style_string);
check_result(result);
return self;
}
|
#set_sequence_indent(path, style) ⇒ Object
document.set_sequence_indent(path, style)
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'ext/yerba/yerba.c', line 502
static VALUE document_set_sequence_indent(VALUE self, VALUE path, VALUE style) {
struct Document *document = get_document(self);
const char *style_string;
if (RB_TYPE_P(style, T_SYMBOL)) {
style_string = rb_id2name(SYM2ID(style));
} else if (RB_TYPE_P(style, T_STRING)) {
style_string = StringValueCStr(style);
} else {
rb_raise(rb_eError, "Invalid sequence indent style (expected Symbol or String)");
}
YerbaResult result = yerba_document_set_sequence_indent(document, StringValueCStr(path), style_string);
check_result(result);
return self;
}
|
#sort(*args) ⇒ Object
document.sort(path = "", by: nil, order: nil, case_sensitive: false)
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |
# File 'ext/yerba/yerba.c', line 754
static VALUE document_sort(int argc, VALUE *argv, VALUE self) {
VALUE path, opts;
rb_scan_args(argc, argv, "01:", &path, &opts);
if (NIL_P(path)) path = rb_str_new_cstr("");
const char *by = NULL;
bool case_sensitive = false;
VALUE v_order = Qnil;
if (!NIL_P(opts)) {
VALUE v_by = rb_hash_aref(opts, ID2SYM(rb_intern("by")));
v_order = rb_hash_aref(opts, ID2SYM(rb_intern("order")));
VALUE v_case_sensitive = rb_hash_aref(opts, ID2SYM(rb_intern("case_sensitive")));
if (SYMBOL_P(v_by)) {
VALUE by_string = rb_sym2str(v_by);
by = StringValueCStr(by_string);
} else if (!NIL_P(v_by)) {
by = StringValueCStr(v_by);
}
if (RTEST(v_case_sensitive)) {
case_sensitive = true;
}
}
struct Document *document = get_document(self);
const char *path_string = StringValueCStr(path);
if (RB_TYPE_P(v_order, T_ARRAY)) {
VALUE order_csv = rb_ary_join(v_order, rb_str_new_cstr(","));
const char *order_string = StringValueCStr(order_csv);
const char *reorder_path = StringValueCStr(path);
const char *reorder_by;
if (by) {
VALUE reorder_by_value = rb_hash_aref(opts, ID2SYM(rb_intern("by")));
if (SYMBOL_P(reorder_by_value)) {
reorder_by_value = rb_sym2str(reorder_by_value);
}
reorder_by = StringValueCStr(reorder_by_value);
} else {
reorder_by = ".";
}
YerbaResult result = yerba_document_reorder(document, reorder_path, reorder_by, order_string);
check_result(result);
return self;
}
const char *order = NULL;
if (SYMBOL_P(v_order)) {
VALUE order_string = rb_sym2str(v_order);
order = StringValueCStr(order_string);
} else if (!NIL_P(v_order)) {
order = StringValueCStr(v_order);
}
VALUE by_with_order = Qnil;
if (order && strcmp(order, "desc") == 0) {
if (by) {
by_with_order = rb_sprintf("%s:desc", by);
} else {
by_with_order = rb_str_new_cstr(":desc");
}
by = StringValueCStr(by_with_order);
}
YerbaResult result = yerba_document_sort(document, path_string, by, case_sensitive);
check_result(result);
return self;
}
|
#sort_keys(path, order) ⇒ Object
document.sort_keys(path, order)
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
# File 'ext/yerba/yerba.c', line 837
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;
}
|
#source(path) ⇒ Object
document.source(path) → raw YAML text of the node at path
317 318 319 320 321 322 323 324 325 326 327 |
# File 'ext/yerba/yerba.c', line 317
static VALUE document_source(VALUE self, VALUE path) {
struct Document *document = get_document(self);
char *text = yerba_document_source(document, StringValueCStr(path));
if (!text) return Qnil;
VALUE result = make_utf8_string(text);
yerba_string_free(text);
return result;
}
|
#stale? ⇒ Boolean
73 74 75 76 77 |
# File 'lib/yerba/document.rb', line 73 def stale? return false unless @path && @loaded_mtime File.mtime(@path) != @loaded_mtime end |
#to_a ⇒ Object
58 59 60 61 |
# File 'lib/yerba/document.rb', line 58 def to_a value = value_at(ROOT_SELECTOR) value.nil? ? [] : value end |
#to_h ⇒ Object
53 54 55 56 |
# File 'lib/yerba/document.rb', line 53 def to_h value = value_at(ROOT_SELECTOR) value.nil? ? {} : value end |
#to_s ⇒ Object
document.to_s
927 928 929 930 931 932 933 934 935 |
# File 'ext/yerba/yerba.c', line 927 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_yaml ⇒ Object
63 64 65 |
# File 'lib/yerba/document.rb', line 63 def to_yaml to_s end |
#valid?(schema, selector: nil) ⇒ Boolean
146 147 148 149 150 |
# File 'lib/yerba/document.rb', line 146 def valid?(schema, selector: nil) errors = validate(schema, selector: selector) errors.empty? end |
#valid_selector?(path) ⇒ Boolean
document.valid_selector?(selector) → true/false
310 311 312 313 314 |
# File 'ext/yerba/yerba.c', line 310
static VALUE document_valid_selector_p(VALUE self, VALUE path) {
struct Document *document = get_document(self);
return yerba_document_valid_selector(document, StringValueCStr(path)) ? Qtrue : Qfalse;
}
|
#validate(schema, selector: nil) ⇒ Object
152 153 154 155 156 |
# File 'lib/yerba/document.rb', line 152 def validate(schema, selector: nil) schema_json = schema.is_a?(String) ? schema : JSON.generate(schema) validate_schema(schema_json, selector) end |
#validate_schema(*args) ⇒ Object
document.validate_schema(schema_json, selector = nil)
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 |
# File 'ext/yerba/yerba.c', line 909
static VALUE document_validate_schema(int argc, VALUE *argv, VALUE self) {
VALUE schema_json, selector;
rb_scan_args(argc, argv, "11", &schema_json, &selector);
struct Document *document = get_document(self);
const char *selector_str = NIL_P(selector) ? NULL : StringValueCStr(selector);
char *result = yerba_document_validate_schema(document, StringValueCStr(schema_json), selector_str);
if (!result) return rb_ary_new();
VALUE json_string = make_utf8_string(result);
yerba_string_free(result);
return rb_funcall(rb_path2class("JSON"), rb_intern("parse"), 1, json_string);
}
|
#validate_selector!(selector) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/yerba/document.rb', line 158 def validate_selector!(selector) return if valid_selector?(selector) available = selectors = "selector \"#{selector}\" is not valid for this document" if available.any? suggestions = DidYouMean::SpellChecker.new(dictionary: available).correct(selector) if suggestions.any? += ". Did you mean: #{suggestions.first(3).join(", ")}?" else += ". Available selectors: #{available.first(5).join(", ")}" += ", ..." if available.length > 5 end end raise Yerba::SelectorNotFoundError, end |
#value_at(path) ⇒ Object
document.get_value(path) → parsed Ruby object (Hash/Array/String/Integer/etc)
330 331 332 333 334 335 336 337 338 339 340 |
# File 'ext/yerba/yerba.c', line 330
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);
}
|
#where ⇒ Object
95 96 97 98 99 |
# File 'lib/yerba/document.rb', line 95 def where(...) return QueryResult.new(nil, []) if empty_root? root.where(...) end |
#write! ⇒ Object
document.save!
938 939 940 941 942 943 944 945 946 947 948 949 950 |
# File 'ext/yerba/yerba.c', line 938
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;
}
|