Module: MassiveRecord::ORM::Finders::ClassMethods

Defined in:
lib/massive_record/orm/finders.rb

Instance Method Summary collapse

Instance Method Details

#default_scope(scope) ⇒ Object

Sets a default scope which will be used for calls like find, first, all etc. Makes it possible to for instance set default column families to load on all calls to the database.



71
72
73
74
75
76
77
78
79
80
# File 'lib/massive_record/orm/finders.rb', line 71

def default_scope(scope)
  self.default_scoping =  case scope
                            when Scope, nil
                              scope
                            when Hash
                              Scope.new(self).apply_finder_options(scope)
                            else
                              raise "Don't know how to set scope with #{scope.class}."
                            end
end

#do_find(*args) ⇒ Object

Method actually doing the find operation. It handles first, last (not supported though), all and find records by id(s). It simply delegates to more spesific methods.

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/massive_record/orm/finders.rb', line 97

def do_find(*args) # :nodoc:
  options = args.extract_options!.to_options

  raise ArgumentError.new("Sorry, conditions are not supported!") if options.has_key? :conditions

  case args.first
  when :first, :last
    send(args.first, options)
  when :all
    find_all(options)
  else
    find_by_ids(*args, options)
  end
end

#exists?(id) ⇒ Boolean

Returns true if a record do exist

Returns:

  • (Boolean)


48
49
50
# File 'lib/massive_record/orm/finders.rb', line 48

def exists?(id)
  !!find(id) rescue false
end

#find_each(*args) ⇒ Object

Similar to all, except that this will use find_in_batches behind the scene.



36
37
38
39
40
41
42
# File 'lib/massive_record/orm/finders.rb', line 36

def find_each(*args)
  find_in_batches(*args) do |rows|
    rows.each do |row|
      yield row
    end
  end
end

#find_in_batches(*args) ⇒ Object

Find records in batches. Makes it easier to work with big data sets where you don’t want to load every record up front.



23
24
25
26
27
28
29
30
# File 'lib/massive_record/orm/finders.rb', line 23

def find_in_batches(*args)
  table.find_in_batches(*args) do |rows|
    records = rows.collect do |row|
      instantiate(*transpose_hbase_row_to_record_attributes_and_raw_data(row))
    end    
    yield records
  end
end

#finder_scopeObject

Entry point for method delegation like find, first, all etc.



57
58
59
60
61
62
63
# File 'lib/massive_record/orm/finders.rb', line 57

def finder_scope
  if default_scoping
    default_scoping.dup
  else
    unscoped
  end
end

#first(*args) ⇒ Object



15
16
17
# File 'lib/massive_record/orm/finders.rb', line 15

def first(*args)
  finder_scope.first(*args)
end

#unscopedObject

Returns an fresh scope object with no limitations set by for instance the default scope



86
87
88
# File 'lib/massive_record/orm/finders.rb', line 86

def unscoped
  Scope.new(self)
end