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)



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
# File 'ext/yerba/yerba.c', line 778

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



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'ext/yerba/yerba.c', line 745

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



58
59
60
61
62
63
64
# File 'lib/yerba/collection.rb', line 58

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

#find_byObject



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

def find_by(...)
  each do |document|
    next unless document.sequence?

    result = document.root.find_by(...)
    return result if result
  end

  nil
end

#get(path) ⇒ Object



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

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

#pluckObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yerba/collection.rb', line 46

def pluck(...)
  results = []

  each do |document|
    next unless document.sequence?

    results.concat(document.root.pluck(...))
  end

  results
end

#whereObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/yerba/collection.rb', line 34

def where(...)
  results = []

  each do |document|
    next unless document.sequence?

    results.concat(document.root.where(...).to_a)
  end

  results
end