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)



1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'ext/yerba/yerba.c', line 1077

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

  check_condition(condition, true);

  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, selector) → [Yerba::Scalar|Map|Sequence, ...]



1063
1064
1065
1066
1067
# File 'ext/yerba/yerba.c', line 1063

static VALUE collection_s_get(VALUE self, VALUE pattern, VALUE path) {
  (void) self;

  return located_list_to_ruby(yerba_glob_get(StringValueCStr(pattern), StringValueCStr(path)), Qnil);
}

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