Module: Rethinker::Document::Selection::ClassMethods

Defined in:
lib/rethinker/document/selection.rb

Instance Method Summary collapse

Instance Method Details

#allObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rethinker/document/selection.rb', line 9

def all
  sel = Rethinker::Selection.new(Rethinker::Criterion.new(:table, table_name), :klass => self)

  unless is_root_class?
    # TODO use this: sel = sel.where(:_type.in(descendants_type_values))
    sel = sel.where do |doc|
      doc.has_fields(:_type) &
      descendants_type_values.map    { |type| doc[:_type].eq(type) }
                             .reduce { |a,b| a | b }
    end
  end

  sel
end

#find(id) ⇒ Object

XXX this doesn’t have the same semantics as other ORMs. the equivalent is find!.



39
40
41
# File 'lib/rethinker/document/selection.rb', line 39

def find(id)
  new_from_db(selector_for(id).run)
end

#find!(id) ⇒ Object



43
44
45
46
47
# File 'lib/rethinker/document/selection.rb', line 43

def find!(id)
  find(id).tap do |doc|
    doc or raise Rethinker::Error::DocumentNotFound, "#{self.class} id #{id} not found"
  end
end

#scope(name, selection) ⇒ Object



24
25
26
27
28
# File 'lib/rethinker/document/selection.rb', line 24

def scope(name, selection)
  singleton_class.class_eval do
    define_method(name) { |*args| selection.call(*args) }
  end
end

#selector_for(id) ⇒ Object



32
33
34
35
# File 'lib/rethinker/document/selection.rb', line 32

def selector_for(id)
  # TODO Pass primary key if not default
  Rethinker::Selection.new([Rethinker::Criterion.new(:table, table_name), Rethinker::Criterion.new(:get, id)], :klass => self)
end