Class: Yerba::Collection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glob) ⇒ Collection

Returns a new instance of Collection.



7
8
9
# File 'lib/yerba/collection.rb', line 7

def initialize(glob)
  @glob = glob
end

Class Method Details

.find(*args) ⇒ Object

Collection.find(glob, path, condition: nil, select: nil)



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'ext/yerba/yerba.c', line 684

static VALUE collection_s_find(int argc, VALUE *argv, VALUE self) {
  (void)self;
  VALUE pattern, path, opts;
  rb_scan_args(argc, argv, "2:", &pattern, &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);
  }

  YerbaTypedList result = yerba_glob_find(StringValueCStr(pattern), StringValueCStr(path), condition, select);

  if (!result.json) return rb_ary_new();

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

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

.get(pattern, path) ⇒ Object

Collection.get(glob, path) — get values across files



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'ext/yerba/yerba.c', line 651

static VALUE collection_s_get(VALUE self, VALUE pattern, VALUE path) {
  (void)self;
  YerbaTypedList result = yerba_glob_get(StringValueCStr(pattern), StringValueCStr(path));

  if (!result.json) return rb_ary_new();

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

  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;
}

Instance Method Details

#apply!Object



23
24
25
26
27
28
29
# File 'lib/yerba/collection.rb', line 23

def apply!
  each do |document|
    yield document

    document.save!
  end
end

#eachObject



11
12
13
# File 'lib/yerba/collection.rb', line 11

def each
  Dir.glob(@glob).each { |path| yield Document.new(path) }
end

#find(path, condition: nil, select: nil) ⇒ Object



19
20
21
# File 'lib/yerba/collection.rb', line 19

def find(path, condition: nil, select: nil)
  self.class.find(@glob, path, condition: condition, select: select)
end

#get(path) ⇒ Object



15
16
17
# File 'lib/yerba/collection.rb', line 15

def get(path)
  self.class.get(@glob, path)
end